‪TYPO3CMS  9.5
FileInfo.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
23 class ‪FileInfo extends \SplFileInfo implements ‪TypeInterface
24 {
33  public function ‪getMimeType()
34  {
35  $mimeType = false;
36  if ($this->isFile()) {
37  $fileExtensionToMimeTypeMapping = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'];
38  $lowercaseFileExtension = strtolower($this->getExtension());
39  if (!empty($fileExtensionToMimeTypeMapping[$lowercaseFileExtension])) {
40  $mimeType = $fileExtensionToMimeTypeMapping[$lowercaseFileExtension];
41  } else {
42  if (function_exists('finfo_file')) {
43  $fileInfo = new \finfo();
44  $mimeType = $fileInfo->file($this->getPathname(), FILEINFO_MIME_TYPE);
45  } elseif (function_exists('mime_content_type')) {
46  $mimeType = mime_content_type($this->getPathname());
47  }
48  }
49  }
50 
51  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][\‪TYPO3\CMS\Core\Type\File\FileInfo::class]['mimeTypeGuessers'] ?? [] as $mimeTypeGuesser) {
52  $hookParameters = [
53  'mimeType' => &$mimeType
54  ];
55 
56  GeneralUtility::callUserFunction(
57  $mimeTypeGuesser,
58  $hookParameters,
59  $this
60  );
61  }
62 
63  return $mimeType;
64  }
65 
77  public function ‪getMimeExtensions(): array
78  {
79  $mimeExtensions = [];
80  if ($this->isFile()) {
81  $fileExtensionToMimeTypeMapping = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'];
82  $mimeType = $this->‪getMimeType();
83  if (in_array($mimeType, $fileExtensionToMimeTypeMapping, true)) {
84  $mimeExtensions = array_keys($fileExtensionToMimeTypeMapping, $mimeType, true);
85  } elseif (function_exists('finfo_file')) {
86  $fileInfo = new \finfo();
87  $mimeExtensions = array_filter(
88  GeneralUtility::trimExplode(
89  '/',
90  (string)$fileInfo->file($this->getPathname(), FILEINFO_EXTENSION)
91  ),
92  function ($item) {
93  // filter invalid items ('???' is used if not found in magic.mime database)
94  return $item !== '' && $item !== '???';
95  }
96  );
97  }
98  }
99  return $mimeExtensions;
100  }
101 }
‪TYPO3\CMS\Core\Type\File
Definition: FileInfo.php:2
‪TYPO3
‪TYPO3\CMS\Core\Type\File\FileInfo\getMimeType
‪string bool getMimeType()
Definition: FileInfo.php:33
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Type\File\FileInfo
Definition: FileInfo.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Type\TypeInterface
Definition: TypeInterface.php:23
‪TYPO3\CMS\Core\Type\File\FileInfo\getMimeExtensions
‪string[] getMimeExtensions()
Definition: FileInfo.php:77