[TASK] Do not generate new processed file if it already exists 09/39709/5
authorFrans Saris <franssaris@gmail.com>
Mon, 25 May 2015 14:48:53 +0000 (16:48 +0200)
committerFrank Nägler <typo3@naegler.net>
Sat, 30 May 2015 19:44:08 +0000 (21:44 +0200)
When a processed file already exists but the sys_file_processedfile
record is deleted the whole processing task is executed again even
if the file already exists.

This change first checks if `targetFile` already exists and if so
uses that file and marks the task as done.

Currently this only works for `Local` file storages as the FAL API
misses proper methods get a file by folder identifier and file name.
When https://review.typo3.org/38479/ is merged a follow up is
needed for master #67126.

Resolves: #67095
Releases: master
Change-Id: I352428ddaf1d2afe4bbf031992b484ade3635e4a
Reviewed-on: http://review.typo3.org/39709
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Frank Nägler <typo3@naegler.net>
Tested-by: Frank Nägler <typo3@naegler.net>
typo3/sysext/core/Classes/Resource/ProcessedFile.php
typo3/sysext/core/Classes/Resource/Processing/LocalImageProcessor.php

index 77915f0..cbf3312 100644 (file)
@@ -113,6 +113,7 @@ class ProcessedFile extends AbstractFile {
         */
        public function __construct(File $originalFile, $taskType, array $processingConfiguration, array $databaseRow = NULL) {
                $this->originalFile = $originalFile;
+               $this->originalFileSha1 = $this->originalFile->getSha1();
                $this->storage = $originalFile->getStorage()->getProcessingFolder()->getStorage();
                $this->taskType = $taskType;
                $this->processingConfiguration = $processingConfiguration;
index 537f79b..b568f03 100644 (file)
@@ -57,6 +57,9 @@ class LocalImageProcessor implements ProcessorInterface {
                if (!$this->canProcessTask($task)) {
                        throw new \InvalidArgumentException('Cannot process task of type "' . $task->getType() . '.' . $task->getName() . '"', 1350570621);
                }
+               if ($this->checkForExistingTargetFile($task)) {
+                       return;
+               }
                $helper = $this->getHelperByTaskName($task->getName());
                try {
                        $result = $helper->process($task);
@@ -90,6 +93,44 @@ class LocalImageProcessor implements ProcessorInterface {
        }
 
        /**
+        * Check if the to be processed target file already exists
+        * if exist take info from that file and mark task as done
+        *
+        * @param TaskInterface $task
+        * @return bool
+        */
+       protected function checkForExistingTargetFile(TaskInterface $task) {
+               $processingFolder = $task->getTargetFile()->getStorage()->getProcessingFolder();
+               $storage = $task->getTargetFile()->getStorage();
+               // @todo: make proper use of the FAL API, see https://forge.typo3.org/issues/67126
+               if ($processingFolder->hasFile($task->getTargetFileName()) && $storage->getDriverType() === 'Local') {
+                       $processedFileIdentifier = rtrim($processingFolder->getIdentifier(), '/') . '/' . $task->getTargetFileName();
+                       $configuration = $storage->getConfiguration();
+                       if ($configuration['pathType'] === 'relative') {
+                               $absoluteBasePath = PATH_site . $configuration['basePath'];
+                       } else {
+                               $absoluteBasePath = $configuration['basePath'];
+                       }
+                       $targetFile = $absoluteBasePath . ltrim($processedFileIdentifier, '/');
+
+                       $task->setExecuted(TRUE);
+                       $imageDimensions = $this->getGraphicalFunctionsObject()->getImageDimensions($targetFile);
+                       $task->getTargetFile()->setName($task->getTargetFileName());
+                       $properties = array(
+                               'width' => $imageDimensions[0],
+                               'height' => $imageDimensions[1],
+                               'size' => filesize($targetFile),
+                               'checksum' => $task->getConfigurationChecksum()
+                       );
+                       $task->getTargetFile()->updateProperties($properties);
+
+                       return TRUE;
+               } else {
+                       return FALSE;
+               }
+       }
+
+       /**
         * @param string $taskName
         * @return LocalCropScaleMaskHelper|LocalPreviewHelper
         * @throws \InvalidArgumentException