Open History to see the last commit
1import type { AssetResource, ChatFileAttachment, EnvironmentId } from "@t3tools/contracts";
2import { videoMimeType } from "@t3tools/shared/video";
3
4import type { DraftComposerFileAttachment } from "./composerImages";
5import type { MediaActionsSource } from "./mediaActionsSource";
6
7export type MediaVideoPreviewSource = {
8 readonly type: "media";
9 readonly name: string;
10 readonly mimeType: string;
11 readonly sourceIdentifier?: string;
12 readonly srcFragment?: string;
13 readonly actionsSource?: MediaActionsSource;
14} & (
15 | { readonly uri: string }
16 | {
17 readonly environmentId: EnvironmentId;
18 readonly resource: Extract<
19 AssetResource,
20 { readonly _tag: "attachment" | "media-file" | "draft-workspace-file" }
21 >;
22 }
23);
24
25/** Resolves the current capability without making it the identity of the video. */
26export function mediaVideoPreviewUri(
27 source: MediaVideoPreviewSource,
28 assetUrl: string | null,
29): string | null {
30 if ("uri" in source) return source.uri;
31 return assetUrl === null ? null : assetUrl + (source.srcFragment ?? "");
32}
33
34/** Keeps thumbnails independent of refreshed asset signatures and scoped to their environment. */
35export function mediaVideoThumbnailKey(source: MediaVideoPreviewSource): string {
36 return JSON.stringify(
37 "uri" in source
38 ? ["media-video", source.uri]
39 : source.resource._tag === "attachment"
40 ? ["media-video", source.environmentId, "attachment", source.resource.attachmentId]
41 : source.resource._tag === "media-file"
42 ? [
43 "media-video",
44 "media-file",
45 source.environmentId,
46 source.resource.threadId,
47 source.resource.path,
48 source.srcFragment ?? "",
49 ]
50 : [
51 "media-video",
52 "draft-workspace-file",
53 source.environmentId,
54 source.resource.cwd,
55 source.resource.path,
56 source.srcFragment ?? "",
57 ],
58 );
59}
60
61export interface LocalVideoPreviewSource {
62 readonly type: "local";
63 readonly attachment: DraftComposerFileAttachment;
64 readonly sourceIdentifier?: string;
65}
66
67export type VideoPreviewSource = LocalVideoPreviewSource | MediaVideoPreviewSource;
68
69export function attachmentVideoPreviewSource(
70 environmentId: EnvironmentId,
71 attachment: ChatFileAttachment,
72 sourceIdentifier?: string,
73): MediaVideoPreviewSource {
74 const mimeType = videoMimeType(attachment) ?? attachment.mimeType;
75 const resource = {
76 _tag: "attachment" as const,
77 attachmentId: attachment.id,
78 fileName: attachment.name,
79 mimeType,
80 };
81 return {
82 type: "media",
83 name: attachment.name,
84 mimeType,
85 ...(sourceIdentifier ? { sourceIdentifier } : {}),
86 environmentId,
87 resource,
88 actionsSource: {
89 name: attachment.name,
90 mimeType,
91 ...(sourceIdentifier ? { sourceIdentifier } : {}),
92 environmentId,
93 resource,
94 },
95 };
96}