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

mediaReference.ts

95 lines3.1 KB

1import { isWindowsAbsolutePath } from "@t3tools/shared/path";
2​
3/** The authored media location, never the temporary URL used to load its bytes. */
4export type MediaReference =
5 | {
6 readonly kind: "file";
7 readonly path: string;
8 readonly relativePath?: string;
9 }
10 | { readonly kind: "url"; readonly url: string };
11​
12function absolutePathParts(path: string) {
13 const windows = isWindowsAbsolutePath(path) || path.startsWith("//");
14 const normalized = windows ? path.replaceAll("\\", "/") : path;
15 const prefix = windows
16 ? /^(?:[a-z]:\/|\/\/[^/]+\/[^/]+(?:\/|$))/i.exec(normalized)?.[0]
17 : normalized.startsWith("/")
18 ? "/"
19 : undefined;
20 if (!prefix) return undefined;
21​
22 const segments: string[] = [];
23 for (const segment of normalized.slice(prefix.length).split("/")) {
24 if (!segment || segment === ".") continue;
25 if (segment === "..") segments.pop();
26 else segments.push(segment);
27 }
28 const root = prefix.replace(/\/$/, "");
29 return { root: windows ? root.toLowerCase() : root, segments, windows };
31​
32/** Compares paths lexically for the copy menu; it does not resolve filesystem symlinks. */
33export function mediaFileReference(
34 path: string,
35 workspaceRoot?: string | null,
36): Extract<MediaReference, { kind: "file" }> {
37 const target = absolutePathParts(path);
38 const workspace = workspaceRoot ? absolutePathParts(workspaceRoot) : undefined;
39 if (
40 !target ||
41 !workspace ||
42 target.windows !== workspace.windows ||
43 target.root !== workspace.root ||
44 target.segments.length <= workspace.segments.length ||
45 !workspace.segments.every((segment, index) =>
46 workspace.windows
47 ? segment.toLowerCase() === target.segments[index]?.toLowerCase()
48 : segment === target.segments[index],
49 )
50 ) {
51 return { kind: "file", path };
52 }
53 return {
54 kind: "file",
55 path,
56 relativePath: target.segments.slice(workspace.segments.length).join("/"),
57 };
59​
60/** Pass the authored source, not a generated URL used by the media player. */
61export function mediaUrlReference(
62 url: string,
63): Extract<MediaReference, { kind: "url" }> | undefined {
64 if (!/^(?:https?:\/\/|\/\/)/i.test(url)) return undefined;
65 try {
66 const parsed = new URL(url.startsWith("//") ? `https:${url}` : url);
67 return parsed.protocol === "http:" || parsed.protocol === "https:"
68 ? { kind: "url", url }
69 : undefined;
70 } catch {
71 return undefined;
72 }
74​
75/** Local paths are already decoded; URL filename escapes are decoded exactly once. */
76export function mediaReferenceFileName(reference: MediaReference): string | undefined {
77 if (reference.kind === "file") {
78 const windows = isWindowsAbsolutePath(reference.path) || reference.path.startsWith("//");
79 return reference.path.split(windows ? /[\\/]/ : "/").at(-1) || undefined;
80 }
81​
82 let basename: string | undefined;
83 try {
84 const url = reference.url;
85 basename = new URL(url.startsWith("//") ? `https:${url}` : url).pathname.split("/").at(-1);
86 } catch {
87 return undefined;
88 }
89 if (!basename) return undefined;
90 try {
91 return decodeURIComponent(basename);
92 } catch {
93 return basename;
94 }