file.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. export var COMMON_MIME_TYPES = new Map([
  2. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
  3. ['aac', 'audio/aac'],
  4. ['abw', 'application/x-abiword'],
  5. ['arc', 'application/x-freearc'],
  6. ['avif', 'image/avif'],
  7. ['avi', 'video/x-msvideo'],
  8. ['azw', 'application/vnd.amazon.ebook'],
  9. ['bin', 'application/octet-stream'],
  10. ['bmp', 'image/bmp'],
  11. ['bz', 'application/x-bzip'],
  12. ['bz2', 'application/x-bzip2'],
  13. ['cda', 'application/x-cdf'],
  14. ['csh', 'application/x-csh'],
  15. ['css', 'text/css'],
  16. ['csv', 'text/csv'],
  17. ['doc', 'application/msword'],
  18. ['docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
  19. ['eot', 'application/vnd.ms-fontobject'],
  20. ['epub', 'application/epub+zip'],
  21. ['gz', 'application/gzip'],
  22. ['gif', 'image/gif'],
  23. ['heic', 'image/heic'],
  24. ['heif', 'image/heif'],
  25. ['htm', 'text/html'],
  26. ['html', 'text/html'],
  27. ['ico', 'image/vnd.microsoft.icon'],
  28. ['ics', 'text/calendar'],
  29. ['jar', 'application/java-archive'],
  30. ['jpeg', 'image/jpeg'],
  31. ['jpg', 'image/jpeg'],
  32. ['js', 'text/javascript'],
  33. ['json', 'application/json'],
  34. ['jsonld', 'application/ld+json'],
  35. ['mid', 'audio/midi'],
  36. ['midi', 'audio/midi'],
  37. ['mjs', 'text/javascript'],
  38. ['mp3', 'audio/mpeg'],
  39. ['mp4', 'video/mp4'],
  40. ['mpeg', 'video/mpeg'],
  41. ['mpkg', 'application/vnd.apple.installer+xml'],
  42. ['odp', 'application/vnd.oasis.opendocument.presentation'],
  43. ['ods', 'application/vnd.oasis.opendocument.spreadsheet'],
  44. ['odt', 'application/vnd.oasis.opendocument.text'],
  45. ['oga', 'audio/ogg'],
  46. ['ogv', 'video/ogg'],
  47. ['ogx', 'application/ogg'],
  48. ['opus', 'audio/opus'],
  49. ['otf', 'font/otf'],
  50. ['png', 'image/png'],
  51. ['pdf', 'application/pdf'],
  52. ['php', 'application/x-httpd-php'],
  53. ['ppt', 'application/vnd.ms-powerpoint'],
  54. ['pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
  55. ['rar', 'application/vnd.rar'],
  56. ['rtf', 'application/rtf'],
  57. ['sh', 'application/x-sh'],
  58. ['svg', 'image/svg+xml'],
  59. ['swf', 'application/x-shockwave-flash'],
  60. ['tar', 'application/x-tar'],
  61. ['tif', 'image/tiff'],
  62. ['tiff', 'image/tiff'],
  63. ['ts', 'video/mp2t'],
  64. ['ttf', 'font/ttf'],
  65. ['txt', 'text/plain'],
  66. ['vsd', 'application/vnd.visio'],
  67. ['wav', 'audio/wav'],
  68. ['weba', 'audio/webm'],
  69. ['webm', 'video/webm'],
  70. ['webp', 'image/webp'],
  71. ['woff', 'font/woff'],
  72. ['woff2', 'font/woff2'],
  73. ['xhtml', 'application/xhtml+xml'],
  74. ['xls', 'application/vnd.ms-excel'],
  75. ['xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
  76. ['xml', 'application/xml'],
  77. ['xul', 'application/vnd.mozilla.xul+xml'],
  78. ['zip', 'application/zip'],
  79. ['7z', 'application/x-7z-compressed'],
  80. // Others
  81. ['mkv', 'video/x-matroska'],
  82. ['mov', 'video/quicktime'],
  83. ['msg', 'application/vnd.ms-outlook']
  84. ]);
  85. export function toFileWithPath(file, path) {
  86. var f = withMimeType(file);
  87. if (typeof f.path !== 'string') { // on electron, path is already set to the absolute path
  88. var webkitRelativePath = file.webkitRelativePath;
  89. Object.defineProperty(f, 'path', {
  90. value: typeof path === 'string'
  91. ? path
  92. // If <input webkitdirectory> is set,
  93. // the File will have a {webkitRelativePath} property
  94. // https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory
  95. : typeof webkitRelativePath === 'string' && webkitRelativePath.length > 0
  96. ? webkitRelativePath
  97. : file.name,
  98. writable: false,
  99. configurable: false,
  100. enumerable: true
  101. });
  102. }
  103. return f;
  104. }
  105. function withMimeType(file) {
  106. var name = file.name;
  107. var hasExtension = name && name.lastIndexOf('.') !== -1;
  108. if (hasExtension && !file.type) {
  109. var ext = name.split('.')
  110. .pop().toLowerCase();
  111. var type = COMMON_MIME_TYPES.get(ext);
  112. if (type) {
  113. Object.defineProperty(file, 'type', {
  114. value: type,
  115. writable: false,
  116. configurable: false,
  117. enumerable: true
  118. });
  119. }
  120. }
  121. return file;
  122. }
  123. //# sourceMappingURL=file.js.map