Skip to content
Source
Sign inSign up
Open History to see the last commit

video.ts

26 lines942 Bytes

1import { GENERIC_MIME_TYPES } from "./image.ts";
2​
3const 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​
13export const VIDEO_FILE_EXTENSIONS = Object.freeze([...VIDEO_MIME_TYPE_BY_EXTENSION.keys()]);
14​
15export function videoMimeType(attachment: {
16 readonly name: string;
17 readonly mimeType: string;
18}): string | null {
19 const mimeType = attachment.mimeType.split(";", 1)[0]?.trim().toLowerCase() ?? "";
20 if (mimeType.startsWith("video/") && !GENERIC_MIME_TYPES.has(mimeType)) return mimeType;
21 if (mimeType !== "" && !GENERIC_MIME_TYPES.has(mimeType)) return null;
22 const dotIndex = attachment.name.lastIndexOf(".");
23 return dotIndex < 0
24 ? null
25 : (VIDEO_MIME_TYPE_BY_EXTENSION.get(attachment.name.slice(dotIndex + 1).toLowerCase()) ?? null);