Open History to see the last commit
1import { videoMimeType } from "./video.ts";
2
3export const WORKSPACE_BROWSER_PREVIEW_EXTENSIONS = [".htm", ".html", ".pdf"] as const;
4
5export const WORKSPACE_IMAGE_PREVIEW_EXTENSIONS = [
6 ".avif",
7 ".gif",
8 ".ico",
9 ".jpeg",
10 ".jpg",
11 ".png",
12 ".svg",
13 ".webp",
14] as const;
15
16const IMAGE_MIME_TYPE_BY_EXTENSION = new Map([
17 [".avif", "image/avif"],
18 [".gif", "image/gif"],
19 [".ico", "image/x-icon"],
20 [".jpeg", "image/jpeg"],
21 [".jpg", "image/jpeg"],
22 [".png", "image/png"],
23 [".svg", "image/svg+xml"],
24 [".webp", "image/webp"],
25]);
26
27const BROWSER_MIME_TYPE_BY_EXTENSION = new Map([
28 [".htm", "text/html"],
29 [".html", "text/html"],
30 [".pdf", "application/pdf"],
31]);
32
33/** Classifies a literal filesystem extension, without URL decoding or suffix removal. */
34export function mediaMimeTypeFromExtension(extension: string): string | null {
35 if (!/^\.[a-z0-9]+$/i.test(extension)) return null;
36 return (
37 IMAGE_MIME_TYPE_BY_EXTENSION.get(extension.toLowerCase()) ??
38 videoMimeType({ name: `media${extension}`, mimeType: "" })
39 );
40}
41
42/** Files the server serves in place from anywhere on its host: media plus browser documents. */
43export function hostPreviewMimeTypeFromExtension(extension: string): string | null {
44 if (!/^\.[a-z0-9]+$/i.test(extension)) return null;
45 return (
46 mediaMimeTypeFromExtension(extension) ??
47 BROWSER_MIME_TYPE_BY_EXTENSION.get(extension.toLowerCase()) ??
48 null
49 );
50}
51
52/** Classifies an authored media path or URL. Filesystem validation uses the literal extension. */
53export function mediaMimeType(path: string): string | null {
54 const trimmed = path.trim();
55 const source = trimmed.startsWith("<") && trimmed.endsWith(">") ? trimmed.slice(1, -1) : trimmed;
56 const dataMimeType = /^data:((?:image|video)\/[\w.+-]+)[;,]/i.exec(source)?.[1];
57 if (dataMimeType) return dataMimeType.toLowerCase();
58
59 let sourcePath = source.split(/[?#]/, 1)[0] ?? "";
60 if (/^(?:https?:|file:|\/\/)/i.test(source)) {
61 try {
62 sourcePath = new URL(source, "https://media.invalid").pathname;
63 } catch {
64 return null;
65 }
66 }
67 try {
68 sourcePath = decodeURIComponent(sourcePath);
69 } catch {
70 // A literal percent character is valid in a filename.
71 }
72 const basename = sourcePath.split(/[\\/]/).at(-1) ?? "";
73 const extensionIndex = basename.lastIndexOf(".");
74 return extensionIndex < 0 ? null : mediaMimeTypeFromExtension(basename.slice(extensionIndex));
75}
76
77export function mediaKindFromPath(path: string): "image" | "video" | null {
78 const mimeType = mediaMimeType(path);
79 if (mimeType === null) return null;
80 return mimeType.startsWith("video/") ? "video" : "image";
81}
82
83function hasPreviewExtension(path: string, extensions: ReadonlyArray<string>): boolean {
84 const pathWithoutQuery = path.split(/[?#]/, 1)[0]?.toLowerCase() ?? "";
85 return extensions.some((extension) => pathWithoutQuery.endsWith(extension));
86}
87
88export function isWorkspaceBrowserPreviewPath(path: string): boolean {
89 return hasPreviewExtension(path, WORKSPACE_BROWSER_PREVIEW_EXTENSIONS);
90}
91
92export function isWorkspaceImagePreviewPath(path: string): boolean {
93 return hasPreviewExtension(path, WORKSPACE_IMAGE_PREVIEW_EXTENSIONS);
94}
95
96/** File viewers receive literal filesystem paths, not Markdown URLs. */
97export function isWorkspaceVideoPreviewPath(path: string): boolean {
98 return videoMimeType({ name: path, mimeType: "" }) !== null;
99}
100
101export function isWorkspacePreviewEntryPath(path: string): boolean {
102 return isWorkspaceBrowserPreviewPath(path) || isWorkspaceImagePreviewPath(path);
103}