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

videoPreviewSource.ts

113 lines3.2 KB

1import {
2 SOURCE_CONTROL_MEDIA_ASSET,
3 type AssetResource,
4 type ChatFileAttachment,
5 type EnvironmentId,
6} from "@t3tools/contracts";
7import { videoMimeType } from "@t3tools/shared/video";
8​
9import type { DraftComposerFileAttachment } from "./composerImages";
10import type { MediaActionsSource } from "./mediaActionsSource";
11​
12export type MediaVideoPreviewSource = {
13 readonly type: "media";
14 readonly name: string;
15 readonly mimeType: string;
16 readonly sourceIdentifier?: string;
17 readonly srcFragment?: string;
18 readonly actionsSource?: MediaActionsSource;
19} & (
20 | { readonly uri: string }
21 | {
22 readonly environmentId: EnvironmentId;
23 readonly resource: Extract<
24 AssetResource,
25 {
26 readonly _tag:
27 | "attachment"
28 | "media-file"
29 | "draft-workspace-file"
30 | typeof SOURCE_CONTROL_MEDIA_ASSET;
31 }
32 >;
33 }
34);
35​
36export function mediaVideoPreviewUri(
37 source: MediaVideoPreviewSource,
38 assetUrl: string | null,
39): string | null {
40 if ("uri" in source) return source.uri;
41 return assetUrl === null ? null : assetUrl + (source.srcFragment ?? "");
43​
44export function mediaVideoThumbnailKey(source: MediaVideoPreviewSource): string {
45 return JSON.stringify(
46 "uri" in source
47 ? ["media-video", source.uri]
48 : source.resource._tag === "attachment"
49 ? ["media-video", source.environmentId, "attachment", source.resource.attachmentId]
50 : source.resource._tag === "media-file"
51 ? [
52 "media-video",
53 "media-file",
54 source.environmentId,
55 source.resource.threadId,
56 source.resource.path,
57 source.srcFragment ?? "",
58 ]
59 : source.resource._tag === SOURCE_CONTROL_MEDIA_ASSET
60 ? [
61 "media-video",
62 SOURCE_CONTROL_MEDIA_ASSET,
63 source.environmentId,
64 source.resource.cwd,
65 source.resource.url,
66 ]
67 : [
68 "media-video",
69 "draft-workspace-file",
70 source.environmentId,
71 source.resource.cwd,
72 source.resource.path,
73 source.srcFragment ?? "",
74 ],
75 );
77​
78export interface LocalVideoPreviewSource {
79 readonly type: "local";
80 readonly attachment: DraftComposerFileAttachment;
81 readonly sourceIdentifier?: string;
83​
84export type VideoPreviewSource = LocalVideoPreviewSource | MediaVideoPreviewSource;
85​
86export function attachmentVideoPreviewSource(
87 environmentId: EnvironmentId,
88 attachment: ChatFileAttachment,
89 sourceIdentifier?: string,
90): MediaVideoPreviewSource {
91 const mimeType = videoMimeType(attachment) ?? attachment.mimeType;
92 const resource = {
93 _tag: "attachment" as const,
94 attachmentId: attachment.id,
95 fileName: attachment.name,
96 mimeType,
97 };
98 return {
99 type: "media",
100 name: attachment.name,
101 mimeType,
102 ...(sourceIdentifier ? { sourceIdentifier } : {}),
103 environmentId,
104 resource,
105 actionsSource: {
106 name: attachment.name,
107 mimeType,
108 ...(sourceIdentifier ? { sourceIdentifier } : {}),
109 environmentId,
110 resource,
111 },
112 };