Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Mime.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Framework\File;
8 
9 class Mime
10 {
16  protected $mimeTypes = [
17  'txt' => 'text/plain',
18  'htm' => 'text/html',
19  'html' => 'text/html',
20  'php' => 'text/html',
21  'css' => 'text/css',
22  'js' => 'application/javascript',
23  'json' => 'application/json',
24  'xml' => 'application/xml',
25  'swf' => 'application/x-shockwave-flash',
26  'flv' => 'video/x-flv',
27 
28  // images
29  'png' => 'image/png',
30  'jpe' => 'image/jpeg',
31  'jpeg' => 'image/jpeg',
32  'jpg' => 'image/jpeg',
33  'gif' => 'image/gif',
34  'bmp' => 'image/bmp',
35  'ico' => 'image/vnd.microsoft.icon',
36  'tiff' => 'image/tiff',
37  'tif' => 'image/tiff',
38  'svg' => 'image/svg+xml',
39  'svgz' => 'image/svg+xml',
40 
41  // archives
42  'zip' => 'application/zip',
43  'rar' => 'application/x-rar-compressed',
44  'exe' => 'application/x-msdownload',
45  'msi' => 'application/x-msdownload',
46  'cab' => 'application/vnd.ms-cab-compressed',
47 
48  // audio/video
49  'mp3' => 'audio/mpeg',
50  'qt' => 'video/quicktime',
51  'mov' => 'video/quicktime',
52 
53  // adobe
54  'pdf' => 'application/pdf',
55  'psd' => 'image/vnd.adobe.photoshop',
56  'ai' => 'application/postscript',
57  'eps' => 'application/postscript',
58  'ps' => 'application/postscript',
59  ];
60 
66  private $defineByExtensionList = [
67  'txt' => 'text/plain',
68  'htm' => 'text/html',
69  'html' => 'text/html',
70  'php' => 'text/html',
71  'css' => 'text/css',
72  'js' => 'application/javascript',
73  'json' => 'application/json',
74  'xml' => 'application/xml',
75  'svg' => 'image/svg+xml',
76  ];
77 
85  public function getMimeType($file)
86  {
87  if (!file_exists($file)) {
88  throw new \InvalidArgumentException("File '$file' doesn't exist");
89  }
90 
91  $result = null;
92  $extension = $this->getFileExtension($file);
93 
94  if (function_exists('mime_content_type')) {
95  $result = $this->getNativeMimeType($file);
96  }
97 
98  if (null === $result && isset($this->mimeTypes[$extension])) {
99  $result = $this->mimeTypes[$extension];
100  } elseif (null === $result) {
101  $result = 'application/octet-stream';
102  }
103 
104  return $result;
105  }
106 
114  private function getNativeMimeType(string $file): string
115  {
116  $extension = $this->getFileExtension($file);
117  $result = mime_content_type($file);
118  if (isset($this->mimeTypes[$extension], $this->defineByExtensionList[$extension])
119  && (strpos($result, 'text/') === 0 || strpos($result, 'image/svg') === 0)
120  ) {
121  $result = $this->mimeTypes[$extension];
122  }
123 
124  return $result;
125  }
126 
133  private function getFileExtension(string $file): string
134  {
135  return strtolower(pathinfo($file, PATHINFO_EXTENSION));
136  }
137 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17