‪TYPO3CMS  10.4
FileInfo.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 
20 
24 class ‪FileInfo extends \SplFileInfo implements ‪TypeInterface
25 {
34  public function ‪getMimeType()
35  {
36  $mimeType = false;
37  if ($this->isFile()) {
38  $fileExtensionToMimeTypeMapping = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'];
39  $lowercaseFileExtension = strtolower($this->getExtension());
40  if (!empty($fileExtensionToMimeTypeMapping[$lowercaseFileExtension])) {
41  $mimeType = $fileExtensionToMimeTypeMapping[$lowercaseFileExtension];
42  } else {
43  if (function_exists('finfo_file')) {
44  $fileInfo = new \finfo();
45  $mimeType = $fileInfo->file($this->getPathname(), FILEINFO_MIME_TYPE);
46  } elseif (function_exists('mime_content_type')) {
47  $mimeType = mime_content_type($this->getPathname());
48  }
49  }
50  }
51 
52  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][\‪TYPO3\CMS\Core\Type\File\FileInfo::class]['mimeTypeGuessers'] ?? [] as $mimeTypeGuesser) {
53  $hookParameters = [
54  'mimeType' => &$mimeType
55  ];
56 
57  GeneralUtility::callUserFunction(
58  $mimeTypeGuesser,
59  $hookParameters,
60  $this
61  );
62  }
63 
64  return $mimeType;
65  }
66 
78  public function ‪getMimeExtensions(): array
79  {
80  $mimeExtensions = [];
81  if ($this->isFile()) {
82  $fileExtensionToMimeTypeMapping = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'];
83  $mimeType = $this->‪getMimeType();
84  if (in_array($mimeType, $fileExtensionToMimeTypeMapping, true)) {
85  $mimeExtensions = array_keys($fileExtensionToMimeTypeMapping, $mimeType, true);
86  } elseif (function_exists('finfo_file')) {
87  $fileInfo = new \finfo();
88  $mimeExtensions = array_filter(
90  '/',
91  (string)$fileInfo->file($this->getPathname(), FILEINFO_EXTENSION)
92  ),
93  ‪function ($item) {
94  // filter invalid items ('???' is used if not found in magic.mime database)
95  return $item !== '' && $item !== '???';
96  }
97  );
98  }
99  }
100  return $mimeExtensions;
101  }
102 }
‪TYPO3\CMS\Core\Type\File
Definition: FileInfo.php:16
‪TYPO3
‪TYPO3\CMS\Core\Type\File\FileInfo\getMimeType
‪string bool getMimeType()
Definition: FileInfo.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\function
‪return function(ContainerConfigurator $container, ContainerBuilder $containerBuilder)
Definition: Services.php:12
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Type\File\FileInfo
Definition: FileInfo.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Type\TypeInterface
Definition: TypeInterface.php:24
‪TYPO3\CMS\Core\Type\File\FileInfo\getMimeExtensions
‪string[] getMimeExtensions()
Definition: FileInfo.php:78