[BUGFIX] Processing folder has now nested subfolders
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Processing / LocalImageProcessor.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Processing;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Imaging\GraphicalFunctions;
18 use TYPO3\CMS\Core\Log\LogManager;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Processes Local Images files
23 */
24 class LocalImageProcessor implements ProcessorInterface
25 {
26 /**
27 * @var \TYPO3\CMS\Core\Log\Logger
28 */
29 protected $logger;
30
31 /**
32 * Constructor
33 */
34 public function __construct()
35 {
36 /** @var $logManager LogManager */
37 $logManager = GeneralUtility::makeInstance(LogManager::class);
38 $this->logger = $logManager->getLogger(__CLASS__);
39 }
40
41 /**
42 * Returns TRUE if this processor can process the given task.
43 *
44 * @param TaskInterface $task
45 * @return bool
46 */
47 public function canProcessTask(TaskInterface $task)
48 {
49 $canProcessTask = $task->getType() === 'Image';
50 $canProcessTask = $canProcessTask & in_array($task->getName(), array('Preview', 'CropScaleMask'));
51 return $canProcessTask;
52 }
53
54 /**
55 * Processes the given task.
56 *
57 * @param TaskInterface $task
58 * @throws \InvalidArgumentException
59 */
60 public function processTask(TaskInterface $task)
61 {
62 if (!$this->canProcessTask($task)) {
63 throw new \InvalidArgumentException('Cannot process task of type "' . $task->getType() . '.' . $task->getName() . '"', 1350570621);
64 }
65 if ($this->checkForExistingTargetFile($task)) {
66 return;
67 }
68 $helper = $this->getHelperByTaskName($task->getName());
69 try {
70 $result = $helper->process($task);
71 if ($result === null) {
72 $task->setExecuted(true);
73 $task->getTargetFile()->setUsesOriginalFile();
74 } elseif (!empty($result['filePath']) && file_exists($result['filePath'])) {
75 $task->setExecuted(true);
76 $imageDimensions = $this->getGraphicalFunctionsObject()->getImageDimensions($result['filePath']);
77 $task->getTargetFile()->setName($task->getTargetFileName());
78 $task->getTargetFile()->updateProperties(
79 array('width' => $imageDimensions[0], 'height' => $imageDimensions[1], 'size' => filesize($result['filePath']), 'checksum' => $task->getConfigurationChecksum())
80 );
81 $task->getTargetFile()->updateWithLocalFile($result['filePath']);
82
83 // New dimensions + no new file (for instance svg)
84 } elseif (!empty($result['width']) && !empty($result['height']) && empty($result['filePath'])) {
85 $task->setExecuted(true);
86 $task->getTargetFile()->setUsesOriginalFile();
87 $task->getTargetFile()->updateProperties(
88 array('width' => $result['width'], 'height' => $result['height'], 'size' => $task->getSourceFile()->getSize(), 'checksum' => $task->getConfigurationChecksum())
89 );
90
91 // Seems we have no valid processing result
92 } else {
93 $task->setExecuted(false);
94 }
95 } catch (\Exception $e) {
96 $task->setExecuted(false);
97 }
98 }
99
100 /**
101 * Check if the to be processed target file already exists
102 * if exist take info from that file and mark task as done
103 *
104 * @param TaskInterface $task
105 * @return bool
106 */
107 protected function checkForExistingTargetFile(TaskInterface $task)
108 {
109 // the storage of the processed file, not of the original file!
110 $storage = $task->getTargetFile()->getStorage();
111 $processingFolder = $storage->getProcessingFolder($task->getSourceFile());
112
113 // explicitly check for the raw filename here, as we check for files that existed before we even started
114 // processing, i.e. that were processed earlier
115 if ($processingFolder->hasFile($task->getTargetFileName())) {
116 // When the processed file already exists set it as processed file
117 $task->getTargetFile()->setName($task->getTargetFileName());
118
119 // If the processed file is stored on a remote server, we must fetch a local copy of the file, as we
120 // have no API for fetching file metadata from a remote file.
121 $localProcessedFile = $storage->getFileForLocalProcessing($task->getTargetFile(), false);
122 $task->setExecuted(true);
123 $imageDimensions = $this->getGraphicalFunctionsObject()->getImageDimensions($localProcessedFile);
124 $properties = array(
125 'width' => $imageDimensions[0],
126 'height' => $imageDimensions[1],
127 'size' => filesize($localProcessedFile),
128 'checksum' => $task->getConfigurationChecksum()
129 );
130 $task->getTargetFile()->updateProperties($properties);
131
132 return true;
133 } else {
134 return false;
135 }
136 }
137
138 /**
139 * @param string $taskName
140 * @return LocalCropScaleMaskHelper|LocalPreviewHelper
141 * @throws \InvalidArgumentException
142 */
143 protected function getHelperByTaskName($taskName)
144 {
145 switch ($taskName) {
146 case 'Preview':
147 $helper = GeneralUtility::makeInstance(LocalPreviewHelper::class, $this);
148 break;
149 case 'CropScaleMask':
150 $helper = GeneralUtility::makeInstance(LocalCropScaleMaskHelper::class, $this);
151 break;
152 default:
153 throw new \InvalidArgumentException('Cannot find helper for task name: "' . $taskName . '"', 1353401352);
154 }
155
156 return $helper;
157 }
158
159 /**
160 * @return GraphicalFunctions
161 */
162 protected function getGraphicalFunctionsObject()
163 {
164 static $graphicalFunctionsObject = null;
165
166 if ($graphicalFunctionsObject === null) {
167 $graphicalFunctionsObject = GeneralUtility::makeInstance(GraphicalFunctions::class);
168 }
169
170 return $graphicalFunctionsObject;
171 }
172 }