|
| 1 | +export const 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 | + [ |
| 19 | + "docx", |
| 20 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 21 | + ], |
| 22 | + ["eot", "application/vnd.ms-fontobject"], |
| 23 | + ["epub", "application/epub+zip"], |
| 24 | + ["gz", "application/gzip"], |
| 25 | + ["gif", "image/gif"], |
| 26 | + ["heic", "image/heic"], |
| 27 | + ["heif", "image/heif"], |
| 28 | + ["htm", "text/html"], |
| 29 | + ["html", "text/html"], |
| 30 | + ["ico", "image/vnd.microsoft.icon"], |
| 31 | + ["ics", "text/calendar"], |
| 32 | + ["jar", "application/java-archive"], |
| 33 | + ["jpeg", "image/jpeg"], |
| 34 | + ["jpg", "image/jpeg"], |
| 35 | + ["js", "text/javascript"], |
| 36 | + ["json", "application/json"], |
| 37 | + ["jsonld", "application/ld+json"], |
| 38 | + ["mid", "audio/midi"], |
| 39 | + ["midi", "audio/midi"], |
| 40 | + ["mjs", "text/javascript"], |
| 41 | + ["mp3", "audio/mpeg"], |
| 42 | + ["mp4", "video/mp4"], |
| 43 | + ["mpeg", "video/mpeg"], |
| 44 | + ["mpkg", "application/vnd.apple.installer+xml"], |
| 45 | + ["odp", "application/vnd.oasis.opendocument.presentation"], |
| 46 | + ["ods", "application/vnd.oasis.opendocument.spreadsheet"], |
| 47 | + ["odt", "application/vnd.oasis.opendocument.text"], |
| 48 | + ["oga", "audio/ogg"], |
| 49 | + ["ogv", "video/ogg"], |
| 50 | + ["ogx", "application/ogg"], |
| 51 | + ["opus", "audio/opus"], |
| 52 | + ["otf", "font/otf"], |
| 53 | + ["png", "image/png"], |
| 54 | + ["pdf", "application/pdf"], |
| 55 | + ["php", "application/x-httpd-php"], |
| 56 | + ["ppt", "application/vnd.ms-powerpoint"], |
| 57 | + [ |
| 58 | + "pptx", |
| 59 | + "application/vnd.openxmlformats-officedocument.presentationml.presentation", |
| 60 | + ], |
| 61 | + ["rar", "application/vnd.rar"], |
| 62 | + ["rtf", "application/rtf"], |
| 63 | + ["sh", "application/x-sh"], |
| 64 | + ["svg", "image/svg+xml"], |
| 65 | + ["swf", "application/x-shockwave-flash"], |
| 66 | + ["tar", "application/x-tar"], |
| 67 | + ["tif", "image/tiff"], |
| 68 | + ["tiff", "image/tiff"], |
| 69 | + ["ts", "video/mp2t"], |
| 70 | + ["ttf", "font/ttf"], |
| 71 | + ["txt", "text/plain"], |
| 72 | + ["vsd", "application/vnd.visio"], |
| 73 | + ["wav", "audio/wav"], |
| 74 | + ["weba", "audio/webm"], |
| 75 | + ["webm", "video/webm"], |
| 76 | + ["webp", "image/webp"], |
| 77 | + ["woff", "font/woff"], |
| 78 | + ["woff2", "font/woff2"], |
| 79 | + ["xhtml", "application/xhtml+xml"], |
| 80 | + ["xls", "application/vnd.ms-excel"], |
| 81 | + ["xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"], |
| 82 | + ["xml", "application/xml"], |
| 83 | + ["xul", "application/vnd.mozilla.xul+xml"], |
| 84 | + ["zip", "application/zip"], |
| 85 | + ["7z", "application/x-7z-compressed"], |
| 86 | + |
| 87 | + // Others |
| 88 | + ["mkv", "video/x-matroska"], |
| 89 | + ["mov", "video/quicktime"], |
| 90 | + ["msg", "application/vnd.ms-outlook"], |
| 91 | +]); |
| 92 | + |
| 93 | +export function toFileWithPath(file, path) { |
| 94 | + const f = withMimeType(file); |
| 95 | + if (typeof f.path !== "string") { |
| 96 | + // on electron, path is already set to the absolute path |
| 97 | + const { webkitRelativePath } = file; |
| 98 | + Object.defineProperty(f, "path", { |
| 99 | + value: |
| 100 | + typeof path === "string" |
| 101 | + ? path |
| 102 | + : // If <input webkitdirectory> is set, |
| 103 | + // the File will have a {webkitRelativePath} property |
| 104 | + // https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory |
| 105 | + typeof webkitRelativePath === "string" && |
| 106 | + webkitRelativePath.length > 0 |
| 107 | + ? webkitRelativePath |
| 108 | + : file.name, |
| 109 | + writable: false, |
| 110 | + configurable: false, |
| 111 | + enumerable: true, |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + return f; |
| 116 | +} |
| 117 | + |
| 118 | +function withMimeType(file) { |
| 119 | + const { name } = file; |
| 120 | + const hasExtension = name && name.lastIndexOf(".") !== -1; |
| 121 | + |
| 122 | + if (hasExtension && !file.type) { |
| 123 | + const ext = name.split(".").pop().toLowerCase(); |
| 124 | + const type = COMMON_MIME_TYPES.get(ext); |
| 125 | + if (type) { |
| 126 | + Object.defineProperty(file, "type", { |
| 127 | + value: type, |
| 128 | + writable: false, |
| 129 | + configurable: false, |
| 130 | + enumerable: true, |
| 131 | + }); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return file; |
| 136 | +} |
0 commit comments