Skip to content

Commit 635bab0

Browse files
Update MimeType.php
1 parent 5f4ccc0 commit 635bab0

File tree

1 file changed

+67
-18
lines changed

1 file changed

+67
-18
lines changed

src/Util/Filter/MimeType.php

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ final class MimeType
3333
public const DEFAULT_MIME_TYPES_FILE = __DIR__ . '/data/mime.types';
3434

3535
/**
36-
* @var ?array
36+
* @var ?array<string, array{
37+
* main: bool,
38+
* extensions: array<string>
39+
* }>
3740
*/
3841
protected static ?array $extensionMimeTypes = null;
3942

@@ -45,7 +48,7 @@ final class MimeType
4548
/**
4649
* The extension prefill
4750
*
48-
* @var string[]
51+
* @var array<string, string>
4952
*/
5053
private const EXTENSION_PREFILL = [
5154
'application/pdf' => 'pdf',
@@ -117,7 +120,10 @@ final class MimeType
117120
/**
118121
* Get Mime types List
119122
*
120-
* @return array{string, array{main: bool, extensions: array<string>}}
123+
* @return array<string, array{
124+
* main: bool,
125+
* extensions: array<string>
126+
* }>
121127
*/
122128
public static function getExtensionMimeTypes(): array
123129
{
@@ -129,14 +135,21 @@ public static function getExtensionMimeTypes(): array
129135
E_COMPILE_ERROR
130136
);
131137
}
138+
132139
self::$extensionMimeTypes = [];
133-
$sock = @fopen($mimeFile, 'r');
140+
$sock = fopen($mimeFile, 'r');
141+
if (!$sock) {
142+
throw new RuntimeException(
143+
'Failed to open mime.types file',
144+
E_COMPILE_ERROR
145+
);
146+
}
134147
$data = '';
135148
while (!feof($sock)) {
136149
$data .= fread($sock, 4096);
137150
}
138151
fclose($sock);
139-
$data = trim(preg_replace('~^\s*#([^=]+)[^\n]+\r?\n~', '', $data));
152+
$data = trim((string) preg_replace('~^\s*#([^=]+)[^\n]+\r?\n~', '', $data));
140153
$data = str_replace(["\r\n", "\n\n"], "\n", $data);
141154
preg_match_all(
142155
'~
@@ -179,7 +192,9 @@ public static function getExtensionMimeTypes(): array
179192
$ext['extensions'] = [self::EXTENSION_PREFILL[$item]] + $ext['extensions'];
180193
$ext['extensions'] = array_values(array_unique($ext['extensions']));
181194
}
182-
self::$extensionMimeTypes[$item] = $ext;
195+
if (is_array($ext)) {
196+
self::$extensionMimeTypes[$item] = $ext;
197+
}
183198
}
184199
}
185200
$registered = [];
@@ -201,7 +216,7 @@ public static function getExtensionMimeTypes(): array
201216
*
202217
* @param string $mime
203218
*
204-
* @return array[]|null
219+
* @return array<string>|null
205220
*/
206221
public static function fromMimeType(string $mime): ?array
207222
{
@@ -288,8 +303,16 @@ public static function clear() : void
288303
self::$extensionMimeTypes = null;
289304
}
290305

306+
/**
307+
* @var array<string, string|false>
308+
*/
291309
private static array $uriMimeTypes = [];
292310

311+
/**
312+
* The finfo object
313+
*
314+
* @var null|finfo|false
315+
*/
293316
private static null|finfo|false $finfoObjectExists = null;
294317

