‪TYPO3CMS  ‪main
ExtractorService.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 
24 
29 {
33  private ‪$extractionServices;
34 
35  public function ‪extractMetaData(‪File $fileObject): array
36  {
37  $newMetaData = $extractedMetaData = [];
38  // Loop through available extractors and fetch metadata for the given file.
39  foreach ($this->‪getExtractionServices($fileObject->‪getStorage()->getDriverType()) as $extractorService) {
40  if ($this->‪isFileTypeSupportedByExtractor($fileObject, $extractorService)
41  && $extractorService->canProcess($fileObject)
42  ) {
43  $metaDataFromExtractor = $extractorService->extractMetaData($fileObject, $extractedMetaData);
44  if (!empty($metaDataFromExtractor)) {
45  $extractedMetaData[] = $metaDataFromExtractor;
46  $newMetaData[$extractorService->getPriority()][] = $metaDataFromExtractor;
47  }
48  }
49  }
50  // Sort metadata by priority so that merging happens in order of precedence.
51  ksort($newMetaData);
52  // Merge the collected metadata.
53  $metaData = [[]];
54  foreach ($newMetaData as $dataFromExtractors) {
55  foreach ($dataFromExtractors as $data) {
56  $metaData[] = $data;
57  }
58  }
59  return array_filter(array_merge(...$metaData));
60  }
61 
67  protected function ‪getExtractionServices(string $driverType): array
68  {
69  if (empty($this->extractionServices[$driverType])) {
70  $this->extractionServices[$driverType] = $this->‪getExtractorRegistry()->getExtractorsWithDriverSupport($driverType);
71  }
72  return $this->extractionServices[$driverType];
73  }
74 
78  private function ‪isFileTypeSupportedByExtractor(File $file, ExtractorInterface $extractor): bool
79  {
80  $isSupported = true;
81  $fileTypeRestrictions = $extractor->getFileTypeRestrictions();
82  if (!empty($fileTypeRestrictions) && !in_array($file->getType(), $fileTypeRestrictions, true)) {
83  $isSupported = false;
84  }
85  return $isSupported;
86  }
87 
91  protected function ‪getExtractorRegistry(): ExtractorRegistry
92  {
93  return GeneralUtility::makeInstance(ExtractorRegistry::class);
94  }
95 }
‪TYPO3\CMS\Core\Resource\Index\ExtractorInterface\getFileTypeRestrictions
‪array getFileTypeRestrictions()
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:281
‪TYPO3\CMS\Core\Resource\Service\ExtractorService\getExtractionServices
‪ExtractorInterface[] getExtractionServices(string $driverType)
Definition: ExtractorService.php:66
‪TYPO3\CMS\Core\Resource\Index\ExtractorInterface
Definition: ExtractorInterface.php:25
‪TYPO3\CMS\Core\Resource\Index\ExtractorRegistry
Definition: ExtractorRegistry.php:25
‪TYPO3\CMS\Core\Resource\Service\ExtractorService\getExtractorRegistry
‪getExtractorRegistry()
Definition: ExtractorService.php:90
‪TYPO3\CMS\Core\Resource\Service\ExtractorService\$extractionServices
‪ExtractorInterface[][] $extractionServices
Definition: ExtractorService.php:32
‪TYPO3\CMS\Core\Resource\Service\ExtractorService\isFileTypeSupportedByExtractor
‪isFileTypeSupportedByExtractor(File $file, ExtractorInterface $extractor)
Definition: ExtractorService.php:77
‪TYPO3\CMS\Core\Resource\Service
Definition: ConfigurationService.php:18
‪TYPO3\CMS\Core\Resource\Service\ExtractorService\extractMetaData
‪extractMetaData(File $fileObject)
Definition: ExtractorService.php:34
‪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\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\Service\ExtractorService
Definition: ExtractorService.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52