TYPO3  7.6
FileProcessingService.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Service;
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 
17 use TYPO3\CMS\Core\Resource;
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  {
78  $processedFileRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ProcessedFileRepository::class);
79 
80  $processedFile = $processedFileRepository->findOneByOriginalFileAndTaskTypeAndConfiguration($fileObject, $taskType, $configuration);
81 
82  // set the storage of the processed file
83  // Pre-process the file
84  $this->emitPreFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
85 
86  // Only handle the file if it is not processed yet
87  // (maybe modified or already processed by a signal)
88  // or (in case of preview images) already in the DB/in the processing folder
89  if (!$processedFile->isProcessed()) {
90  $this->process($processedFile, $targetStorage);
91  }
92 
93  // Post-process (enrich) the file
94  $this->emitPostFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
95 
96  return $processedFile;
97  }
98 
105  protected function process(Resource\ProcessedFile $processedFile, Resource\ResourceStorage $targetStorage)
106  {
107 
108  // We only have to trigger the file processing if the file either is new, does not exist or the
109  // original file has changed since the last processing run (the last case has to trigger a reprocessing
110  // even if the original file was used until now)
111  if ($processedFile->isNew() || (!$processedFile->usesOriginalFile() && !$processedFile->exists()) ||
112  $processedFile->isOutdated()) {
113  $task = $processedFile->getTask();
115  $processor = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor::class);
116  $processor->processTask($task);
117 
118  if ($task->isExecuted() && $task->isSuccessful() && $processedFile->isProcessed()) {
120  $processedFileRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ProcessedFileRepository::class);
121  $processedFileRepository->add($processedFile);
122  }
123  }
124  }
125 
131  protected function getSignalSlotDispatcher()
132  {
133  if (!isset($this->signalSlotDispatcher)) {
134  $this->signalSlotDispatcher = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class)
135  ->get(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
136  }
138  }
139 
148  protected function emitPreFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = array())
149  {
150  $this->getSignalSlotDispatcher()->dispatch(\TYPO3\CMS\Core\Resource\ResourceStorage::class, self::SIGNAL_PreFileProcess, array($this, $this->driver, $processedFile, $file, $context, $configuration));
151  }
152 
161  protected function emitPostFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = array())
162  {
163  $this->getSignalSlotDispatcher()->dispatch(\TYPO3\CMS\Core\Resource\ResourceStorage::class, self::SIGNAL_PostFileProcess, array($this, $this->driver, $processedFile, $file, $context, $configuration));
164  }
165 }