Skip to content

Commit

Permalink
Check if user can update node
Browse files Browse the repository at this point in the history
...and add suffix '_OCR' to processed file if not.

Signed-off-by: XueSheng-GIT <[email protected]>
  • Loading branch information
XueSheng-GIT committed Jan 5, 2025
1 parent 3a623a0 commit d7ea3f9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
10 changes: 5 additions & 5 deletions lib/Helper/IProcessingFileAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@

interface IProcessingFileAccessor {
/**
* Returns the id of the file which is currently
* Returns the path of the file which is currently
* processed via OCR
* @return ?int
* @return ?string
*/
public function getCurrentlyProcessedFileId() : ?int;
public function getCurrentlyProcessedFilePath() : ?string;

/**
* Sets the id of the file which is currently
* Sets the path of the file which is currently
* processed via OCR
*/
public function setCurrentlyProcessedFileId(?int $fileId) : void;
public function setCurrentlyProcessedFilePath(?string $filePath) : void;
}
14 changes: 7 additions & 7 deletions lib/Helper/ProcessingFileAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
namespace OCA\WorkflowOcr\Helper;

/**
* This class is a singleton which holds the id
* This class is a singleton which holds the path
* of the currently OCR processed file. This ensures
* that a files is not added to the processing queue
* if the 'postWrite' hook was triggered by a new
* version created by the OCR process.
*/
class ProcessingFileAccessor implements IProcessingFileAccessor {
/** @var ?int */
private $currentlyProcessedFileId;
/** @var ?string */
private $currentlyProcessedFilePath;

/** @var ProcessingFileAccessor */
private static $instance;
Expand All @@ -50,14 +50,14 @@ private function __construct() {
/**
* @inheritdoc
*/
public function getCurrentlyProcessedFileId() : ?int {
return $this->currentlyProcessedFileId;
public function getCurrentlyProcessedFilePath() : ?string {
return $this->currentlyProcessedFilePath;
}

/**
* @inheritdoc
*/
public function setCurrentlyProcessedFileId(?int $fileId) : void {
$this->currentlyProcessedFileId = $fileId;
public function setCurrentlyProcessedFilePath(?string $filePath) : void {
$this->currentlyProcessedFilePath = $filePath;
}
}
2 changes: 1 addition & 1 deletion lib/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private function tryGetJobArgs(Node $node, $operation, & $argsArray) : bool {

private function eventTriggeredByOcrProcess(Node $node) : bool {
// Check if the event was triggered by OCR rewrite of the file
if ($node->getId() === $this->processingFileAccessor->getCurrentlyProcessedFileId()) {
if ($node->getPath() === $this->processingFileAccessor->getCurrentlyProcessedFilePath()) {
$this->logger->debug('Not processing event because file with path \'{path}\' was written by OCR process.',
['path' => $node->getPath()]);
return true;
Expand Down
21 changes: 15 additions & 6 deletions lib/Service/OcrService.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private function createNewFileVersion(string $filePath, string $ocrContent, int
$dirPath = dirname($filePath);
$filename = basename($filePath);

$this->processingFileAccessor->setCurrentlyProcessedFileId($fileId);
$this->processingFileAccessor->setCurrentlyProcessedFilePath($filePath);

try {
$view = $this->viewFactory->create($dirPath);
Expand All @@ -267,7 +267,7 @@ private function createNewFileVersion(string $filePath, string $ocrContent, int
$view->touch($filename, $fileMtime);
}
} finally {
$this->processingFileAccessor->setCurrentlyProcessedFileId(null);
$this->processingFileAccessor->setCurrentlyProcessedFilePath(null);
}
}

Expand Down Expand Up @@ -315,14 +315,23 @@ private function doPostProcessing(Node $file, string $uid, WorkflowSettings $set

// Only create a new file version if the file OCR result was not empty #130
if ($result->getRecognizedText() !== '') {
if ($settings->getKeepOriginalFileVersion()) {
if ($settings->getKeepOriginalFileVersion() && $file->isUpdateable()) {
// Add label to original file to prevent its expiry
$this->setFileVersionsLabel($file, $uid, self::FILE_VERSION_LABEL_VALUE);
}

$newFilePath = $originalFileExtension === $newFileExtension ?
$filePath :
$filePath . '.pdf';
if ($originalFileExtension === $newFileExtension) {
if (!$file->isUpdateable()) {
// Add suffix '_OCR' if original file cannot be updated
$fileInfo = pathinfo($filePath);
$newFilePath = $fileInfo['dirname'] . '/' . $fileInfo['filename'] . '_OCR.' . $originalFileExtension;
} else {
$newFilePath = $filePath;
}
} else {
$newFilePath = $filePath . '.pdf';
}


$this->createNewFileVersion($newFilePath, $fileContent, $fileId, $fileMtime);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Helper/ProcessingFIleAccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function testSingleton() {

public function testGetSet() {
$o = ProcessingFileAccessor::getInstance();
$o ->setCurrentlyProcessedFileId(42);
$this->assertEquals(42, $o->getCurrentlyProcessedFileId());
$o->setCurrentlyProcessedFileId(null);
$o ->setCurrentlyProcessedFilePath('/someuser/files/somefile.pdf');
$this->assertEquals('/someuser/files/somefile.pdf', $o->getCurrentlyProcessedFilePath());
$o->setCurrentlyProcessedFilePath(null);
}
}

0 comments on commit d7ea3f9

Please sign in to comment.