| 1 | import { GENERIC_MIME_TYPES } from "./image.ts"; |
| 2 | |
| 3 | const VIDEO_MIME_TYPE_BY_EXTENSION = new Map([ |
| 4 | ["avi", "video/x-msvideo"], |
| 5 | ["m4v", "video/mp4"], |
| 6 | ["mkv", "video/x-matroska"], |
| 7 | ["mov", "video/quicktime"], |
| 8 | ["mp4", "video/mp4"], |
| 9 | ["ogv", "video/ogg"], |
| 10 | ["webm", "video/webm"], |
| 11 | ]); |
| 12 | |
| 13 | export const VIDEO_FILE_EXTENSIONS = Object.freeze([...VIDEO_MIME_TYPE_BY_EXTENSION.keys()]); |
| 14 | |
| 15 | /** Recognizes videos even when the file picker omitted their MIME type. */ |
| 16 | export function videoMimeType(attachment: { |
| 17 | readonly name: string; |
| 18 | readonly mimeType: string; |
| 19 | }): string | null { |
| 20 | const mimeType = attachment.mimeType.split(";", 1)[0]?.trim().toLowerCase() ?? ""; |
| 21 | if (mimeType.startsWith("video/")) return mimeType; |
| 22 | // The name is only evidence when nothing recorded what this is. A definite type already |
| 23 | // answers the question, and a `.mp4` on a PDF must not override it. |
| 24 | if (mimeType !== "" && !GENERIC_MIME_TYPES.has(mimeType)) return null; |
| 25 | const dotIndex = attachment.name.lastIndexOf("."); |
| 26 | return dotIndex < 0 |
| 27 | ? null |
| 28 | : (VIDEO_MIME_TYPE_BY_EXTENSION.get(attachment.name.slice(dotIndex + 1).toLowerCase()) ?? null); |
| 29 | } |