Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if user can update node #286

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading