TYPO3 CMS  TYPO3_6-2
FileProcessingService.php
Go to the documentation of this file.
1 <?php
3 
19 
26 
30  protected $storage;
31 
35  protected $driver;
36 
41 
45  protected $logger;
46 
47  const SIGNAL_PreFileProcess = 'preFileProcess';
48  const SIGNAL_PostFileProcess = 'postFileProcess';
49 
56  public function __construct(Resource\ResourceStorage $storage, Resource\Driver\DriverInterface $driver) {
57  $this->storage = $storage;
58  $this->driver = $driver;
59 
61  $logManager = Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager');
62  $this->logger = $logManager->getLogger(__CLASS__);
63  }
64 
76  public function processFile(Resource\FileInterface $fileObject, Resource\ResourceStorage $targetStorage, $taskType, $configuration) {
78  $processedFileRepository = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ProcessedFileRepository');
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  // We only have to trigger the file processing if the file either is new, does not exist or the
108  // original file has changed since the last processing run (the last case has to trigger a reprocessing
109  // even if the original file was used until now)
110  if ($processedFile->isNew() || (!$processedFile->usesOriginalFile() && !$processedFile->exists()) ||
111  $processedFile->isOutdated()) {
112 
113  $task = $processedFile->getTask();
115  $processor = Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor');
116  $processor->processTask($task);
117 
118  if ($task->isExecuted() && $task->isSuccessful() && $processedFile->isProcessed()) {
120  $processedFileRepository = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ProcessedFileRepository');
121  $processedFileRepository->add($processedFile);
122  }
123  }
124  }
125 
131  protected function getSignalSlotDispatcher() {
132  if (!isset($this->signalSlotDispatcher)) {
133  $this->signalSlotDispatcher = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager')
134  ->get('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
135  }
137  }
138 
147  protected function emitPreFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = array()) {
148  $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', self::SIGNAL_PreFileProcess, array($this, $this->driver, $processedFile, $file, $context, $configuration));
149  }
150 
159  protected function emitPostFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration = array()) {
160  $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', self::SIGNAL_PostFileProcess, array($this, $this->driver, $processedFile, $file, $context, $configuration));
161  }
162 }
emitPreFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration=array())
emitPostFileProcessSignal(Resource\ProcessedFile $processedFile, Resource\FileInterface $file, $context, array $configuration=array())