Open History to see the last commit
1import type { AssetResource, ThreadId } from "@t3tools/contracts";
2import { mediaMimeType, mediaMimeTypeFromExtension } from "@t3tools/shared/filePreview";
3
4import {
5 classifyMarkdownImageSource,
6 markdownImageSourceFragment,
7 type MarkdownImageSource,
8} from "./markdownImages.ts";
9import {
10 fileBasename,
11 splitFilePathPosition,
12 splitMarkdownLinkSearchAndHash,
13} from "./markdownLinks.ts";
14import {
15 mediaFileReference,
16 mediaReferenceFileName,
17 mediaUrlReference,
18 type MediaReference,
19} from "./mediaReference.ts";
20
21export type MediaSourceResource = Extract<AssetResource, { readonly _tag: "media-file" }>;
22
23/** What a piece of authored media is and how its bytes can be reached. */
24export type ResolvedMediaSource = {
25 readonly kind: "image" | "video";
26 readonly mimeType: string;
27 readonly name: string;
28 readonly reference?: MediaReference;
29 readonly srcFragment: string;
30} & (
31 | {
32 readonly access: "direct";
33 readonly uri: string;
34 }
35 | {
36 readonly access: "environment";
37 readonly resource: MediaSourceResource;
38 }
39 | {
40 readonly access: "unavailable";
41 }
42);
43
44export interface ResolveMediaSourceInput {
45 readonly threadId: ThreadId | undefined;
46 readonly workspaceRoot?: string | null | undefined;
47 readonly resolvedFilePath?: string | undefined;
48 /** Image syntax can target an endpoint without a recognizable extension. */
49 readonly imageEmbed?: boolean | undefined;
50}
51
52function classify(source: string, input: ResolveMediaSourceInput): MarkdownImageSource {
53 return input.resolvedFilePath === undefined
54 ? classifyMarkdownImageSource(source, input.workspaceRoot)
55 : { _tag: "WorkspaceFile", path: input.resolvedFilePath };
56}
57
58export function resolveMediaSource(
59 source: string,
60 input: ResolveMediaSourceInput,
61): ResolvedMediaSource | null {
62 const classified = classify(source, input);
63 if (classified._tag === "Blocked") return null;
64
65 const path =
66 classified._tag === "Direct"
67 ? splitMarkdownLinkSearchAndHash(classified.uri).path
68 : splitFilePathPosition(classified.path).path;
69 // Local paths have already been decoded. Do not interpret literal #, ?, or % characters again.
70 const basename = fileBasename(path);
71 const extensionIndex = basename.lastIndexOf(".");
72 const detectedMimeType =
73 classified._tag === "Direct"
74 ? mediaMimeType(classified.uri)
75 : extensionIndex < 0
76 ? null
77 : mediaMimeTypeFromExtension(basename.slice(extensionIndex));
78 const mimeType = detectedMimeType ?? (input.imageEmbed ? "image/*" : null);
79 if (mimeType === null) return null;
80 const kind = mimeType.startsWith("video/") ? "video" : "image";
81
82 const reference =
83 classified._tag === "Direct"
84 ? mediaUrlReference(classified.uri)
85 : mediaFileReference(path, input.workspaceRoot);
86 const name = (reference && mediaReferenceFileName(reference)) || basename || kind;
87 const common = {
88 kind,
89 mimeType,
90 name,
91 ...(reference ? { reference } : {}),
92 srcFragment: markdownImageSourceFragment(source),
93 } as const;
94
95 if (classified._tag === "Direct") {
96 return { ...common, access: "direct", uri: classified.uri };
97 }
98 if (input.threadId === undefined) return { ...common, access: "unavailable" };
99 return {
100 ...common,
101 access: "environment",
102 resource: { _tag: "media-file", threadId: input.threadId, path },
103 };
104}