@@ -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}
0 commit comments