[BUGFIX] Allow processed folders in different storage
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Service / FileProcessingService.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Service;
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\Resource;
18 use TYPO3\CMS\Core\Utility;
19
20 /**
21 * File processing service
22 *
23 * @author Oliver Hader <oliver.hader@typo3.org>
24 */
25 class FileProcessingService {
26
27 /**
28 * @var Resource\ResourceStorage
29 */
30 protected $storage;
31
32 /**
33 * @var Resource\Driver\DriverInterface
34 */
35 protected $driver;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
39 */
40 protected $signalSlotDispatcher;
41
42 /**
43 * @var \TYPO3\CMS\Core\Log\Logger
44 */
45 protected $logger;
46
47 const SIGNAL_PreFileProcess = 'preFileProcess';
48 const SIGNAL_PostFileProcess = 'postFileProcess';
49
50 /**
51 * Creates this object.
52 *
53 * @param Resource\ResourceStorage $storage
54 * @param Resource\Driver\DriverInterface $driver
55 */
56 public function __construct(Resource\ResourceStorage $storage, Resource\Driver\DriverInterface $driver) {
57 $this->storage = $storage;
58 $this->driver = $driver;
59
60 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
61 $logManager = Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class);
62 $this->logger = $logManager->getLogger(__CLASS__);
63 }
64
65 /**
66 * Processes a file
67 *
68 * @param Resource\FileInterface $fileObject The file object
69 * @param Resource\ResourceStorage $targetStorage The storage to store the processed file in
70 * @param string $taskType
71 * @param array $configuration
72 *
73 * @return Resource\ProcessedFile
74 * @throws \InvalidArgumentException
75 */
76 public function processFile(Resource\FileInterface $fileObject, Resource\ResourceStorage $targetStorage, $taskType, $configuration) {
77 /** @var $processedFileRepository Resource\ProcessedFileRepository */
78 $processedFileRepository = Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ProcessedFileRepository::class);
79
80 $processedFile = $processedFileRepository->findOneByOriginalFileAndTaskTypeAndConfiguration($fileObject, $taskType, $configuration);
81
82 // set the storage of the processed file
83 // Pre-process the file
84 $this->emitPreFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
85
86 // Only handle the file if it is not processed yet
87 // (maybe modified or already processed by a signal)
88 // or (in case of preview images) already in the DB/in the processing folder
89 if (!$processedFile->isProcessed()) {
90 $this->process($processedFile, $targetStorage);
91 }
92
93 // Post-process (enrich) the file
94 $this->emitPostFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
95
96 return $processedFile;
97 }
98
99 /**
100 * Processes the file
101 *
102 * @param Resource\ProcessedFile $processedFile
103 * @param Resource\ResourceStorage $targetStorage The storage to put the processed file into
104 */
105 protected function process(Resource\ProcessedFile $processedFile, Resource\ResourceStorage $targetStorage) {
106
107 // We only have to trigger the file processing if the file either is new, does not exist or the
108 // original file has changed since the last processing run (the last case has to trigger a reprocessing
109 // even if the original file was used until now)
110 if ($processedFile->isNew() || (!$processedFile->usesOriginalFile() && !$processedFile->exists()) ||
111 $processedFile->isOutdated()) {
112
113 $task = $processedFile->getTask();
114 /** @var $processor Resource\Processing\LocalImageProcessor */
115 $processor = Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor::class);
116 $processor->processTask($task);
117
118 if ($processedFile->isProcessed()) {
119 /** @var $processedFileRepository Resource\ProcessedFileRepository */
120 $processedFileRepository = Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ProcessedFileRepository::class);
121 $processedFileRepository->add($processedFile);
122 }
123 }
124 }
125
126 /**
127 * Get the SignalSlot dispatcher
128 *
129 * @return \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
130 */
131 protected function getSignalSlotDispatcher() {
132 if (!isset($this->signalSlotDispatcher)) {
133 $this->signalSlotDispatcher = Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class)
134 ->get(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
135 }
136 return $this->signalSlotDispatcher;
137 }
138
139 /**
140 * Emits file pre-processing signal.
141 *
142 * @param Resource\ProcessedFile $processedFile
143 * @param Resource\FileInterface $file
144 * @param string $context
145 * @param array $configuration
146 */
147 protected function emitPreFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = array()) {
148 $this->getSignalSlotDispatcher()->dispatch(\TYPO3\CMS\Core\Resource\ResourceStorage::class, self::SIGNAL_PreFileProcess, array($this, $this->driver, $processedFile, $file, $context, $configuration));
149 }
150
151 /**
152 * Emits file post-processing signal.
153 *
154 * @param Resource\ProcessedFile $processedFile
155 * @param Resource\FileInterface $file
156 * @param $context
157 * @param array $configuration
158 */
159 protected function emitPostFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = array()) {
160 $this->getSignalSlotDispatcher()->dispatch(\TYPO3\CMS\Core\Resource\ResourceStorage::class, self::SIGNAL_PostFileProcess, array($this, $this->driver, $processedFile, $file, $context, $configuration));
161 }
162
163 }