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

image.ts

41 lines1.4 KB

1/**
2 * A picture the provider would reject is still a file, so widening this map would
3 * promote attachments the send path cannot carry. Mirrors
4 * `PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES`.
5 */
6const IMAGE_MIME_TYPE_BY_EXTENSION = new Map([
7 ["gif", "image/gif"],
8 ["jpeg", "image/jpeg"],
9 ["jpg", "image/jpeg"],
10 ["png", "image/png"],
11 ["webp", "image/webp"],
12]);
13​
14const SUPPORTED_IMAGE_MIME_TYPES = new Set(IMAGE_MIME_TYPE_BY_EXTENSION.values());
15​
16export const GENERIC_MIME_TYPES = new Set([
17 "application/octet-stream",
18 "binary/octet-stream",
19 "application/unknown",
20]);
21​
22export function imageMimeType(attachment: {
23 readonly name: string;
24 readonly mimeType: string;
25}): string | null {
26 const mimeType = attachment.mimeType.split(";", 1)[0]?.trim().toLowerCase() ?? "";
27 if (SUPPORTED_IMAGE_MIME_TYPES.has(mimeType)) return mimeType;
28 if (mimeType.startsWith("image/")) return null;
29 // The name is only evidence when nothing recorded what this is. A definite type already
30 // answers the question, and a `.png` on a PDF must not override it.
31 if (mimeType !== "" && !GENERIC_MIME_TYPES.has(mimeType)) return null;
32 const dotIndex = attachment.name.lastIndexOf(".");
33 return dotIndex < 0
34 ? null
35 : (IMAGE_MIME_TYPE_BY_EXTENSION.get(
36 attachment.name
37 .slice(dotIndex + 1)
38 .trim()
39 .toLowerCase(),
40 ) ?? null);