TYPO3 CMS  TYPO3_8-7
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 
46  protected $logger;
47 
48  const SIGNAL_PreFileProcess = 'preFileProcess';
49  const SIGNAL_PostFileProcess = 'postFileProcess';
50 
57  public function __construct(Resource\ResourceStorage $storage, Resource\Driver\DriverInterface $driver)
58  {
59  $this->storage = $storage;
60  $this->driver = $driver;
61 
63  $logManager = GeneralUtility::makeInstance(LogManager::class);
64  $this->logger = $logManager->getLogger(__CLASS__);
65  }
66 
78  public function processFile(Resource\FileInterface $fileObject, Resource\ResourceStorage $targetStorage, $taskType, $configuration)
79  {
80  // Enforce default configuration for preview processing here,
81  // to be sure we find already processed files below,
82  // which we wouldn't if we would change the configuration later, as configuration is part of the lookup.
83  if ($taskType === Resource\ProcessedFile::CONTEXT_IMAGEPREVIEW) {
85  }
86 
88  $processedFileRepository = GeneralUtility::makeInstance(Resource\ProcessedFileRepository::class);
89 
90  $processedFile = $processedFileRepository->findOneByOriginalFileAndTaskTypeAndConfiguration($fileObject, $taskType, $configuration);
91 
92  // set the storage of the processed file
93  // Pre-process the file
94  $this->emitPreFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
95 
96  // Only handle the file if it is not processed yet
97  // (maybe modified or already processed by a signal)
98  // or (in case of preview images) already in the DB/in the processing folder
99  if (!$processedFile->isProcessed()) {
100  $this->process($processedFile, $targetStorage);
101  }
102 
103  // Post-process (enrich) the file
104  $this->emitPostFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
105 
106  return $processedFile;
107  }
108 
115  protected function process(Resource\ProcessedFile $processedFile, Resource\ResourceStorage $targetStorage)
116  {
117  // We only have to trigger the file processing if the file either is new, does not exist or the
118  // original file has changed since the last processing run (the last case has to trigger a reprocessing
119  // even if the original file was used until now)
120  if ($processedFile->isNew() || (!$processedFile->usesOriginalFile() && !$processedFile->exists()) ||
121  $processedFile->isOutdated()) {
122  $task = $processedFile->getTask();
124  $processor = GeneralUtility::makeInstance(Resource\Processing\LocalImageProcessor::class);
125  $processor->processTask($task);
126 
127  if ($task->isExecuted() && $task->isSuccessful() && $processedFile->isProcessed()) {
129  $processedFileRepository = GeneralUtility::makeInstance(Resource\ProcessedFileRepository::class);
130  $processedFileRepository->add($processedFile);
131  }
132  }
133  }
134 
140  protected function getSignalSlotDispatcher()
141  {
142  if (!isset($this->signalSlotDispatcher)) {
143  $this->signalSlotDispatcher = GeneralUtility::makeInstance(ObjectManager::class)->get(Dispatcher::class);
144  }
146  }
147 
156  protected function emitPreFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = [])
157  {
158  $this->getSignalSlotDispatcher()->dispatch(
159  Resource\ResourceStorage::class,
160  self::SIGNAL_PreFileProcess,
161  [$this, $this->driver, $processedFile, $file, $context, $configuration]
162  );
163  }
164 
173  protected function emitPostFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = [])
174  {
175  $this->getSignalSlotDispatcher()->dispatch(
176  Resource\ResourceStorage::class,
177  self::SIGNAL_PostFileProcess,
178  [$this, $this->driver, $processedFile, $file, $context, $configuration]
179  );
180  }
181 }
emitPreFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration=[])
static makeInstance($className,... $constructorArguments)
emitPostFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration=[])