‪TYPO3CMS  9.5
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 
22 
27 {
31  protected ‪$storage;
32 
36  protected ‪$driver;
37 
42 
43  const ‪SIGNAL_PreFileProcess = 'preFileProcess';
44  const ‪SIGNAL_PostFileProcess = 'postFileProcess';
45 
52  public function ‪__construct(Resource\‪ResourceStorage ‪$storage, Resource\Driver\DriverInterface ‪$driver)
53  {
54  $this->storage = ‪$storage;
55  $this->driver = ‪$driver;
56  }
57 
69  public function ‪processFile(Resource\‪FileInterface $fileObject, Resource\‪ResourceStorage $targetStorage, $taskType, $configuration)
70  {
71  // Enforce default configuration for preview processing here,
72  // to be sure we find already processed files below,
73  // which we wouldn't if we would change the configuration later, as configuration is part of the lookup.
74  if ($taskType === Resource\‪ProcessedFile::CONTEXT_IMAGEPREVIEW) {
76  }
77  // Ensure that the processing configuration which is part of the hash sum is properly cast, so
78  // unnecessary duplicate images are not produced, see #80942
79  foreach ($configuration as &$value) {
81  $value = (int)$value;
82  }
83  }
84 
86  $processedFileRepository = GeneralUtility::makeInstance(Resource\ProcessedFileRepository::class);
87 
88  $processedFile = $processedFileRepository->findOneByOriginalFileAndTaskTypeAndConfiguration($fileObject, $taskType, $configuration);
89 
90  // set the storage of the processed file
91  // Pre-process the file
92  $this->‪emitPreFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
93 
94  // Only handle the file if it is not processed yet
95  // (maybe modified or already processed by a signal)
96  // or (in case of preview images) already in the DB/in the processing folder
97  if (!$processedFile->isProcessed()) {
98  $this->‪process($processedFile, $targetStorage);
99  }
100 
101  // Post-process (enrich) the file
102  $this->‪emitPostFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
103 
104  return $processedFile;
105  }
106 
113  protected function ‪process(Resource\ProcessedFile $processedFile, Resource\ResourceStorage $targetStorage)
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(Resource\Processing\LocalImageProcessor::class);
123  $processor->processTask($task);
124 
125  if ($task->isExecuted() && $task->isSuccessful() && $processedFile->isProcessed()) {
127  $processedFileRepository = GeneralUtility::makeInstance(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(ObjectManager::class)->get(Dispatcher::class);
142  }
144  }
145 
154  protected function ‪emitPreFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = [])
155  {
157  Resource\ResourceStorage::class,
158  self::SIGNAL_PreFileProcess,
159  [$this, $this->driver, $processedFile, $file, $context, $configuration]
160  );
161  }
162 
171  protected function ‪emitPostFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = [])
172  {
174  Resource\ResourceStorage::class,
175  self::SIGNAL_PostFileProcess,
176  [$this, $this->driver, $processedFile, $file, $context, $configuration]
177  );
178  }
179 }
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGEPREVIEW
‪const CONTEXT_IMAGEPREVIEW
Definition: ProcessedFile.php:50
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\process
‪process(Resource\ProcessedFile $processedFile, Resource\ResourceStorage $targetStorage)
Definition: FileProcessingService.php:110
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:21
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\$signalSlotDispatcher
‪Dispatcher $signalSlotDispatcher
Definition: FileProcessingService.php:38
‪TYPO3\CMS\Core\Resource\Driver\DriverInterface
Definition: DriverInterface.php:22
‪TYPO3\CMS\Core\Resource\Service
Definition: FileProcessingService.php:2
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\getSignalSlotDispatcher
‪Dispatcher getSignalSlotDispatcher()
Definition: FileProcessingService.php:135
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\SIGNAL_PostFileProcess
‪const SIGNAL_PostFileProcess
Definition: FileProcessingService.php:41
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\emitPreFileProcessSignal
‪emitPreFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration=[])
Definition: FileProcessingService.php:151
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\emitPostFileProcessSignal
‪emitPostFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration=[])
Definition: FileProcessingService.php:168
‪TYPO3\CMS\Extbase\SignalSlot\Dispatcher\dispatch
‪mixed dispatch($signalClassName, $signalName, array $signalArguments=[])
Definition: Dispatcher.php:115
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\processFile
‪Resource ProcessedFile processFile(Resource\FileInterface $fileObject, Resource\ResourceStorage $targetStorage, $taskType, $configuration)
Definition: FileProcessingService.php:66
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\preProcessConfiguration
‪static array preProcessConfiguration(array $configuration)
Definition: LocalPreviewHelper.php:57
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:37
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:42
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\SIGNAL_PreFileProcess
‪const SIGNAL_PreFileProcess
Definition: FileProcessingService.php:40
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\__construct
‪__construct(Resource\ResourceStorage $storage, Resource\Driver\DriverInterface $driver)
Definition: FileProcessingService.php:49
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:74
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService
Definition: FileProcessingService.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\$driver
‪Resource Driver DriverInterface $driver
Definition: FileProcessingService.php:34
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:25
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\$storage
‪Resource ResourceStorage $storage
Definition: FileProcessingService.php:30
‪TYPO3\CMS\Extbase\SignalSlot\Dispatcher
Definition: Dispatcher.php:28