‪TYPO3CMS  10.4
FileProcessingService.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\EventDispatcher\EventDispatcherInterface;
33 
38 {
42  protected ‪$storage;
43 
47  protected ‪$driver;
48 
52  protected ‪$eventDispatcher;
53 
57  const ‪SIGNAL_PreFileProcess = 'preFileProcess';
58 
62  const ‪SIGNAL_PostFileProcess = 'postFileProcess';
63 
71  public function ‪__construct(‪ResourceStorage ‪$storage, ‪DriverInterface ‪$driver, EventDispatcherInterface ‪$eventDispatcher = null)
72  {
73  $this->storage = ‪$storage;
74  $this->driver = ‪$driver;
75  $this->eventDispatcher = ‪$eventDispatcher ?? GeneralUtility::getContainer()->get(EventDispatcherInterface::class);
76  }
77 
89  public function ‪processFile(‪FileInterface $fileObject, ‪ResourceStorage $targetStorage, $taskType, $configuration)
90  {
91  // Enforce default configuration for preview processing here,
92  // to be sure we find already processed files below,
93  // which we wouldn't if we would change the configuration later, as configuration is part of the lookup.
94  if ($taskType === ‪ProcessedFile::CONTEXT_IMAGEPREVIEW) {
95  $configuration = ‪LocalPreviewHelper::preProcessConfiguration($configuration);
96  }
97  // Ensure that the processing configuration which is part of the hash sum is properly cast, so
98  // unnecessary duplicate images are not produced, see #80942
99  foreach ($configuration as &$value) {
101  $value = (int)$value;
102  }
103  }
104 
106  $processedFileRepository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
107 
108  $processedFile = $processedFileRepository->findOneByOriginalFileAndTaskTypeAndConfiguration($fileObject, $taskType, $configuration);
109 
110  // set the storage of the processed file
111  // Pre-process the file
113  $event = $this->eventDispatcher->dispatch(
114  new ‪BeforeFileProcessingEvent($this->driver, $processedFile, $fileObject, $taskType, $configuration)
115  );
116  $processedFile = $event->getProcessedFile();
117 
118  // Only handle the file if it is not processed yet
119  // (maybe modified or already processed by a signal)
120  // or (in case of preview images) already in the DB/in the processing folder
121  if (!$processedFile->isProcessed()) {
122  $this->‪process($processedFile, $targetStorage);
123  }
124 
125  // Post-process (enrich) the file
127  $event = $this->eventDispatcher->dispatch(
128  new ‪AfterFileProcessingEvent($this->driver, $processedFile, $fileObject, $taskType, $configuration)
129  );
130 
131  return $event->getProcessedFile();
132  }
133 
140  protected function ‪process(‪ProcessedFile $processedFile, ‪ResourceStorage $targetStorage)
141  {
142  // We only have to trigger the file processing if the file either is new, does not exist or the
143  // original file has changed since the last processing run (the last case has to trigger a reprocessing
144  // even if the original file was used until now)
145  if ($processedFile->‪isNew() || (!$processedFile->‪usesOriginalFile() && !$processedFile->‪exists()) ||
146  $processedFile->‪isOutdated()) {
147  $task = $processedFile->‪getTask();
148  $processor = $this->‪getProcessorByTask($task);
149  $processor->processTask($task);
150 
151  if ($task->isExecuted() && $task->isSuccessful() && $processedFile->‪isProcessed()) {
153  $processedFileRepository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
154  $processedFileRepository->add($processedFile);
155  }
156  }
157  }
158 
164  {
165  $processorRegistry = GeneralUtility::makeInstance(ProcessorRegistry::class);
166 
167  return $processorRegistry->getProcessorByTask($task);
168  }
169 }
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:30
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGEPREVIEW
‪const CONTEXT_IMAGEPREVIEW
Definition: ProcessedFile.php:52
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper
Definition: LocalPreviewHelper.php:29
‪TYPO3\CMS\Core\Resource\ProcessedFile\usesOriginalFile
‪bool usesOriginalFile()
Definition: ProcessedFile.php:437
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\Resource\Event\AfterFileProcessingEvent
Definition: AfterFileProcessingEvent.php:30
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:33
‪TYPO3\CMS\Core\Resource\Driver\DriverInterface
Definition: DriverInterface.php:23
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\getProcessorByTask
‪Resource Processing ProcessorInterface getProcessorByTask(TaskInterface $task)
Definition: FileProcessingService.php:160
‪TYPO3\CMS\Core\Resource\Event\BeforeFileProcessingEvent
Definition: BeforeFileProcessingEvent.php:30
‪TYPO3\CMS\Core\Resource\ProcessedFile\exists
‪bool exists()
Definition: ProcessedFile.php:275
‪TYPO3\CMS\Core\Resource\Service
Definition: ExtractorService.php:18
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\SIGNAL_PostFileProcess
‪const SIGNAL_PostFileProcess
Definition: FileProcessingService.php:59
‪TYPO3\CMS\Core\Resource\ProcessedFile\isProcessed
‪bool isProcessed()
Definition: ProcessedFile.php:293
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\process
‪process(ProcessedFile $processedFile, ResourceStorage $targetStorage)
Definition: FileProcessingService.php:137
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\__construct
‪__construct(ResourceStorage $storage, DriverInterface $driver, EventDispatcherInterface $eventDispatcher=null)
Definition: FileProcessingService.php:68
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\preProcessConfiguration
‪static array preProcessConfiguration(array $configuration)
Definition: LocalPreviewHelper.php:45
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:44
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\SIGNAL_PreFileProcess
‪const SIGNAL_PreFileProcess
Definition: FileProcessingService.php:54
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Core\Resource\ProcessedFile\getTask
‪Processing TaskInterface getTask()
Definition: ProcessedFile.php:562
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Resource\Processing\ProcessorRegistry
Definition: ProcessorRegistry.php:28
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService
Definition: FileProcessingService.php:38
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\$driver
‪Resource Driver DriverInterface $driver
Definition: FileProcessingService.php:45
‪TYPO3\CMS\Core\Resource\Processing\ProcessorInterface
Definition: ProcessorInterface.php:22
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: FileProcessingService.php:49
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\processFile
‪Resource ProcessedFile processFile(FileInterface $fileObject, ResourceStorage $targetStorage, $taskType, $configuration)
Definition: FileProcessingService.php:86
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\$storage
‪Resource ResourceStorage $storage
Definition: FileProcessingService.php:41
‪TYPO3\CMS\Core\Resource\ProcessedFile\isNew
‪bool isNew()
Definition: ProcessedFile.php:233
‪TYPO3\CMS\Core\Resource\ProcessedFile\isOutdated
‪bool isOutdated()
Definition: ProcessedFile.php:447