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