File.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Libs\TikTok\Kernel\Support;
  11. use finfo;
  12. /**
  13. * Class File.
  14. */
  15. class File {
  16. /**
  17. * MIME mapping.
  18. *
  19. * @var array
  20. */
  21. protected static $extensionMap = [
  22. 'audio/wav' => '.wav',
  23. 'audio/x-ms-wma' => '.wma',
  24. 'video/x-ms-wmv' => '.wmv',
  25. 'video/mp4' => '.mp4',
  26. 'audio/mpeg' => '.mp3',
  27. 'audio/amr' => '.amr',
  28. 'application/vnd.rn-realmedia' => '.rm',
  29. 'audio/mid' => '.mid',
  30. 'image/bmp' => '.bmp',
  31. 'image/gif' => '.gif',
  32. 'image/png' => '.png',
  33. 'image/tiff' => '.tiff',
  34. 'image/jpeg' => '.jpg',
  35. 'application/pdf' => '.pdf',
  36. // 列举更多的文件 mime, 企业号是支持的,公众平台这边之后万一也更新了呢
  37. 'application/msword' => '.doc',
  38. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => '.docx',
  39. 'application/vnd.openxmlformats-officedocument.wordprocessingml.template' => '.dotx',
  40. 'application/vnd.ms-word.document.macroEnabled.12' => '.docm',
  41. 'application/vnd.ms-word.template.macroEnabled.12' => '.dotm',
  42. 'application/vnd.ms-excel' => '.xls',
  43. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => '.xlsx',
  44. 'application/vnd.openxmlformats-officedocument.spreadsheetml.template' => '.xltx',
  45. 'application/vnd.ms-excel.sheet.macroEnabled.12' => '.xlsm',
  46. 'application/vnd.ms-excel.template.macroEnabled.12' => '.xltm',
  47. 'application/vnd.ms-excel.addin.macroEnabled.12' => '.xlam',
  48. 'application/vnd.ms-excel.sheet.binary.macroEnabled.12' => '.xlsb',
  49. 'application/vnd.ms-powerpoint' => '.ppt',
  50. 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => '.pptx',
  51. 'application/vnd.openxmlformats-officedocument.presentationml.template' => '.potx',
  52. 'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => '.ppsx',
  53. 'application/vnd.ms-powerpoint.addin.macroEnabled.12' => '.ppam',
  54. ];
  55. /**
  56. * File header signatures.
  57. *
  58. * @var array
  59. */
  60. protected static $signatures = [
  61. 'ffd8ff' => '.jpg',
  62. '424d' => '.bmp',
  63. '47494638' => '.gif',
  64. '2f55736572732f6f7665' => '.png',
  65. '89504e47' => '.png',
  66. '494433' => '.mp3',
  67. 'fffb' => '.mp3',
  68. 'fff3' => '.mp3',
  69. '3026b2758e66cf11' => '.wma',
  70. '52494646' => '.wav',
  71. '57415645' => '.wav',
  72. '41564920' => '.avi',
  73. '000001ba' => '.mpg',
  74. '000001b3' => '.mpg',
  75. '2321414d52' => '.amr',
  76. '25504446' => '.pdf',
  77. ];
  78. /**
  79. * Return steam extension.
  80. *
  81. * @param string $stream
  82. *
  83. * @return string|false
  84. */
  85. public static function getStreamExt(string $stream) {
  86. $ext = self::getExtBySignature($stream);
  87. try {
  88. if (empty($ext) && is_readable($stream)) {
  89. $stream = file_get_contents($stream);
  90. }
  91. } catch (\Exception $e) {
  92. }
  93. $fileInfo = new finfo(FILEINFO_MIME);
  94. $mime = strstr($fileInfo->buffer($stream), ';', true);
  95. return self::$extensionMap[$mime] ?? $ext;
  96. }
  97. /**
  98. * Get file extension by file header signature.
  99. *
  100. * @param string $stream
  101. *
  102. * @return string
  103. */
  104. public static function getExtBySignature(string $stream): string {
  105. $prefix = strval(bin2hex(mb_strcut($stream, 0, 10)));
  106. foreach (self::$signatures as $signature => $extension) {
  107. if (0 === strpos($prefix, strval($signature))) {
  108. return $extension;
  109. }
  110. }
  111. return '';
  112. }
  113. }