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 "./mediaActions";
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<AssetResource, { readonly _tag: "attachment" | "media-file" }>;
19 }
20);
21
22/** Resolves the current capability without making it the identity of the video. */
23export function mediaVideoPreviewUri(
24 source: MediaVideoPreviewSource,
25 assetUrl: string | null,
26): string | null {
27 if ("uri" in source) return source.uri;
28 return assetUrl === null ? null : assetUrl + (source.srcFragment ?? "");
29}
30
31/** Keeps thumbnails independent of refreshed asset signatures and scoped to their environment. */
32export function mediaVideoThumbnailKey(source: MediaVideoPreviewSource): string {
33 return JSON.stringify(
34 "uri" in source
35 ? ["media-video", source.uri]
36 : source.resource._tag === "attachment"
37 ? ["media-video", source.environmentId, "attachment", source.resource.attachmentId]
38 : [
39 "media-video",
40 source.environmentId,
41 source.resource.threadId,
42 source.resource.path,
43 source.srcFragment ?? "",
44 ],
45 );
46}
47
48export interface LocalVideoPreviewSource {
49 readonly type: "local";
50 readonly attachment: DraftComposerFileAttachment;
51 readonly sourceIdentifier?: string;
52}
53
54export type VideoPreviewSource = LocalVideoPreviewSource | MediaVideoPreviewSource;
55
56export function attachmentVideoPreviewSource(
57 environmentId: EnvironmentId,
58 attachment: ChatFileAttachment,
59 sourceIdentifier?: string,
60): MediaVideoPreviewSource {
61 const mimeType = videoMimeType(attachment) ?? attachment.mimeType;
62 const resource = {
63 _tag: "attachment" as const,
64 attachmentId: attachment.id,
65 fileName: attachment.name,
66 mimeType,
67 };
68 return {
69 type: "media",
70 name: attachment.name,
71 mimeType,
72 ...(sourceIdentifier ? { sourceIdentifier } : {}),
73 environmentId,
74 resource,
75 actionsSource: {
76 name: attachment.name,
77 mimeType,
78 ...(sourceIdentifier ? { sourceIdentifier } : {}),
79 environmentId,
80 resource,
81 },
82 };
83}