|
| 1 | +export const FILE_TYPES = { |
| 2 | + // Specific types that need to match a fixed set of mime types |
| 3 | + pdf: { icon: "file-pdf", mimeTypes: ["application/pdf"] }, |
| 4 | + word: { |
| 5 | + icon: "file-word", |
| 6 | + mimeTypes: [ |
| 7 | + "application/msword", |
| 8 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 9 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.template", |
| 10 | + "application/vnd.ms-word.document.macroEnabled.12", |
| 11 | + "application/vnd.ms-word.template.macroEnabled.12", |
| 12 | + ], |
| 13 | + }, |
| 14 | + excel: { |
| 15 | + icon: "file-excel", |
| 16 | + mimeTypes: [ |
| 17 | + "application/vnd.ms-excel", |
| 18 | + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| 19 | + "application/vnd.openxmlformats-officedocument.spreadsheetml.template", |
| 20 | + "application/vnd.ms-excel.sheet.macroEnabled.12", |
| 21 | + "application/vnd.ms-excel.template.macroEnabled.12", |
| 22 | + "application/vnd.ms-excel.addin.macroEnabled.12", |
| 23 | + "application/vnd.ms-excel.sheet.binary.macroEnabled.12", |
| 24 | + ], |
| 25 | + }, |
| 26 | + powerpoint: { |
| 27 | + icon: "file-powerpoint", |
| 28 | + mimeTypes: [ |
| 29 | + "application/vnd.ms-powerpoint", |
| 30 | + "application/vnd.openxmlformats-officedocument.presentationml.presentation", |
| 31 | + "application/vnd.openxmlformats-officedocument.presentationml.template", |
| 32 | + "application/vnd.openxmlformats-officedocument.presentationml.slideshow", |
| 33 | + "application/vnd.ms-powerpoint.addin.macroEnabled.12", |
| 34 | + "application/vnd.ms-powerpoint.presentation.macroEnabled.12", |
| 35 | + "application/vnd.ms-powerpoint.template.macroEnabled.12", |
| 36 | + "application/vnd.ms-powerpoint.slideshow.macroEnabled.12", |
| 37 | + ], |
| 38 | + }, |
| 39 | + // Fallback types taken from the first part of the mime type |
| 40 | + image: { icon: "file-image", match: /image\// }, |
| 41 | + video: { icon: "file-video", match: /video\// }, |
| 42 | + text: { icon: "file-lines", match: /text\// }, |
| 43 | + audio: { icon: "file-audio", match: /audio\// }, |
| 44 | +}; |
| 45 | + |
| 46 | +export function getIcon(fileType, additionalFileTypes = {}) { |
| 47 | + const allFileTypes = { ...additionalFileTypes, ...FILE_TYPES }; |
| 48 | + |
| 49 | + return allFileTypes[fileType]?.icon ?? "file"; |
| 50 | +} |
| 51 | + |
| 52 | +export function getFileType(mimeType, additionalFileTypes = {}) { |
| 53 | + if (!mimeType) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + const allFileTypes = { ...additionalFileTypes, ...FILE_TYPES }; |
| 58 | + |
| 59 | + const match = Object.entries(allFileTypes).find(([, config]) => { |
| 60 | + if (config.mimeTypes) { |
| 61 | + return config.mimeTypes?.includes(mimeType); |
| 62 | + } else if (config.match) { |
| 63 | + return mimeType.search(config.match) > -1; |
| 64 | + } |
| 65 | + |
| 66 | + return false; |
| 67 | + }); |
| 68 | + |
| 69 | + return match?.[0] ?? null; |
| 70 | +} |
0 commit comments