Skip to content

Commit fe820e1

Browse files
R0WiXueSheng-GIT
andauthored
Fix#273 (#290)
* Check if user can update node ...and add suffix '_OCR' to processed file if not. Signed-off-by: XueSheng-GIT <Michael.Frase@gmx.de> * Node update permissions adjustments * Create dedicated private function for determining new file name * Adjust and add tests * Update Composer Deps --------- Signed-off-by: XueSheng-GIT <Michael.Frase@gmx.de> Co-authored-by: XueSheng-GIT <Michael.Frase@gmx.de>
1 parent d112c22 commit fe820e1

File tree

8 files changed

+119
-120
lines changed

8 files changed

+119
-120
lines changed

composer.lock

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Helper/IProcessingFileAccessor.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525

2626
interface IProcessingFileAccessor {
2727
/**
28-
* Returns the id of the file which is currently
28+
* Returns the path of the file which is currently
2929
* processed via OCR
30-
* @return ?int
30+
* @return ?string
3131
*/
32-
public function getCurrentlyProcessedFileId() : ?int;
32+
public function getCurrentlyProcessedFilePath() : ?string;
3333

3434
/**
35-
* Sets the id of the file which is currently
35+
* Sets the path of the file which is currently
3636
* processed via OCR
3737
*/
38-
public function setCurrentlyProcessedFileId(?int $fileId) : void;
38+
public function setCurrentlyProcessedFilePath(?string $filePath) : void;
3939
}

lib/Helper/ProcessingFileAccessor.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
namespace OCA\WorkflowOcr\Helper;
2525

2626
/**
27-
* This class is a singleton which holds the id
27+
* This class is a singleton which holds the path
2828
* of the currently OCR processed file. This ensures
2929
* that a files is not added to the processing queue
3030
* if the 'postWrite' hook was triggered by a new
3131
* version created by the OCR process.
3232
*/
3333
class ProcessingFileAccessor implements IProcessingFileAccessor {
34-
/** @var ?int */
35-
private $currentlyProcessedFileId;
34+
/** @var ?string */
35+
private $currentlyProcessedFilePath;
3636

3737
/** @var ProcessingFileAccessor */
3838
private static $instance;
@@ -50,14 +50,14 @@ private function __construct() {
5050
/**
5151
* @inheritdoc
5252
*/
53-
public function getCurrentlyProcessedFileId() : ?int {
54-
return $this->currentlyProcessedFileId;
53+
public function getCurrentlyProcessedFilePath() : ?string {
54+
return $this->currentlyProcessedFilePath;
5555
}
5656

5757
/**
5858
* @inheritdoc
5959
*/
60-
public function setCurrentlyProcessedFileId(?int $fileId) : void {
61-
$this->currentlyProcessedFileId = $fileId;
60+
public function setCurrentlyProcessedFilePath(?string $filePath) : void {
61+
$this->currentlyProcessedFilePath = $filePath;
6262
}
6363
}

lib/Operation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ private function tryGetJobArgs(Node $node, $operation, & $argsArray) : bool {
226226

227227
private function eventTriggeredByOcrProcess(Node $node) : bool {
228228
// Check if the event was triggered by OCR rewrite of the file
229-
if ($node->getId() === $this->processingFileAccessor->getCurrentlyProcessedFileId()) {
229+
if ($node->getPath() === $this->processingFileAccessor->getCurrentlyProcessedFilePath()) {
230230
$this->logger->debug('Not processing event because file with path \'{path}\' was written by OCR process.',
231231
['path' => $node->getPath()]);
232232
return true;

lib/Service/OcrService.php

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,13 @@ private function processTagsAfterSuccessfulOcr(File $file, WorkflowSettings $set
245245
/**
246246
* @param string $filePath The filepath of the file to write
247247
* @param string $ocrContent The new filecontent (which was OCR processed)
248-
* @param int $fileId The id of the file to write. Used for locking.
249248
* @param int $fileMtime The mtime of the new file. Can be used to restore the original modification time of the non-OCR file.
250249
*/
251-
private function createNewFileVersion(string $filePath, string $ocrContent, int $fileId, ?int $fileMtime = null) : void {
250+
private function createNewFileVersion(string $filePath, string $ocrContent, ?int $fileMtime = null) : void {
252251
$dirPath = dirname($filePath);
253252
$filename = basename($filePath);
254253

255-
$this->processingFileAccessor->setCurrentlyProcessedFileId($fileId);
254+
$this->processingFileAccessor->setCurrentlyProcessedFilePath($filePath);
256255

257256
try {
258257
$view = $this->viewFactory->create($dirPath);
@@ -267,7 +266,7 @@ private function createNewFileVersion(string $filePath, string $ocrContent, int
267266
$view->touch($filename, $fileMtime);
268267
}
269268
} finally {
270-
$this->processingFileAccessor->setCurrentlyProcessedFileId(null);
269+
$this->processingFileAccessor->setCurrentlyProcessedFilePath(null);
271270
}
272271
}
273272

@@ -307,24 +306,20 @@ private function setFileVersionsLabel(File $file, string $uid, string $label): v
307306
private function doPostProcessing(Node $file, string $uid, WorkflowSettings $settings, OcrProcessorResult $result, ?int $fileMtime = null): void {
308307
$this->processTagsAfterSuccessfulOcr($file, $settings);
309308

310-
$filePath = $file->getPath();
311309
$fileId = $file->getId();
312310
$fileContent = $result->getFileContent();
313311
$originalFileExtension = $file->getExtension();
314312
$newFileExtension = $result->getFileExtension();
315313

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

323-
$newFilePath = $originalFileExtension === $newFileExtension ?
324-
$filePath :
325-
$filePath . '.pdf';
326-
327-
$this->createNewFileVersion($newFilePath, $fileContent, $fileId, $fileMtime);
321+
$newFilePath = $this->determineNewFilePath($file, $originalFileExtension, $newFileExtension);
322+
$this->createNewFileVersion($newFilePath, $fileContent, $fileMtime);
328323
}
329324

330325
$this->eventService->textRecognized($result, $file);
@@ -333,4 +328,28 @@ private function doPostProcessing(Node $file, string $uid, WorkflowSettings $set
333328
$this->notificationService->createSuccessNotification($uid, $fileId);
334329
}
335330
}
331+
332+
/**
333+
* Determines the new file path for a given file by analyzing the original- and new file extension.
334+
* Also takes into consideration, if the file can be updated by the current user.
335+
*
336+
* @param Node $file The original file node for which the OCR processing has been succeeded.
337+
* @param string $originalFileExtension The original file extension.
338+
* @param string $newFileExtension The new file extension to be applied.
339+
* @return string The new file path with the updated extension.
340+
*/
341+
private function determineNewFilePath(Node $file, string $originalFileExtension, string $newFileExtension): string {
342+
$filePath = $file->getPath();
343+
if ($originalFileExtension !== $newFileExtension) {
344+
// If the extension changed, will create a new file with the new extension
345+
return $filePath . '.' . $newFileExtension;
346+
}
347+
if (!$file->isUpdateable()) {
348+
// Add suffix '_OCR' if original file cannot be updated
349+
$fileInfo = pathinfo($filePath);
350+
return $fileInfo['dirname'] . '/' . $fileInfo['filename'] . '_OCR.' . $newFileExtension;
351+
}
352+
// By returning the original file path, we will create a new file version of the original file
353+
return $filePath;
354+
}
336355
}

tests/Unit/Helper/ProcessingFIleAccessorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public function testSingleton() {
3636

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

0 commit comments

Comments
 (0)