From d7ea3f9fbab4cb0899a9df7acd16a5833674f20b Mon Sep 17 00:00:00 2001 From: XueSheng-GIT Date: Sun, 5 Jan 2025 07:53:24 +0100 Subject: [PATCH] Check if user can update node ...and add suffix '_OCR' to processed file if not. Signed-off-by: XueSheng-GIT --- lib/Helper/IProcessingFileAccessor.php | 10 ++++----- lib/Helper/ProcessingFileAccessor.php | 14 ++++++------- lib/Operation.php | 2 +- lib/Service/OcrService.php | 21 +++++++++++++------ .../Helper/ProcessingFIleAccessorTest.php | 6 +++--- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/lib/Helper/IProcessingFileAccessor.php b/lib/Helper/IProcessingFileAccessor.php index 251a3a8..23c5b2d 100644 --- a/lib/Helper/IProcessingFileAccessor.php +++ b/lib/Helper/IProcessingFileAccessor.php @@ -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; } diff --git a/lib/Helper/ProcessingFileAccessor.php b/lib/Helper/ProcessingFileAccessor.php index 2ec1760..8b6a19f 100644 --- a/lib/Helper/ProcessingFileAccessor.php +++ b/lib/Helper/ProcessingFileAccessor.php @@ -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; @@ -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; } } diff --git a/lib/Operation.php b/lib/Operation.php index 8f07aa1..2c1848e 100644 --- a/lib/Operation.php +++ b/lib/Operation.php @@ -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; diff --git a/lib/Service/OcrService.php b/lib/Service/OcrService.php index 92dcfc6..7d7cebc 100644 --- a/lib/Service/OcrService.php +++ b/lib/Service/OcrService.php @@ -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); @@ -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); } } @@ -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); } diff --git a/tests/Unit/Helper/ProcessingFIleAccessorTest.php b/tests/Unit/Helper/ProcessingFIleAccessorTest.php index 4d220e1..31eadc2 100644 --- a/tests/Unit/Helper/ProcessingFIleAccessorTest.php +++ b/tests/Unit/Helper/ProcessingFIleAccessorTest.php @@ -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); } }