Open History to see the last commit
1import {
2 type EnvironmentId,
3 isProviderSendTurnSupportedImageMimeType,
4 PROVIDER_SEND_TURN_MAX_FILE_BYTES,
5} from "@t3tools/contracts";
6import {
7 clampFileAttachmentUploadBytes,
8 fileAttachmentTooLargeMessage,
9} from "@t3tools/client-runtime/state/attachments";
10
11import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";
12import { isHeicImageFile } from "../../lib/imageCompression";
13import { isVideoAttachment, videoMimeType } from "../../types";
14import { isSvgAttachment } from "@t3tools/shared/image";
15import { isWorkspaceImagePreviewPath } from "@t3tools/shared/mediaPreview";
16
17type ComposerAttachmentFileKind = "image" | "file" | "unsupported-image";
18
19interface FileAttachmentCapabilityState {
20 readonly attachmentUploadsCapabilityKnown: boolean;
21 readonly supportsAttachmentUploads: boolean;
22 readonly maxFileAttachmentBytes: number | null;
23}
24
25const IMAGE_MIME_TYPE_BY_EXTENSION: Readonly<Record<string, string>> = {
26 gif: "image/gif",
27 jpeg: "image/jpeg",
28 jpg: "image/jpeg",
29 png: "image/png",
30 webp: "image/webp",
31};
32
33/**
34 * Some sources (drags from other apps, files piped through a shell) hand over a
35 * `File` with an empty or generic MIME type.
36 */
37function inferImageMimeTypeFromName(name: string): string | null {
38 const dotIndex = name.lastIndexOf(".");
39 if (dotIndex <= 0) {
40 return null;
41 }
42 return IMAGE_MIME_TYPE_BY_EXTENSION[name.slice(dotIndex + 1).toLowerCase()] ?? null;
43}
44
45function inferImageMimeTypeForUnknownFile(file: Pick<File, "name" | "type">): string | null {
46 const mimeType = file.type.toLowerCase();
47 if (mimeType !== "" && mimeType !== "application/octet-stream") {
48 return null;
49 }
50 return inferImageMimeTypeFromName(file.name);
51}
52
53export function normalizeComposerImageFileMimeType(file: File): File {
54 const inferredMimeType = inferImageMimeTypeForUnknownFile(file);
55 if (!inferredMimeType) {
56 return file;
57 }
58 return new File([file], file.name, {
59 type: inferredMimeType,
60 lastModified: file.lastModified,
61 });
62}
63
64function isDngFile(file: Pick<File, "name" | "type">): boolean {
65 const mimeType = file.type.split(";", 1)[0]?.trim().toLowerCase();
66 return (
67 file.name.toLowerCase().endsWith(".dng") ||
68 mimeType === "image/dng" ||
69 mimeType === "image/x-dng" ||
70 mimeType === "image/x-adobe-dng"
71 );
72}
73
74export function composerFileAttachmentMimeType(file: Pick<File, "name" | "type">): string {
75 // Downstream image detection uses MIME, even for attachments staged as files.
76 if (isDngFile(file)) return "image/x-adobe-dng";
77 if (isSvgAttachment({ name: file.name, mimeType: file.type })) return "image/svg+xml";
78 return (
79 videoMimeType({ name: file.name, mimeType: file.type }) ??
80 (file.type || "application/octet-stream")
81 );
82}
83
84export function classifyComposerAttachmentFile(
85 file: Pick<File, "name" | "type">,
86): ComposerAttachmentFileKind {
87 if (isSvgAttachment({ name: file.name, mimeType: file.type })) return "file";
88 if (isDngFile(file)) return "file";
89 if (isHeicImageFile(file)) {
90 return "image";
91 }
92 if (inferImageMimeTypeForUnknownFile(file)) {
93 return "image";
94 }
95 if (!file.type.toLowerCase().startsWith("image/")) {
96 return "file";
97 }
98 if (isProviderSendTurnSupportedImageMimeType(file.type)) return "image";
99 // These can be previewed as files, but providers must not receive them as images.
100 return isWorkspaceImagePreviewPath(file.name) ? "file" : "unsupported-image";
101}
102
103export function isPreviewableComposerVideo(
104 file: ComposerFileAttachment,
105 environmentId: EnvironmentId,
106): boolean {
107 return (
108 isVideoAttachment(file) &&
109 (file.file !== null ||
110 (file.uploadedAttachmentId !== undefined && file.uploadEnvironmentId === environmentId))
111 );
112}
113
114export function composerOtherFilesForPresentation(
115 files: ReadonlyArray<ComposerFileAttachment>,
116 environmentId: EnvironmentId,
117 inlineFileIds: ReadonlySet<string>,
118): ComposerFileAttachment[] {
119 return files.filter(
120 (file) => !isPreviewableComposerVideo(file, environmentId) && !inlineFileIds.has(file.id),
121 );
122}
123
124export function fileAttachmentStagingLimit(input: FileAttachmentCapabilityState): number | null {
125 if (!input.attachmentUploadsCapabilityKnown) {
126 return PROVIDER_SEND_TURN_MAX_FILE_BYTES;
127 }
128 if (!input.supportsAttachmentUploads || input.maxFileAttachmentBytes === null) {
129 return null;
130 }
131 return clampFileAttachmentUploadBytes(input.maxFileAttachmentBytes);
132}
133
134export function fileAttachmentCapabilityBlockReason(
135 input: FileAttachmentCapabilityState & {
136 readonly files: ReadonlyArray<{ readonly name: string; readonly sizeBytes: number }>;
137 },
138): string | null {
139 if (input.files.length === 0) {
140 return null;
141 }
142 if (!input.attachmentUploadsCapabilityKnown) {
143 return "Waiting for the server before file attachments can send";
144 }
145 const maxFileAttachmentBytes = fileAttachmentStagingLimit(input);
146 if (maxFileAttachmentBytes === null) {
147 return "This server does not accept file attachments right now. Remove the files to send.";
148 }
149 const oversizedFile = input.files.find((file) => file.sizeBytes > maxFileAttachmentBytes);
150 if (oversizedFile) {
151 return fileAttachmentTooLargeMessage(oversizedFile.name, maxFileAttachmentBytes);
152 }
153 return null;
154}
155
156/** Release only unpersisted uploads; hydrated files still need their server copies. */
157export function attachmentsToReleaseOnUploadCapabilityLoss(
158 attachments: ReadonlyArray<ComposerImageAttachment | ComposerFileAttachment>,
159): Array<ComposerImageAttachment | ComposerFileAttachment> {
160 return attachments.filter(
161 (attachment) => !(attachment.type === "file" && attachment.uploadedAttachmentId !== undefined),
162 );
163}
164
165export function shouldHandleComposerAttachmentPaste(input: {
166 readonly files: ReadonlyArray<File>;
167 readonly plainText: string;
168}): boolean {
169 if (
170 input.files.some((file) => {
171 if (isDngFile(file)) return true;
172 const classification = classifyComposerAttachmentFile(file);
173 return (
174 classification === "image" ||
175 classification === "unsupported-image" ||
176 isSvgAttachment({ name: file.name, mimeType: file.type })
177 );
178 })
179 ) {
180 return true;
181 }
182
183 if (input.plainText.length > 0) {
184 return false;
185 }
186
187 return input.files.some((file) => classifyComposerAttachmentFile(file) === "file");
188}