[TASK] Merge submodule extbase into core
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Service / FileProcessingService.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Oliver Hader <oliver.hader@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Core\Resource;
31 use TYPO3\CMS\Core\Utility;
32
33 /**
34 * File processing service
35 *
36 * @author Oliver Hader <oliver.hader@typo3.org>
37 */
38 class FileProcessingService {
39
40 /**
41 * @var Resource\ResourceStorage
42 */
43 protected $storage;
44
45 /**
46 * @var Resource\Driver\AbstractDriver
47 */
48 protected $driver;
49
50 /**
51 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
52 */
53 protected $signalSlotDispatcher;
54
55 /**
56 * @var \TYPO3\CMS\Core\Log\Logger
57 */
58 protected $logger;
59
60 const SIGNAL_PreFileProcess = 'preFileProcess';
61 const SIGNAL_PostFileProcess = 'postFileProcess';
62
63 /**
64 * Creates this object.
65 *
66 * @param Resource\ResourceStorage $storage
67 * @param Resource\Driver\AbstractDriver $driver
68 */
69 public function __construct(Resource\ResourceStorage $storage, Resource\Driver\AbstractDriver $driver) {
70 $this->storage = $storage;
71 $this->driver = $driver;
72
73 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
74 $logManager = Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager');
75 $this->logger = $logManager->getLogger(__CLASS__);
76 }
77
78 /**
79 * Processes a file
80 *
81 * @param Resource\FileInterface $fileObject The file object
82 * @param Resource\ResourceStorage $targetStorage The storage to store the processed file in
83 * @param string $taskType
84 * @param array $configuration
85 *
86 * @return Resource\ProcessedFile
87 * @throws \InvalidArgumentException
88 */
89 public function processFile(Resource\FileInterface $fileObject, Resource\ResourceStorage $targetStorage, $taskType, $configuration) {
90 /** @var $processedFileRepository Resource\ProcessedFileRepository */
91 $processedFileRepository = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ProcessedFileRepository');
92
93 $processedFile = $processedFileRepository->findOneByOriginalFileAndTaskTypeAndConfiguration($fileObject, $taskType, $configuration);
94
95 // set the storage of the processed file
96 // Pre-process the file
97 $this->emitPreFileProcess($processedFile, $fileObject, $taskType, $configuration);
98
99 // Only handle the file if it is not processed yet
100 // (maybe modified or already processed by a signal)
101 // or (in case of preview images) already in the DB/in the processing folder
102 if (!$processedFile->isProcessed()) {
103 $this->process($processedFile, $targetStorage);
104 }
105
106 // Post-process (enrich) the file
107 $this->emitPostFileProcess($processedFile, $fileObject, $taskType, $configuration);
108
109 return $processedFile;
110 }
111
112 /**
113 * Processes the file
114 *
115 * @param Resource\ProcessedFile $processedFile
116 * @param Resource\ResourceStorage $targetStorage The storage to put the processed file into
117 *
118 * @throws \RuntimeException
119 */
120 protected function process(Resource\ProcessedFile $processedFile, Resource\ResourceStorage $targetStorage) {
121 $targetFolder = $targetStorage->getProcessingFolder();
122 if (!is_object($targetFolder)) {
123 throw new \RuntimeException('Could not get processing folder for storage ' . $this->storage->getName(), 1350514301);
124 }
125
126 // We only have to trigger the file processing if the file either is new, does not exist or the
127 // original file has changed since the last processing run (the last case has to trigger a reprocessing
128 // even if the original file was used until now)
129 if ($processedFile->isNew() || (!$processedFile->usesOriginalFile() && !$processedFile->exists()) ||
130 $processedFile->isOutdated()) {
131
132 $task = $processedFile->getTask();
133 /** @var $processor Resource\Processing\LocalImageProcessor */
134 $processor = Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor');
135 $processor->processTask($task);
136
137 if ($processedFile->isProcessed()) {
138 /** @var $processedFileRepository Resource\ProcessedFileRepository */
139 $processedFileRepository = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ProcessedFileRepository');
140 $processedFileRepository->add($processedFile);
141 }
142 }
143 }
144
145 /**
146 * Get the SignalSlot dispatcher
147 *
148 * @return \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
149 */
150 protected function getSignalSlotDispatcher() {
151 if (!isset($this->signalSlotDispatcher)) {
152 $this->signalSlotDispatcher = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager')
153 ->get('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
154 }
155 return $this->signalSlotDispatcher;
156 }
157
158 /**
159 * Emits file pre-processing signal.
160 *
161 * @param Resource\ProcessedFile $processedFile
162 * @param Resource\FileInterface $file
163 * @param string $context
164 * @param array $configuration
165 */
166 protected function emitPreFileProcess(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = array()) {
167 $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', self::SIGNAL_PreFileProcess, array($this, $this->driver, $processedFile, $file, $context, $configuration));
168 }
169
170 /**
171 * Emits file post-processing signal.
172 *
173 * @param Resource\ProcessedFile $processedFile
174 * @param Resource\FileInterface $file
175 * @param $context
176 * @param array $configuration
177 */
178 protected function emitPostFileProcess(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = array()) {
179 $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', self::SIGNAL_PostFileProcess, array($this, $this->driver, $processedFile, $file, $context, $configuration));
180 }
181 }
182
183
184 ?>