TYPO3 CMS  TYPO3_7-6
FileProcessingService.php
Go to the documentation of this file.
1 <?php
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 
19 
24 {
28  protected $storage;
29 
33  protected $driver;
34 
39 
43  protected $logger;
44 
45  const SIGNAL_PreFileProcess = 'preFileProcess';
46  const SIGNAL_PostFileProcess = 'postFileProcess';
47 
54  public function __construct(Resource\ResourceStorage $storage, Resource\Driver\DriverInterface $driver)
55  {
56  $this->storage = $storage;
57  $this->driver = $driver;
58 
60  $logManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class);
61  $this->logger = $logManager->getLogger(__CLASS__);
62  }
63 
75  public function processFile(Resource\FileInterface $fileObject, Resource\ResourceStorage $targetStorage, $taskType, $configuration)
76  {
77  // Enforce default configuration for preview processing here,
78  // to be sure we find already processed files below,
79  // which we wouldn't if we would change the configuration later, as configuration is part of the lookup.
80  if ($taskType === Resource\ProcessedFile::CONTEXT_IMAGEPREVIEW) {
82  }
83 
85  $processedFileRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ProcessedFileRepository::class);
86 
87  $processedFile = $processedFileRepository->findOneByOriginalFileAndTaskTypeAndConfiguration($fileObject, $taskType, $configuration);
88 
89  // set the storage of the processed file
90  // Pre-process the file
91  $this->emitPreFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
92 
93  // Only handle the file if it is not processed yet
94  // (maybe modified or already processed by a signal)
95  // or (in case of preview images) already in the DB/in the processing folder
96  if (!$processedFile->isProcessed()) {
97  $this->process($processedFile, $targetStorage);
98  }
99 
100  // Post-process (enrich) the file
101  $this->emitPostFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
102 
103  return $processedFile;
104  }
105 
112  protected function process(Resource\ProcessedFile $processedFile, Resource\ResourceStorage $targetStorage)
113  {
114 
115  // We only have to trigger the file processing if the file either is new, does not exist or the
116  // original file has changed since the last processing run (the last case has to trigger a reprocessing
117  // even if the original file was used until now)
118  if ($processedFile->isNew() || (!$processedFile->usesOriginalFile() && !$processedFile->exists()) ||
119  $processedFile->isOutdated()) {
120  $task = $processedFile->getTask();
122  $processor = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor::class);
123  $processor->processTask($task);
124 
125  if ($task->isExecuted() && $task->isSuccessful() && $processedFile->isProcessed()) {
127  $processedFileRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ProcessedFileRepository::class);
128  $processedFileRepository->add($processedFile);
129  }
130  }
131  }
132 
138  protected function getSignalSlotDispatcher()
139  {
140  if (!isset($this->signalSlotDispatcher)) {
141  $this->signalSlotDispatcher = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class)
142  ->get(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
143  }
145  }
146 
155  protected function emitPreFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = [])
156  {
157  $this->getSignalSlotDispatcher()->dispatch(\TYPO3\CMS\Core\Resource\ResourceStorage::class, self::SIGNAL_PreFileProcess, [$this, $this->driver, $processedFile, $file, $context, $configuration]);
158  }
159 
168  protected function emitPostFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = [])
169  {
170  $this->getSignalSlotDispatcher()->dispatch(\TYPO3\CMS\Core\Resource\ResourceStorage::class, self::SIGNAL_PostFileProcess, [$this, $this->driver, $processedFile, $file, $context, $configuration]);
171  }
172 }
emitPreFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration=[])
emitPostFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration=[])