-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMime.php
495 lines (384 loc) · 14.5 KB
/
Mime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// mime
// class with static methods to get or guess mime types
final class Mime extends Root
{
// config
protected static array $config = [
'defaultFamily'=>'binary', // famille par défaut pour la méthode add
'groupToExtension'=>[ // permet de lier des extensions à des groupes
'audio'=>'mp3',
'calendar'=>'ics',
'css'=>['css','scss'],
'csv'=>'csv',
'doc'=>['doc','doct','docx','docxt'],
'font'=>'ttf',
'html'=>'html',
'imageRaster'=>['jpg','gif','jpeg','png'],
'imageVector'=>'svg',
'js'=>['js','jsx'],
'json'=>'json',
'pdf'=>'pdf',
'php'=>'php',
'txt'=>'txt',
'video'=>['mp4','mov'],
'xml'=>'xml',
'zip'=>'zip'],
'mimeToExtension'=>[ // liste de mimetype commun avec leur extension
'audio/mpeg'=>'mp3',
'text/calendar'=>'ics',
'text/csv'=>'csv',
'application/octet-stream'=>['ttf'],
'text/html'=>'html',
'image/gif'=>'gif',
'image/jpeg'=>['jpg','jpeg'],
'image/png'=>'png',
'image/svg'=>'svg',
'text/json'=>'json',
'application/json'=>'json',
'application/problem+json'=>'json',
'application/pdf'=>'pdf',
'text/plain'=>'txt',
'text/css'=>'css',
'text/x-scss'=>'scss',
'text/javascript'=>'js',
'text/x-php'=>'php',
'application/msword'=>['doc','doct'],
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'=>'docx',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template'=>'docxt',
'video/mp4'=>'mp4',
'video/quicktime'=>'mov',
'text/xml'=>'xml',
'application/zip'=>'zip'],
'family'=>[ // permet de lier des groupes à des familles
'image'=>['imageRaster','imageVector'],
'binary'=>['audio','font','imageRaster','imageVector','pdf','video','zip'],
'text'=>['calendar','css','csv','doc','html','js','json','php','txt','xml']]
];
// isEmpty
// retourne vrai si le mime type en est un d'un fichier vide
final public static function isEmpty($value):bool
{
return is_string($value) && Str::isStarts(['inode/x-empty','application/x-empty'],$value);
}
// isGroup
// retourne vrai si le mime type est du group spécifé
// possible de fournir un fichier ou une resource si get est true
final public static function isGroup($group,$value,bool $get=false,bool $fromPath=true):bool
{
$return = false;
if(is_string($group))
{
if($get === true && self::getGroup($value,$fromPath) === $group)
$return = true;
elseif(is_string($value) && self::group($value) === $group)
$return = true;
}
return $return;
}
// isFamily
// retourne vrai si la famille est celle spécifié
final public static function isFamily($family,$value,bool $get=false,bool $fromPath=true):bool
{
$return = false;
$group = ($get === true)? self::getGroup($value,$fromPath):self::group($value);
if(!empty($group))
$return = Arr::some(self::$config['family'],fn($array,$key) => is_array($array) && in_array($group,$array,true) && $key === $family);
return $return;
}
// isExtensionInGroup
// retourne vrai si l'extension est dans le group spécifié
final public static function isExtensionInGroup($value,$mimeGroup):bool
{
$return = false;
if(is_string($value) && is_string($mimeGroup))
{
$value = strtolower($value);
$extensions = self::extensionsFromGroup($mimeGroup);
$return = (in_array($value,$extensions,true));
}
return $return;
}
// get
// retourne le mimetype du fichier ou de la resource à partir de finfo
final public static function get($value,bool $charset=true):?string
{
$return = null;
if(is_resource($value))
$return = self::getFromResource($value,$charset,true);
elseif(File::is($value))
{
$value = File::path($value);
$finfo = Finfo::open();
if(!empty($finfo))
{
$mime = Finfo::read($finfo,$value);
if(is_string($mime) && !empty($mime))
{
if(self::toExtension($mime,true) === 'txt')
$return = self::fromPath($value);
else
$return = $mime;
if(is_string($return) && $charset === false)
$return = self::removeCharset($return);
}
Finfo::close($finfo);
}
}
return $return;
}
// getFromResource
// retourne le mime type à partir d'une resource
// gère le context option
final public static function getFromResource($value,bool $charset=true,bool $contextOption=true):?string
{
$return = null;
if($contextOption === true || Res::isPhpWritable($value))
{
$return = Res::getContextMime($value);
if(is_string($return))
{
$return = self::fromExtension($return) ?? $return;
if($charset === false)
$return = self::removeCharset($return);
}
}
if(empty($return))
{
if(Res::isFile($value))
{
$path = Res::path($value);
if(is_string($path))
$return = self::get($path,$charset);
}
elseif(Res::isHttp($value))
{
$header = Res::wrapperData($value);
if(is_array($header) && !empty($header))
{
$mime = Header::contentType($header,$charset);
if(is_string($mime) && !empty($mime))
$return = $mime;
}
}
}
return $return;
}
// getGroup
// retourne le group du mimetype du fichier ou de la resource à partir de finfo
// si extension est true, le fichier n'a pas à exister et l'extension sera utilisé pour déterminer
final public static function getGroup($value,bool $fromPath=true):?string
{
$return = null;
$mime = self::get($value);
if($fromPath === true && (empty($mime) || self::isEmpty($mime)))
$mime = self::fromPath($value);
if(!empty($mime))
$return = self::group($mime);
return $return;
}
// getFamilies
// retourne toutes les familles du fichier ou de la resource
// si extension est true, le fichier n'a pas à exister et l'extension sera utilisé pour déterminer
final public static function getFamilies($value,bool $fromPath=true):?array
{
$return = null;
$group = self::getGroup($value,$fromPath);
if(!empty($group))
$return = self::families($group);
return $return;
}
// getFamily
// retourne la première famille du fichier ou de la resource
// si extension est true, le fichier n'a pas à exister et l'extension sera utilisé pour déterminer
final public static function getFamily($value,bool $fromPath=true):?string
{
$return = null;
$families = self::getFamilies($value,$fromPath);
if(!empty($families))
$return = current($families);
return $return;
}
// getCorrectExtension
// fait un mime type sur un fichier ou la resource et retourne l'extension que celui-ci devrait utilisé
final public static function getCorrectExtension($value):?string
{
$return = null;
$mime = self::get($value);
if(is_string($mime))
$return = self::toExtension($mime,true);
return $return;
}
// group
// retourne le nom du groupe à partir d'un mimeType
final public static function group(string $value):?string
{
$return = null;
$extension = self::toExtension($value,true);
if(!empty($extension))
{
$extension = strtolower($extension);
$closure = fn($v) => is_array($v) && (in_array($extension,$v,true)) || $extension === $v;
$return = Arr::findKey(self::$config['groupToExtension'],$closure);
}
return $return;
}
// families
// retourne toutes les familles contenant le groupe donné en argument
final public static function families(string $value):array
{
$return = [];
foreach (self::$config['family'] as $key => $array)
{
if(is_array($array) && in_array($value,$array,true))
$return[] = $key;
}
return $return;
}
// family
// retourne la première famille trouvé contenant le groupe donné en argument
final public static function family(string $value):?string
{
$return = null;
$families = self::families($value);
if(!empty($families))
$return = current($families);
return $return;
}
// fromPath
// retourne le mimetype du fichier à partir de son extension
// si path est seulement l'extension, la fonction retourne également le mime type
// pratique pour les fichiers qui n'existent pas
final public static function fromPath($value):?string
{
$return = null;
if(is_resource($value))
$value = Res::extension($value);
elseif(is_string($value))
$value = Path::extension($value) ?? $value;
if(is_string($value))
$return = self::fromExtension($value);
return $return;
}
// fromExtension
// retourne le mime type à partir d'une extension
final public static function fromExtension(string $extension):?string
{
$closure = fn($value) => (is_array($value) && Arr::in($extension,$value,false)) || Str::icompare($extension,$value);
return Arr::findKey(self::$config['mimeToExtension'],$closure);
}
// groupFromBasename
// retourne le group mime type à partir d'un basename
final public static function groupFromBasename(string $basename):?string
{
$return = null;
$extension = Path::extension($basename);
if(!empty($extension))
$return = self::groupFromExtension($extension);
return $return;
}
// groupFromExtension
// retourne le group mime type à partir d'une extension
final public static function groupFromExtension(string $extension):?string
{
$return = null;
$mime = self::fromExtension($extension);
if(!empty($mime))
$return = self::group($mime);
return $return;
}
// toExtension
// retourne la meilleur extension trouvée à partir d'un mime type
final public static function toExtension(string $mime,bool $extension=true):?string
{
$return = null;
foreach (self::$config['mimeToExtension'] as $key => $value)
{
if(!is_array($value))
$value = [$value];
if(stripos($mime,$key) === 0)
{
$return = current($value);
break;
}
elseif(Arr::in($mime,$value,false) && $extension === true)
{
$return = strtolower($mime);
break;
}
}
return $return;
}
// extensionsFromGroup
// retourne toutes les extensions admises pour un groupe
final public static function extensionsFromGroup(string $value):array
{
$return = [];
if(array_key_exists($value,self::$config['groupToExtension']))
$return = (array) self::$config['groupToExtension'][$value];
return $return;
}
// extensionFromGroup
// retourne une extension admise dans un group
// par défaut retourne index 0
final public static function extensionFromGroup(string $value,int $index=0):?string
{
$return = null;
$extensions = self::extensionsFromGroup($value);
if(is_array($extensions))
$return = Arr::index($index,$extensions);
return $return;
}
// removeCharset
// enlève le charset à partir d'une string mime
final public static function removeCharset(string $return):string
{
if(strpos($return,';') > 0)
$return = Str::explodeIndex(0,';',$return,null,true);
return $return;
}
// register
// permet d'ajouter une nouvelle entrée mime dans la configuration de la classe
// fournir le mime, une ou plusieurs extensions, le nom du group
// possible de fournir le nom de la famille, sinon ce sera binary par défaut
final public static function register(string $mime,$extension,string $group,$families=null):bool
{
$return = false;
if(!empty($extension))
{
$mime = self::removeCharset($mime);
if(empty($families))
$families = self::families($group);
if(empty($families))
$families = self::$config['defaultFamily'];
if(!is_array($families))
$families = [$families];
if(!is_array($extension))
$extension = [$extension];
if(!array_key_exists($mime,self::$config['mimeToExtension']))
self::$config['mimeToExtension'][$mime] = $extension;
else
self::$config['mimeToExtension'][$mime] = Arr::mergeUnique(self::$config['mimeToExtension'][$mime],$extension);
if(!array_key_exists($group,self::$config['groupToExtension']))
self::$config['groupToExtension'][$group] = [];
self::$config['groupToExtension'][$group] = Arr::mergeUnique(self::$config['groupToExtension'][$group],$extension);
foreach ($families as $family)
{
if(!array_key_exists($family,self::$config['family']))
self::$config['family'][$family] = [];
if(!in_array($group,self::$config['family'][$family], true))
self::$config['family'][$family][] = $group;
}
$return = true;
}
return $return;
}
}
?>