‪TYPO3CMS  ‪main
ImageProcessingService.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 
26 
31 {
32  public function ‪__construct(
33  private readonly ‪ProcessedFileRepository $processedFileRepository,
34  private readonly ‪Context $context,
35  private readonly ‪ResourceMutex $locker,
36  ) {}
37 
38  public function ‪process(int $processedFileId): ‪ProcessedFile
39  {
41  $processedFile = $this->processedFileRepository->findByUid($processedFileId);
42  try {
43  $this->‪validateProcessedFile($processedFile);
44  $hadToWaitForLock = $this->locker->acquireLock(self::class, (string)$processedFileId);
45 
46  if ($hadToWaitForLock) {
47  // Fetch the processed file again, as it might have been processed by
48  // another process while waiting for the lock
50  $processedFile = $this->processedFileRepository->findByUid($processedFileId);
51  $this->‪validateProcessedFile($processedFile);
52  }
53 
54  $this->context->setAspect('fileProcessing', new ‪FileProcessingAspect(false));
55  $processedFile = $processedFile->getOriginalFile()->process(
56  $processedFile->getTaskIdentifier(),
57  $processedFile->getProcessingConfiguration()
58  );
59 
60  $this->‪validateProcessedFile($processedFile);
62  $processedFile = $e->‪getProcessedFile();
63  } finally {
64  $this->locker->releaseLock(self::class);
65  }
66 
67  return $processedFile;
68  }
69 
75  private function ‪validateProcessedFile(‪ProcessedFile $processedFile): void
76  {
77  if ($processedFile->‪isProcessed()) {
78  throw new ‪FileAlreadyProcessedException($processedFile, 1599395651);
79  }
80  }
81 }
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:39
‪TYPO3\CMS\Core\Context\FileProcessingAspect
Definition: FileProcessingAspect.php:29
‪TYPO3\CMS\Core\Locking\ResourceMutex
Definition: ResourceMutex.php:56
‪TYPO3\CMS\Core\Resource\Service\ImageProcessingService\validateProcessedFile
‪validateProcessedFile(ProcessedFile $processedFile)
Definition: ImageProcessingService.php:75
‪TYPO3\CMS\Core\Resource\Service\ImageProcessingService
Definition: ImageProcessingService.php:31
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Core\Resource\Service
Definition: ConfigurationService.php:18
‪TYPO3\CMS\Core\Resource\Exception\FileAlreadyProcessedException\getProcessedFile
‪getProcessedFile()
Definition: FileAlreadyProcessedException.php:40
‪TYPO3\CMS\Core\Resource\Exception\FileAlreadyProcessedException
Definition: FileAlreadyProcessedException.php:29
‪TYPO3\CMS\Core\Resource\ProcessedFile\isProcessed
‪isProcessed()
Definition: ProcessedFile.php:265
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Core\Resource\Service\ImageProcessingService\__construct
‪__construct(private readonly ProcessedFileRepository $processedFileRepository, private readonly Context $context, private readonly ResourceMutex $locker,)
Definition: ImageProcessingService.php:32
‪TYPO3\CMS\Core\Resource\Service\ImageProcessingService\process
‪process(int $processedFileId)
Definition: ImageProcessingService.php:38