[BUGFIX] Only persist processedFile if processing was successful
[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\Utility\GeneralUtility;
18
19 /**
20 * Processes Local Images files
21 */
22 class LocalImageProcessor implements ProcessorInterface {
23
24 /**
25 * @var \TYPO3\CMS\Core\Log\Logger
26 */
27 protected $logger;
28
29 /**
30 * Constructor
31 */
32 public function __construct() {
33 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
34 $logManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class);
35 $this->logger = $logManager->getLogger(__CLASS__);
36 }
37
38 /**
39 * Returns TRUE if this processor can process the given task.
40 *
41 * @param TaskInterface $task
42 * @return bool
43 */
44 public function canProcessTask(TaskInterface $task) {
45 $canProcessTask = $task->getType() === 'Image';
46 $canProcessTask = $canProcessTask & in_array($task->getName(), array('Preview', 'CropScaleMask'));
47 return $canProcessTask;
48 }
49
50 /**
51 * Processes the given task.
52 *
53 * @param TaskInterface $task
54 * @throws \InvalidArgumentException
55 */
56 public function processTask(TaskInterface $task) {
57 if (!$this->canProcessTask($task)) {
58 throw new \InvalidArgumentException('Cannot process task of type "' . $task->getType() . '.' . $task->getName() . '"', 1350570621);
59 }
60 $helper = $this->getHelperByTaskName($task->getName());
61 try {
62 $result = $helper->process($task);
63 if ($result === NULL) {
64 $task->setExecuted(TRUE);
65 $task->getTargetFile()->setUsesOriginalFile();
66 } elseif (!empty($result['filePath']) && file_exists($result['filePath'])) {
67 $task->setExecuted(TRUE);
68 $imageDimensions = $this->getGraphicalFunctionsObject()->getImageDimensions($result['filePath']);
69 $task->getTargetFile()->setName($task->getTargetFileName());
70 $task->getTargetFile()->updateProperties(
71 array('width' => $imageDimensions[0], 'height' => $imageDimensions[1], 'size' => filesize($result['filePath']), 'checksum' => $task->getConfigurationChecksum())
72 );
73 $task->getTargetFile()->updateWithLocalFile($result['filePath']);
74
75 // New dimensions + no new file (for instance svg)
76 } elseif (!empty($result['width']) && !empty($result['height']) && empty($result['filePath'])) {
77 $task->setExecuted(TRUE);
78 $task->getTargetFile()->setUsesOriginalFile();
79 $task->getTargetFile()->updateProperties(
80 array('width' => $result['width'], 'height' => $result['height'], 'size' => $task->getSourceFile()->getSize(), 'checksum' => $task->getConfigurationChecksum())
81 );
82
83 // Seems we have no valid processing result
84 } else {
85 $task->setExecuted(FALSE);
86 }
87 } catch (\Exception $e) {
88 $task->setExecuted(FALSE);
89 }
90 }
91
92 /**
93 * @param string $taskName
94 * @return LocalCropScaleMaskHelper|LocalPreviewHelper
95 * @throws \InvalidArgumentException
96 */
97 protected function getHelperByTaskName($taskName) {
98 switch ($taskName) {
99 case 'Preview':
100 $helper = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper::class, $this);
101 break;
102 case 'CropScaleMask':
103 $helper = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper::class, $this);
104 break;
105 default:
106 throw new \InvalidArgumentException('Cannot find helper for task name: "' . $taskName . '"', 1353401352);
107 }
108
109 return $helper;
110 }
111
112 /**
113 * Creates error image based on gfx/notfound_thumb.png
114 * Requires GD lib enabled, otherwise it will exit with the three
115 * textstrings outputted as text. Outputs the image stream to browser and exits!
116 *
117 * @param string $filename Name of the file
118 * @param string $textline1 Text line 1
119 * @param string $textline2 Text line 2
120 * @param string $textline3 Text line 3
121 * @return void
122 * @throws \RuntimeException
123 *
124 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8. Use \TYPO3\CMS\Core\Imaging\GraphicalFunctions::getTemporaryImageWithText() instead.
125 */
126 public function getTemporaryImageWithText($filename, $textline1, $textline2, $textline3) {
127 GeneralUtility::logDeprecatedFunction();
128 $graphicalFunctions = $this->getGraphicalFunctionsObject();
129 $graphicalFunctions->getTemporaryImageWithText($filename, $textline1, $textline2, $textline3);
130 }
131
132 /**
133 * @return \TYPO3\CMS\Core\Imaging\GraphicalFunctions
134 */
135 protected function getGraphicalFunctionsObject() {
136 static $graphicalFunctionsObject = NULL;
137
138 if ($graphicalFunctionsObject === NULL) {
139 $graphicalFunctionsObject = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Imaging\GraphicalFunctions::class);
140 }
141
142 return $graphicalFunctionsObject;
143 }
144
145 }