Mime.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\File;
  7. /**
  8. * Utility for mime type retrieval
  9. */
  10. class Mime
  11. {
  12. /**
  13. * Mime types
  14. *
  15. * @var array
  16. */
  17. protected $mimeTypes = [
  18. 'txt' => 'text/plain',
  19. 'htm' => 'text/html',
  20. 'html' => 'text/html',
  21. 'php' => 'text/html',
  22. 'css' => 'text/css',
  23. 'js' => 'application/javascript',
  24. 'json' => 'application/json',
  25. 'xml' => 'application/xml',
  26. 'swf' => 'application/x-shockwave-flash',
  27. 'flv' => 'video/x-flv',
  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. // archives
  41. 'zip' => 'application/zip',
  42. 'rar' => 'application/x-rar-compressed',
  43. 'exe' => 'application/x-msdownload',
  44. 'msi' => 'application/x-msdownload',
  45. 'cab' => 'application/vnd.ms-cab-compressed',
  46. // audio/video
  47. 'mp3' => 'audio/mpeg',
  48. 'qt' => 'video/quicktime',
  49. 'mov' => 'video/quicktime',
  50. // adobe
  51. 'pdf' => 'application/pdf',
  52. 'psd' => 'image/vnd.adobe.photoshop',
  53. 'ai' => 'application/postscript',
  54. 'eps' => 'application/postscript',
  55. 'ps' => 'application/postscript',
  56. ];
  57. /**
  58. * List of mime types that can be defined by file extension.
  59. *
  60. * @var array
  61. */
  62. private $defineByExtensionList = [
  63. 'txt' => 'text/plain',
  64. 'htm' => 'text/html',
  65. 'html' => 'text/html',
  66. 'php' => 'text/html',
  67. 'css' => 'text/css',
  68. 'js' => 'application/javascript',
  69. 'json' => 'application/json',
  70. 'xml' => 'application/xml',
  71. 'svg' => 'image/svg+xml',
  72. ];
  73. /**
  74. * Get mime type of a file
  75. *
  76. * @param string $file
  77. * @return string
  78. * @throws \InvalidArgumentException
  79. */
  80. public function getMimeType($file)
  81. {
  82. if (!file_exists($file)) {
  83. throw new \InvalidArgumentException("File '$file' doesn't exist");
  84. }
  85. $result = null;
  86. $extension = $this->getFileExtension($file);
  87. if (function_exists('mime_content_type')) {
  88. $result = $this->getNativeMimeType($file);
  89. }
  90. if (null === $result && isset($this->mimeTypes[$extension])) {
  91. $result = $this->mimeTypes[$extension];
  92. } elseif (null === $result) {
  93. $result = 'application/octet-stream';
  94. }
  95. return $result;
  96. }
  97. /**
  98. * Get mime type by the native mime_content_type function.
  99. *
  100. * Search for extended mime type if mime_content_type() returned 'application/octet-stream' or 'text/plain'
  101. *
  102. * @param string $file
  103. * @return string
  104. */
  105. private function getNativeMimeType(string $file): string
  106. {
  107. $extension = $this->getFileExtension($file);
  108. $result = mime_content_type($file);
  109. if (isset($this->mimeTypes[$extension], $this->defineByExtensionList[$extension])
  110. && (strpos($result, 'text/') === 0 || strpos($result, 'image/svg') === 0)
  111. ) {
  112. $result = $this->mimeTypes[$extension];
  113. }
  114. return $result;
  115. }
  116. /**
  117. * Get file extension by file name.
  118. *
  119. * @param string $file
  120. * @return string
  121. */
  122. private function getFileExtension(string $file): string
  123. {
  124. return strtolower(pathinfo($file, PATHINFO_EXTENSION));
  125. }
  126. }