2 namespace TYPO3\CMS\Core\
Resource\Service
;
4 /***************************************************************
7 * (c) 2012-2013 Oliver Hader <oliver.hader@typo3.org>
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.
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 text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
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.
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
30 use TYPO3\CMS\Core\
Resource;
31 use TYPO3\CMS\Core\Utility
;
34 * File processing service
36 * @author Oliver Hader <oliver.hader@typo3.org>
38 class FileProcessingService
{
41 * @var Resource\ResourceStorage
46 * @var Resource\Driver\DriverInterface
51 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
53 protected $signalSlotDispatcher;
56 * @var \TYPO3\CMS\Core\Log\Logger
60 const SIGNAL_PreFileProcess
= 'preFileProcess';
61 const SIGNAL_PostFileProcess
= 'postFileProcess';
64 * Creates this object.
66 * @param Resource\ResourceStorage $storage
67 * @param Resource\Driver\DriverInterface $driver
69 public function __construct(Resource\ResourceStorage
$storage, Resource\Driver\DriverInterface
$driver) {
70 $this->storage
= $storage;
71 $this->driver
= $driver;
73 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
74 $logManager = Utility\GeneralUtility
::makeInstance('TYPO3\CMS\Core\Log\LogManager');
75 $this->logger
= $logManager->getLogger(__CLASS__
);
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
86 * @return Resource\ProcessedFile
87 * @throws \InvalidArgumentException
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');
93 $processedFile = $processedFileRepository->findOneByOriginalFileAndTaskTypeAndConfiguration($fileObject, $taskType, $configuration);
95 // set the storage of the processed file
96 // Pre-process the file
97 $this->emitPreFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
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);
106 // Post-process (enrich) the file
107 $this->emitPostFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
109 return $processedFile;
115 * @param Resource\ProcessedFile $processedFile
116 * @param Resource\ResourceStorage $targetStorage The storage to put the processed file into
118 * @throws \RuntimeException
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);
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()) {
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);
137 if ($processedFile->isProcessed()) {
138 /** @var $processedFileRepository Resource\ProcessedFileRepository */
139 $processedFileRepository = Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Resource\\ProcessedFileRepository');
140 $processedFileRepository->add($processedFile);
146 * Get the SignalSlot dispatcher
148 * @return \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
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');
155 return $this->signalSlotDispatcher
;
159 * Emits file pre-processing signal.
161 * @param Resource\ProcessedFile $processedFile
162 * @param Resource\FileInterface $file
163 * @param string $context
164 * @param array $configuration
166 protected function emitPreFileProcessSignal(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));
171 * Emits file post-processing signal.
173 * @param Resource\ProcessedFile $processedFile
174 * @param Resource\FileInterface $file
176 * @param array $configuration
178 protected function emitPostFileProcessSignal(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));