295318
/**
@@ -310,25 +333,33 @@ public static function streamMimeType(StreamInterface $stream) : ?string
310333
{
311334
$uri = $stream->getMetadata('uri');
312335
if ($uri && isset(self::$uriMimeTypes[$uri])) {
313-
return self::$uriMimeTypes[$uri];
336+
return self::$uriMimeTypes[$uri]?:null;
314337
}
315-
338+
$uri = (string) $uri;
316339
$info = self::createFinfo();
317340
$mimeType = $info?->buffer((string) $stream)?:null;
318341
if ($mimeType && file_exists($uri)) {
319342
if ($mimeType === 'text/plain' && (
320-
$ext = pathinfo($uri, PATHINFO_EXTENSION)
343+
$ext = pathinfo($uri, PATHINFO_EXTENSION)
321344
) && ($key = array_search($ext, self::EXTENSION_PREFILL))
322345
) {
323346
$mimeType = $key;
324347
}
325-
self::$uriMimeTypes[$uri] = $mimeType;
348+
if (!is_string($mimeType)) {
349+
$mimeType = null;
350+
}
351+
/**
352+
* @var ?string $mimeType
353+
*/
354+
self::$uriMimeTypes[$uri] = $mimeType??false;
326355
return $mimeType;
327356
}
328357
return $mimeType;
329358
}
330359

331360
/**
361+
* Get Mime Type from File Path
362+
*
332363
* @param string $filePath
333364
* @param ?UploadedFileInterface $uploadedFile
334365
* @return string|null
@@ -348,19 +379,23 @@ public static function fileMimeType(string $filePath, ?UploadedFileInterface $up
348379

349380
self::$uriMimeTypes[$filePath] = false;
350381
$finfo = self::createFinfo();
351-
$mimeType = Consolidation::callbackReduceError(fn() => $finfo?->file($filePath))?:null;
382+
set_error_handler(null);
383+
$mimeType = $finfo?->file($filePath)?:null;
384+
restore_error_handler();
352385
if (!$mimeType && $fn_exists === null) {
353386
$fn_exists = function_exists('mime_content_type');
354387
}
355388
if ($fn_exists) {
356389
/**
357390
* @var ?string $mimeType
358391
*/
359-
$mimeType = Consolidation::callbackReduceError(fn () => mime_content_type($filePath))?:null;
392+
set_error_handler(null);
393+
$mimeType = mime_content_type($filePath);
394+
restore_error_handler();
360395
}
361396
if ($mimeType) {
362397
if ($mimeType === 'text/plain' && (
363-
$ext = pathinfo($uploadedFile?->getClientFilename()??$filePath, PATHINFO_EXTENSION)
398+
$ext = pathinfo($uploadedFile?->getClientFilename()??$filePath, PATHINFO_EXTENSION)
364399
) && ($key = array_search($ext, self::EXTENSION_PREFILL))
365400
) {
366401
$mimeType = $key;
@@ -381,6 +416,12 @@ public static function fileMimeType(string $filePath, ?UploadedFileInterface $up
381416
return $mimeType;
382417
}
383418

419+
/**
420+
* Get Mime Type from Uploaded File
421+
*
422+
* @param UploadedFileInterface $uploadedFile
423+
* @return ?string
424+
*/
384425
public static function mimeTypeUploadedFile(UploadedFileInterface $uploadedFile): ?string
385426
{
386427
$uri = $uploadedFile->getStream()->getMetadata('uri');
@@ -390,16 +431,24 @@ public static function mimeTypeUploadedFile(UploadedFileInterface $uploadedFile)
390431
return null;
391432
}
392433
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
393-
return is_string($extension)
394-
? MimeType::mime($extension)
395-
: null;
434+
if (!is_string($extension)) {
435+
return null;
436+
}
437+
return MimeType::mime($extension);
396438
}
397439
return self::fileMimeType($uri, $uploadedFile);
398440
}
399441

442+
/**
443+
* Resolve Media Type Uploaded Files
444+
*
445+
* @param UploadedFileInterface $uploadedFile
446+
* @return UploadedFileInterface
447+
* @noinspection PhpUnused
448+
*/
400449
public static function resolveMediaTypeUploadedFiles(
401450
UploadedFileInterface $uploadedFile
402-
): UploadedFile|UploadedFileInterface {
451+
): UploadedFileInterface {
403452
$mimeType = self::mimeTypeUploadedFile($uploadedFile);
404453
if (!$mimeType || $uploadedFile->getClientMediaType() === $mimeType) {
405454
return $uploadedFile;

0 commit comments

Comments
 (0)