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