Skip to content
Merged
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
40 changes: 24 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions lib/Exception/OcrAlreadyDoneException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2025 Robin Windey <ro.windey@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\WorkflowOcr\Exception;

use Exception;

class OcrAlreadyDoneException extends Exception {
public function __construct(string $message) {
$this->message = $message;
}
}
5 changes: 5 additions & 0 deletions lib/OcrProcessors/Local/OcrMyPdfBasedProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\WorkflowOcr\OcrProcessors\Local;

use OCA\WorkflowOcr\Exception\OcrAlreadyDoneException;
use OCA\WorkflowOcr\Exception\OcrNotPossibleException;
use OCA\WorkflowOcr\Exception\OcrResultEmptyException;
use OCA\WorkflowOcr\Helper\ISidecarFileAccessor;
Expand Down Expand Up @@ -63,6 +64,10 @@ public function ocrFile(File $file, WorkflowSettings $settings, GlobalSettings $
$exitCode = $this->command->getExitCode();

if (!$success) {
# Gracefully handle OCR_MODE_SKIP_FILE (ExitCode.already_done_ocr)
if ($exitCode === 6) {
throw new OcrAlreadyDoneException('File ' . $file->getPath() . ' appears to contain text so it may not need OCR. Message: ' . $errorOutput . ' ' . $stdErr);
}
throw new OcrNotPossibleException('OCRmyPDF exited abnormally with exit-code ' . $exitCode . ' for file ' . $file->getPath() . '. Message: ' . $errorOutput . ' ' . $stdErr);
}

Expand Down
10 changes: 9 additions & 1 deletion lib/OcrProcessors/Remote/WorkflowOcrRemoteProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\WorkflowOcr\OcrProcessors\Remote;

use OCA\WorkflowOcr\Exception\OcrAlreadyDoneException;
use OCA\WorkflowOcr\Exception\OcrNotPossibleException;
use OCA\WorkflowOcr\Model\GlobalSettings;
use OCA\WorkflowOcr\Model\WorkflowSettings;
Expand Down Expand Up @@ -56,7 +57,14 @@ public function ocrFile(File $file, WorkflowSettings $settings, GlobalSettings $
$this->logger->debug('OCR result received', ['apiResult' => $apiResult]);

if ($apiResult instanceof ErrorResult) {
throw new OcrNotPossibleException($apiResult->getMessage());
$resultMessage = $apiResult->getMessage();
$exitCode = $apiResult->getOcrMyPdfExitCode();

# Gracefully handle OCR_MODE_SKIP_FILE (ExitCode.already_done_ocr)
if ($exitCode === 6) {
throw new OcrAlreadyDoneException('File ' . $file->getPath() . ' appears to contain text so it may not need OCR. Message: ' . $resultMessage);
}
throw new OcrNotPossibleException($resultMessage);
}

return new OcrProcessorResult(
Expand Down
6 changes: 3 additions & 3 deletions lib/Service/OcrService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
use OCA\Files_Versions\Versions\IMetadataVersion;
use OCA\Files_Versions\Versions\IMetadataVersionBackend;
use OCA\Files_Versions\Versions\IVersionManager;
use OCA\WorkflowOcr\Exception\OcrResultEmptyException;
use OCA\WorkflowOcr\Exception\OcrAlreadyDoneException;
use OCA\WorkflowOcr\Helper\IProcessingFileAccessor;
use OCA\WorkflowOcr\Model\WorkflowSettings;
use OCA\WorkflowOcr\OcrProcessors\IOcrProcessorFactory;
Expand Down Expand Up @@ -158,8 +158,8 @@ public function runOcrProcess(int $fileId, string $uid, WorkflowSettings $settin

try {
$result = $ocrProcessor->ocrFile($file, $settings, $globalSettings);
} catch (OcrResultEmptyException $ex) {
// #232: it's okay to have an empty result if the file was skipped due to OCR mode
} catch (OcrAlreadyDoneException $ex) {
// #232: Skip file if OCR was already done
if ($settings->getOcrMode() === WorkflowSettings::OCR_MODE_SKIP_FILE) {
$this->logger->debug('Skipping empty OCR result for file with id {fileId} because OCR mode is set to \'skip file\'', ['fileId' => $fileId]);
return;
Expand Down
17 changes: 14 additions & 3 deletions tests/Integration/BackendTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function checkOcrMyPdfInstalled() : bool {
return $command->getExitCode() === 0;
}

protected function addOperation(string $mimeType) {
protected function addOperation(string $mimeType, string $operationJson = '') {
// NOTE :: we're creating the workflow operation via
// REST API because if we'd use the manager directly, we'd
// face some issues because of caching etc (test ist running
Expand All @@ -118,10 +118,9 @@ protected function addOperation(string $mimeType) {
"invalid":false
}
],
"operation":"",
"operation":"' . str_replace('"', '\"', $operationJson) . '",
"valid":true
}';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
Expand All @@ -135,6 +134,18 @@ protected function addOperation(string $mimeType) {
}

private function deleteOperation() {
// Clear managers cache and operations to ensure "deleteOperation" works
$reflection = new \ReflectionClass($this->workflowEngineManager);

$operationsByScopeProperty = $reflection->getProperty('operationsByScope');
$operationsByScopeProperty->setAccessible(true);
$operationsByScope = $operationsByScopeProperty->getValue($this->workflowEngineManager);
$operationsByScope->clear();

$operationsProperty = $reflection->getProperty('operations');
$operationsProperty->setAccessible(true);
$operationsProperty->setValue($this->workflowEngineManager, []);

$operations = $this->workflowEngineManager->getOperations($this->operationClass, $this->context);
foreach ($operations as $operation) {
try {
Expand Down
5 changes: 5 additions & 0 deletions tests/Integration/IntegrationTestApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ public function getRequests(): array {
public function getResponses(): array {
return $this->responses;
}

public function reset(): void {
$this->requests = [];
$this->responses = [];
}
}
31 changes: 31 additions & 0 deletions tests/Integration/LocalBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace OCA\WorkflowOcr\Tests\Integration;

use OCA\WorkflowOcr\Events\TextRecognizedEvent;
use OCA\WorkflowOcr\Model\WorkflowSettings;
use OCA\WorkflowOcr\OcrProcessors\Remote\Client\IApiClient;
use OCP\EventDispatcher\IEventDispatcher;

Expand Down Expand Up @@ -76,6 +77,7 @@ protected function setUp(): void {
protected function tearDown(): void {
parent::tearDown();
$this->dispatcher->removeListener(TextRecognizedEvent::class, $this->eventListener);
$this->capturedEvents = [];
}

/**
Expand All @@ -92,4 +94,33 @@ public function testWorkflowOcrLocalBackend(): void {
$this->assertInstanceOf(TextRecognizedEvent::class, $textRecognizedEvent, 'Expected TextRecognizedEvent instance');
$this->assertEquals('This document is ready for OCR', trim($textRecognizedEvent->getRecognizedText()), 'Expected recognized text');
}

/**
* Test processing a file via ocrmypdf CLI where file already contains OCR and mode is set to "skip file".
*/
public function testWorkflowOcrLocalBackendSkipFile(): void {
$this->addOperation('application/pdf', '{"ocrMode":' . WorkflowSettings::OCR_MODE_SKIP_FILE . '}');
$this->uploadTestFile('document-has-ocr.pdf');
$this->runOcrBackgroundJob();

$this->assertEmpty($this->apiClient->getRequests(), 'Expected no OCR Backend Service requests');
$this->assertEquals(0, count($this->capturedEvents), 'Expected no TextRecognizedEvent');
}

/**
* Test processing a file via ocrmypdf CLI where file already contains OCR and mode is set to "skip text".
*/
public function testWorkflowOcrLocalBackendSkipText(): void {
$this->addOperation('application/pdf', '{"ocrMode":' . WorkflowSettings::OCR_MODE_SKIP_TEXT . '}');
$this->uploadTestFile('document-has-ocr.pdf');
$this->runOcrBackgroundJob();

$this->assertEmpty($this->apiClient->getRequests(), 'Expected no OCR Backend Service requests');
$this->assertEquals(1, count($this->capturedEvents), 'Expected no TextRecognizedEvent');

// Ocrmypdf will "recognize" a special text for pages which already have OCR
$textRecognizedEvent = $this->capturedEvents[0];
$this->assertInstanceOf(TextRecognizedEvent::class, $textRecognizedEvent, 'Expected TextRecognizedEvent instance');
$this->assertEquals('[OCR skipped on page(s) 1]', trim($textRecognizedEvent->getRecognizedText()), 'Expected recognized text');
}
}
21 changes: 21 additions & 0 deletions tests/Integration/OcrBackendServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

namespace OCA\WorkflowOcr\Tests\Integration;

use OCA\WorkflowOcr\Model\WorkflowSettings;
use OCA\WorkflowOcr\OcrProcessors\Remote\Client\IApiClient;
use OCA\WorkflowOcr\OcrProcessors\Remote\Client\Model\ErrorResult;
use OCA\WorkflowOcr\OcrProcessors\Remote\Client\Model\OcrResult;

/**
Expand Down Expand Up @@ -54,6 +56,7 @@ protected function setUp(): void {
}

$this->apiClient = $this->container->get(IntegrationTestApiClient::class);
$this->apiClient->reset();
$this->overwriteService(IApiClient::class, $this->apiClient);
}

Expand All @@ -77,4 +80,22 @@ public function testWorkflowOcrBackendService() {
$this->assertEquals('application/pdf', $ocrResult->getContentType(), 'Expected content type in response');
$this->assertTrue(strpos($ocrResult->getRecognizedText(), 'This document is ready for OCR') >= 0, 'Expected recognized text in response');
}

public function testWorkflowOcrBackendServiceSkipFile() {
$this->addOperation('application/pdf', '{"ocrMode":' . WorkflowSettings::OCR_MODE_SKIP_FILE . '}');
$this->uploadTestFile('document-has-ocr.pdf');
$this->runOcrBackgroundJob();

$requests = $this->apiClient->getRequests();
$this->assertCount(1, $requests, 'Expected 1 OCR request');
$request = $requests[0];
$this->assertTrue($request['ocrMyPdfParameters'] === '', 'Expected OCR parameters in request');

$responses = $this->apiClient->getResponses();
$this->assertCount(1, $responses, 'Expected 1 OCR response');
$this->assertTrue($responses[0] instanceof ErrorResult, 'Expected ErrorResult instance, type is: ' . get_class($responses[0]));
/** @var ErrorResult */
$ocrResult = $responses[0];
$this->assertEquals($ocrResult->getOcrMyPdfExitCode(), 6, 'Expected ocrmypdf ExitCode 6');
}
}
Binary file added tests/Integration/testdata/document-has-ocr.pdf
Binary file not shown.
30 changes: 30 additions & 0 deletions tests/Unit/OcrProcessors/Local/PdfOcrProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\WorkflowOcr\Tests\Unit\OcrProcessors\Local;

use OCA\WorkflowOcr\Exception\OcrAlreadyDoneException;
use OCA\WorkflowOcr\Exception\OcrNotPossibleException;
use OCA\WorkflowOcr\Exception\OcrResultEmptyException;
use OCA\WorkflowOcr\Helper\ISidecarFileAccessor;
Expand Down Expand Up @@ -386,6 +387,35 @@ public function testAppliesCustomCliArgsCorrectly() {
$processor->ocrFile($this->fileBefore, $workflowSettings, $this->defaultGlobalSettings);
}

public function testThrowsOcrAlreadyDoneExceptionIfErrorCodeIsEquals6() {
$this->command->expects($this->once())
->method('setCommand')
->willReturn($this->command);
$this->command->expects($this->once())
->method('execute')
->willReturn(false);
$this->command->expects($this->once())
->method('getError')
->willReturn('error');
$this->command->expects($this->once())
->method('getStdErr')
->willReturn('stdErr');
$this->command->expects($this->once())
->method('getExitCode')
->willReturn(6);

$thrown = false;
try {
$processor = new PdfOcrProcessor($this->command, $this->logger, $this->sidecarFileAccessor, $this->commandLineUtils);
$processor->ocrFile($this->fileBefore, $this->defaultSettings, $this->defaultGlobalSettings);
} catch (\Throwable $t) {
$thrown = true;
$this->assertInstanceOf(OcrAlreadyDoneException::class, $t);
}

$this->assertTrue($thrown);
}

public static function dataProvider_testAppliesOcrModeParameter() {
return [
[WorkflowSettings::OCR_MODE_SKIP_TEXT, ' --skip-text '],
Expand Down
Loading