Skip to content

Commit ffce79d

Browse files
authored
Implement #130 (#160) (#161)
Only create new file version if OCR result was not empty Signed-off-by: Robin Windey <ro.windey@gmail.com> Signed-off-by: Robin Windey <ro.windey@gmail.com> Signed-off-by: Robin Windey <ro.windey@gmail.com>
1 parent 221f3f2 commit ffce79d

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

lib/BackgroundJobs/ProcessFileJob.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,21 @@ private function processFile(string $filePath, WorkflowSettings $settings) : voi
184184
return;
185185
}
186186

187-
188187
$fileContent = $ocrFile->getFileContent();
189188
$nodeId = $node->getId();
190189
$originalFileExtension = $node->getExtension();
191190
$newFileExtension = $ocrFile->getFileExtension();
192191

193-
if ($originalFileExtension === $newFileExtension) {
194-
$this->createNewFileVersion($filePath, $fileContent, $nodeId);
195-
$this->eventService->textRecognized($ocrFile, $node);
196-
} else {
197-
$this->createNewFileVersion($filePath.".pdf", $fileContent, $nodeId);
198-
$this->eventService->textRecognized($ocrFile, $node);
192+
// Only create a new file version if the file OCR result was not empty #130
193+
if ($ocrFile->getRecognizedText() !== '') {
194+
$newFilePath = $originalFileExtension === $newFileExtension ?
195+
$filePath :
196+
$filePath . ".pdf";
197+
198+
$this->createNewFileVersion($newFilePath, $fileContent, $nodeId);
199199
}
200+
201+
$this->eventService->textRecognized($ocrFile, $node);
200202
}
201203

202204
private function getNode(string $filePath) : ?Node {

tests/Unit/BackgroundJobs/ProcessFileJobTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,38 @@ public function testCallsProcessingFileAccessor(array $arguments, string $user,
403403
$this->assertEquals(1, $calledWithNull);
404404
}
405405

406+
/**
407+
* @dataProvider dataProvider_ValidArguments
408+
*/
409+
public function testDoesNotCreateNewFileVersionIfOcrContentWasEmpty(array $arguments, string $user, string $rootFolderPath, string $originalFileExtension, string $expectedOcrFilename) {
410+
$this->processFileJob->setArgument($arguments);
411+
$mimeType = 'application/pdf';
412+
$content = 'someFileContent';
413+
$ocrContent = '';
414+
$ocrResult = new OcrProcessorResult($ocrContent, "pdf", $ocrContent);
415+
416+
$fileMock = $this->createValidFileMock($mimeType, $content);
417+
$this->rootFolder->method('get')
418+
->willReturn($fileMock);
419+
420+
$this->ocrService->expects($this->once())
421+
->method('ocrFile')
422+
->willReturn($ocrResult);
423+
424+
$viewMock = $this->createMock(IView::class);
425+
$this->viewFactory->expects($this->never())
426+
->method('create')
427+
->willReturn($viewMock);
428+
429+
$this->processingFileAccessor->expects($this->never())
430+
->method('setCurrentlyProcessedFileId');
431+
432+
$this->eventService->expects($this->once())
433+
->method('textRecognized');
434+
435+
$this->processFileJob->execute($this->jobList);
436+
}
437+
406438
public function dataProvider_InvalidArguments() {
407439
$arr = [
408440
[null, 1],

0 commit comments

Comments
 (0)