‪TYPO3CMS  ‪main
FileProcessingService.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\EventDispatcher\EventDispatcherInterface;
21 use TYPO3\CMS\Core\Resource\Driver\DriverInterface;
31 
46 {
47  public function ‪__construct(
48  protected readonly EventDispatcherInterface $eventDispatcher,
49  protected readonly ‪ProcessedFileRepository $processedFileRepository,
50  protected readonly ‪ProcessorRegistry $processorRegistry,
51  ) {}
52 
53  public function ‪processFile(‪File|‪FileReference $fileObject, string $taskType, DriverInterface $driver, array $configuration): ‪ProcessedFile
54  {
55  // Processing always works on the original file
56  $originalFile = $fileObject instanceof ‪FileReference ? $fileObject->getOriginalFile() : $fileObject;
57 
58  // Find an entry in the DB or create a new ProcessedFile which can then be added (see ->add below)
59  $processedFile = $this->processedFileRepository->findOneByOriginalFileAndTaskTypeAndConfiguration($originalFile, $taskType, $configuration);
60 
61  // Pre-process the file
62  $event = $this->eventDispatcher->dispatch(
63  new ‪BeforeFileProcessingEvent($driver, $processedFile, $fileObject, $taskType, $configuration)
64  );
65  $processedFile = $event->getProcessedFile();
66  $task = $processedFile->getTask();
67 
68  // Only handle the file if it is not processed yet
69  // (maybe modified or already processed by an event)
70  // or (in case of preview images) already in the DB/in the processing folder
71  if ($task->fileNeedsProcessing()) {
72  $this->‪getProcessorByTask($task)->processTask($task);
73  if ($task->isExecuted() && $task->isSuccessful() && $processedFile->isProcessed()) {
74  $this->processedFileRepository->add($processedFile);
75  }
76  }
77 
78  // Post-process (enrich) the file
79  $event = $this->eventDispatcher->dispatch(
80  new ‪AfterFileProcessingEvent($driver, $processedFile, $fileObject, $taskType, $configuration)
81  );
82 
83  return $event->getProcessedFile();
84  }
85 
87  {
88  return $this->processorRegistry->getProcessorByTask($task);
89  }
90 }
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:39
‪TYPO3\CMS\Core\Resource\Event\AfterFileProcessingEvent
Definition: AfterFileProcessingEvent.php:30
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:34
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:37
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\__construct
‪__construct(protected readonly EventDispatcherInterface $eventDispatcher, protected readonly ProcessedFileRepository $processedFileRepository, protected readonly ProcessorRegistry $processorRegistry,)
Definition: FileProcessingService.php:47
‪TYPO3\CMS\Core\Resource\Event\BeforeFileProcessingEvent
Definition: BeforeFileProcessingEvent.php:30
‪TYPO3\CMS\Core\Resource\Service
Definition: ConfigurationService.php:18
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\processFile
‪processFile(File|FileReference $fileObject, string $taskType, DriverInterface $driver, array $configuration)
Definition: FileProcessingService.php:53
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService\getProcessorByTask
‪getProcessorByTask(TaskInterface $task)
Definition: FileProcessingService.php:86
‪TYPO3\CMS\Core\Resource\Processing\ProcessorRegistry
Definition: ProcessorRegistry.php:27
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService
Definition: FileProcessingService.php:46
‪TYPO3\CMS\Core\Resource\Processing\ProcessorInterface
Definition: ProcessorInterface.php:22