‪TYPO3CMS  ‪main
LocalImageProcessor.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
22 
26 class ‪LocalImageProcessor implements ‪ProcessorInterface, LoggerAwareInterface
27 {
28  use LoggerAwareTrait;
29 
33  public function ‪canProcessTask(‪TaskInterface $task): bool
34  {
35  return $task->‪getType() === 'Image'
36  && in_array($task->‪getName(), ['Preview', 'CropScaleMask'], true);
37  }
38 
44  public function ‪processTask(‪TaskInterface $task): void
45  {
46  if ($this->‪checkForExistingTargetFile($task)) {
47  return;
48  }
49  $this->‪processTaskWithLocalFile($task, null);
50  }
51 
57  public function ‪processTaskWithLocalFile(‪TaskInterface $task, ?string $localFile): void
58  {
59  $helper = $this->‪getHelperByTaskName($task->‪getName());
60  try {
61  if ($localFile === null) {
62  $result = $helper->process($task);
63  } else {
64  $result = $helper->processWithLocalFile($task, $localFile);
65  }
66  if ($result === null) {
67  $task->‪setExecuted(true);
69  } elseif (!empty($result['filePath']) && file_exists($result['filePath'])) {
70  $task->‪setExecuted(true);
71  $imageInformation = GeneralUtility::makeInstance(ImageInfo::class, $result['filePath']);
74  'width' => $imageInformation->getWidth(),
75  'height' => $imageInformation->getHeight(),
76  'size' => $imageInformation->getSize(),
77  'checksum' => $task->‪getConfigurationChecksum(),
78  ]);
79  $task->‪getTargetFile()->‪updateWithLocalFile($result['filePath']);
80  } else {
81  // Seems we have no valid processing result
82  $task->‪setExecuted(false);
83  }
84  } catch (\‪Exception $e) {
85  // @todo: Swallowing all exceptions including PHP warnings here is a bad idea.
86  // @todo: This should be restricted to more specific exceptions - if at all.
87  // @todo: For now, we at least log the situation.
88  $this->logger->error(sprintf('Processing task of image file'), ['exception' => $e]);
89  $task->‪setExecuted(false);
90  }
91  }
92 
100  {
101  // the storage of the processed file, not of the original file!
102  $storage = $task->‪getTargetFile()->‪getStorage();
103  $processingFolder = $storage->getProcessingFolder($task->‪getSourceFile());
104 
105  // explicitly check for the raw filename here, as we check for files that existed before we even started
106  // processing, i.e. that were processed earlier
107  if ($processingFolder->hasFile($task->‪getTargetFileName())) {
108  // When the processed file already exists set it as processed file
110 
111  // If the processed file is stored on a remote server, we must fetch a local copy of the file, as we
112  // have no API for fetching file metadata from a remote file.
113  $localProcessedFile = $storage->getFileForLocalProcessing($task->‪getTargetFile(), false);
114  $task->‪setExecuted(true);
115  $imageInformation = GeneralUtility::makeInstance(ImageInfo::class, $localProcessedFile);
116  $properties = [
117  'width' => $imageInformation->getWidth(),
118  'height' => $imageInformation->getHeight(),
119  'size' => $imageInformation->getSize(),
120  'checksum' => $task->‪getConfigurationChecksum(),
121  ];
122  $task->‪getTargetFile()->‪updateProperties($properties);
123 
124  return true;
125  }
126  return false;
127  }
128 
134  protected function ‪getHelperByTaskName($taskName)
135  {
136  switch ($taskName) {
137  case 'Preview':
138  $helper = GeneralUtility::makeInstance(LocalPreviewHelper::class);
139  break;
140  case 'CropScaleMask':
141  $helper = GeneralUtility::makeInstance(LocalCropScaleMaskHelper::class);
142  break;
143  default:
144  throw new \InvalidArgumentException('Cannot find helper for task name: "' . $taskName . '"', 1353401352);
145  }
146 
147  return $helper;
148  }
149 }
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper
Definition: LocalPreviewHelper.php:28
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getType
‪string getType()
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:33
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\processTaskWithLocalFile
‪processTaskWithLocalFile(TaskInterface $task, ?string $localFile)
Definition: LocalImageProcessor.php:57
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getSourceFile
‪Resource File getSourceFile()
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper
Definition: LocalCropScaleMaskHelper.php:28
‪TYPO3\CMS\Core\Resource\ProcessedFile\setName
‪setName($name)
Definition: ProcessedFile.php:252
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\checkForExistingTargetFile
‪bool checkForExistingTargetFile(TaskInterface $task)
Definition: LocalImageProcessor.php:99
‪TYPO3\CMS\Core\Resource\ProcessedFile\setUsesOriginalFile
‪setUsesOriginalFile()
Definition: ProcessedFile.php:415
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractGraphicalTask.php:16
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\getHelperByTaskName
‪LocalCropScaleMaskHelper LocalPreviewHelper getHelperByTaskName($taskName)
Definition: LocalImageProcessor.php:134
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor
Definition: LocalImageProcessor.php:27
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFile
‪Resource ProcessedFile getTargetFile()
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪int< 0, getSize():int { if( $this->deleted) { throw new \RuntimeException( 'File has been deleted.', 1329821480);} if(empty( $this->properties[ 'size'])) { $fileInfo=$this-> getStorage() -> getFileInfoByIdentifier($this->getIdentifier(), ['size'])
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getName
‪string getName()
‪TYPO3\CMS\Core\Type\File\ImageInfo
Definition: ImageInfo.php:28
‪TYPO3\CMS\Core\Resource\ProcessedFile\updateWithLocalFile
‪updateWithLocalFile($filePath)
Definition: ProcessedFile.php:184
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\canProcessTask
‪canProcessTask(TaskInterface $task)
Definition: LocalImageProcessor.php:33
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\processTask
‪processTask(TaskInterface $task)
Definition: LocalImageProcessor.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Resource\ProcessedFile\updateProperties
‪updateProperties(array $properties)
Definition: ProcessedFile.php:341
‪TYPO3\CMS\Core\Resource\Processing\ProcessorInterface
Definition: ProcessorInterface.php:22
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\setExecuted
‪setExecuted($successful)
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getConfigurationChecksum
‪string getConfigurationChecksum()
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFileName
‪string getTargetFileName()