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