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

composerAttachmentFiles.ts

303 lines11.1 KB

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";
10import { msg } from "gt-react";
11​
12import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";
13import { m, type MessageResolver } from "../../i18n/messages";
14import { isHeicImageFile } from "../../lib/imageCompression";
15import { isVideoAttachment, videoMimeType } from "../../types";
16import { isSvgAttachment } from "@t3tools/shared/image";
17import { isWorkspaceImagePreviewPath } from "@t3tools/shared/mediaPreview";
18​
19type ComposerAttachmentFileKind = "image" | "file" | "unsupported-image";
20​
21interface FileAttachmentCapabilityState {
22 readonly attachmentUploadsCapabilityKnown: boolean;
23 readonly supportsAttachmentUploads: boolean;
24 readonly maxFileAttachmentBytes: number | null;
26​
27const IMAGE_MIME_TYPE_BY_EXTENSION: Readonly<Record<string, string>> = {
28 gif: "image/gif",
29 jpeg: "image/jpeg",
30 jpg: "image/jpeg",
31 png: "image/png",
32 webp: "image/webp",
33};
34​
35/**
36 * Some sources (drags from other apps, files piped through a shell) hand over a
37 * `File` with an empty or generic MIME type.
38 */
39function inferImageMimeTypeFromName(name: string): string | null {
40 const dotIndex = name.lastIndexOf(".");
41 if (dotIndex <= 0) {
42 return null;
43 }
44 return IMAGE_MIME_TYPE_BY_EXTENSION[name.slice(dotIndex + 1).toLowerCase()] ?? null;
46​
47function inferImageMimeTypeForUnknownFile(file: Pick<File, "name" | "type">): string | null {
48 const mimeType = file.type.toLowerCase();
49 if (mimeType !== "" && mimeType !== "application/octet-stream") {
50 return null;
51 }
52 return inferImageMimeTypeFromName(file.name);
54​
55export function normalizeComposerImageFileMimeType(file: File): File {
56 const inferredMimeType = inferImageMimeTypeForUnknownFile(file);
57 if (!inferredMimeType) {
58 return file;
59 }
60 return new File([file], file.name, {
61 type: inferredMimeType,
62 lastModified: file.lastModified,
63 });
65​
66function composerImageSignature(
67 image: Pick<ComposerImageAttachment, "mimeType" | "sizeBytes" | "name">,
68): string {
69 // Hydrated images get fresh lastModified values, so exclude it from dedupe.
70 return `${image.mimeType}\u0000${image.sizeBytes}\u0000${image.name}`;
72​
73// Byte hashes by file; a file whose bytes were never read gets a key that matches only itself.
74const composerImageFingerprints = new WeakMap<File, string>();
75let unreadComposerImageCount = 0;
76​
77function composerImageFingerprint(file: File): string {
78 let fingerprint = composerImageFingerprints.get(file);
79 if (fingerprint === undefined) {
80 fingerprint = `unread:${unreadComposerImageCount++}`;
81 composerImageFingerprints.set(file, fingerprint);
82 }
83 return fingerprint;
85​
86/** Records the bytes behind an image file, so a draft drops it only as a byte-for-byte repeat. */
87export function rememberComposerImageBytes(file: File, bytes: Uint8Array): void {
88 // cyrb53, synchronous: crypto.subtle is missing on plain-http remote origins.
89 let first = 0xdeadbeef;
90 let second = 0x41c6ce57;
91 for (let index = 0; index < bytes.length; index++) {
92 const byte = bytes[index]!;
93 first = Math.imul(first ^ byte, 2654435761);
94 second = Math.imul(second ^ byte, 1597334677);
95 }
96 first = Math.imul(first ^ (first >>> 16), 2246822507);
97 first ^= Math.imul(second ^ (second >>> 13), 3266489909);
98 second = Math.imul(second ^ (second >>> 16), 2246822507);
99 second ^= Math.imul(first ^ (first >>> 13), 3266489909);
100 composerImageFingerprints.set(file, `${second >>> 0}:${first >>> 0}`);
102​
103export async function readComposerImageBytes(file: File): Promise<void> {
104 // An unreadable file keeps matching only itself.
105 const bytes = await file.arrayBuffer().catch(() => null);
106 if (bytes) rememberComposerImageBytes(file, new Uint8Array(bytes));
108​
109/**
110 * Matches name, type, size and bytes; unread files match only themselves.
111 * Producers must await readComposerImageBytes before publishing images to the draft.
112 */
113export function composerImageDedupKey(
114 image: Pick<ComposerImageAttachment, "mimeType" | "sizeBytes" | "name" | "file">,
115): string {
116 return `${composerImageSignature(image)}\u0000${composerImageFingerprint(image.file)}`;
118​
119// Compression changes an image's bytes, so a compressed image answers for the file it came from.
120const composerImageSourceKeys = new WeakMap<File, string>();
121​
122/** A pasted image's name, type and size: a guess, before its bytes are read, that the draft has it. */
123export function composerImageSourceKey(file: File): string {
124 return (
125 composerImageSourceKeys.get(file) ??
126 composerImageSignature({
127 mimeType: file.type,
128 sizeBytes: file.size,
129 name: file.name || "image",
130 })
131 );
133​
134export function rememberComposerImageSource(source: File, prepared: File): void {
135 if (prepared !== source) composerImageSourceKeys.set(prepared, composerImageSourceKey(source));
137​
138function isDngFile(file: Pick<File, "name" | "type">): boolean {
139 const mimeType = file.type.split(";", 1)[0]?.trim().toLowerCase();
140 return (
141 file.name.toLowerCase().endsWith(".dng") ||
142 mimeType === "image/dng" ||
143 mimeType === "image/x-dng" ||
144 mimeType === "image/x-adobe-dng"
145 );
147​
148export function composerFileAttachmentMimeType(file: Pick<File, "name" | "type">): string {
149 // Downstream image detection uses MIME, even for attachments staged as files.
150 if (isDngFile(file)) return "image/x-adobe-dng";
151 if (isSvgAttachment({ name: file.name, mimeType: file.type })) return "image/svg+xml";
152 return (
153 videoMimeType({ name: file.name, mimeType: file.type }) ??
154 (file.type || "application/octet-stream")
155 );
157​
158export function classifyComposerAttachmentFile(
159 file: Pick<File, "name" | "type">,
160): ComposerAttachmentFileKind {
161 if (isSvgAttachment({ name: file.name, mimeType: file.type })) return "file";
162 if (isDngFile(file)) return "file";
163 if (isHeicImageFile(file)) {
164 return "image";
165 }
166 if (inferImageMimeTypeForUnknownFile(file)) {
167 return "image";
168 }
169 if (!file.type.toLowerCase().startsWith("image/")) {
170 return "file";
171 }
172 if (isProviderSendTurnSupportedImageMimeType(file.type)) return "image";
173 // These can be previewed as files, but providers must not receive them as images.
174 return isWorkspaceImagePreviewPath(file.name) ? "file" : "unsupported-image";
176​
177export function isPreviewableComposerVideo(
178 file: ComposerFileAttachment,
179 environmentId: EnvironmentId,
180): boolean {
181 return (
182 isVideoAttachment(file) &&
183 (file.file !== null ||
184 (file.uploadedAttachmentId !== undefined && file.uploadEnvironmentId === environmentId))
185 );
187​
188export function composerOtherFilesForPresentation(
189 files: ReadonlyArray<ComposerFileAttachment>,
190 environmentId: EnvironmentId,
191 inlineFileIds: ReadonlySet<string>,
192): ComposerFileAttachment[] {
193 return files.filter(
194 (file) => !isPreviewableComposerVideo(file, environmentId) && !inlineFileIds.has(file.id),
195 );
197​
198export function fileAttachmentStagingLimit(input: FileAttachmentCapabilityState): number | null {
199 if (!input.attachmentUploadsCapabilityKnown) {
200 return PROVIDER_SEND_TURN_MAX_FILE_BYTES;
201 }
202 if (!input.supportsAttachmentUploads || input.maxFileAttachmentBytes === null) {
203 return null;
204 }
205 return clampFileAttachmentUploadBytes(input.maxFileAttachmentBytes);
207​
208export function fileAttachmentCapabilityBlockReason(
209 input: FileAttachmentCapabilityState & {
210 readonly files: ReadonlyArray<{ readonly name: string; readonly sizeBytes: number }>;
211 },
212 resolve: MessageResolver = m,
213): string | null {
214 if (input.files.length === 0) {
215 return null;
216 }
217 if (!input.attachmentUploadsCapabilityKnown) {
218 return resolve(msg("Waiting for the server before file attachments can send"));
219 }
220 const maxFileAttachmentBytes = fileAttachmentStagingLimit(input);
221 if (maxFileAttachmentBytes === null) {
222 return resolve(
223 msg("This server does not accept file attachments right now. Remove the files to send."),
224 );
225 }
226 const oversizedFile = input.files.find((file) => file.sizeBytes > maxFileAttachmentBytes);
227 if (oversizedFile) {
228 return fileAttachmentTooLargeMessage(oversizedFile.name, maxFileAttachmentBytes, resolve);
229 }
230 return null;
232​
233/** Release only unpersisted uploads; hydrated files still need their server copies. */
234export function attachmentsToReleaseOnUploadCapabilityLoss(
235 attachments: ReadonlyArray<ComposerImageAttachment | ComposerFileAttachment>,
236): Array<ComposerImageAttachment | ComposerFileAttachment> {
237 return attachments.filter(
238 (attachment) => !(attachment.type === "file" && attachment.uploadedAttachmentId !== undefined),
239 );
241​
242function isOnlyFileNames(text: string, files: ReadonlyArray<File>): boolean {
243 const names = new Set(files.map((file) => file.name));
244 return text
245 .split(/\r?\n/)
246 .filter((line) => line.length > 0)
247 .every((line) => names.has(line));
249​
250/**
251 * A browser's Copy Image puts only an <img> in text/html; a chat message (QQ, WeChat) puts its text
252 * beside the <img>; rich text sources (Word, Pages) put only the text there.
253 */
254export function clipboardHtmlImageContent(html: string): "only-image" | "image-and-text" | "none" {
255 if (html.length === 0) return "none";
256 const body = new DOMParser().parseFromString(html, "text/html").body;
257 if (body.querySelector("img") === null) return "none";
258 return (body.textContent ?? "").trim().length === 0 ? "only-image" : "image-and-text";
260​
261function isPastedImageFile(file: File): boolean {
262 const classification = classifyComposerAttachmentFile(file);
263 return classification === "image" || classification === "unsupported-image";
265​
266/** The images of a message copied with its text: they attach while the text pastes as text. */
267export function imagesToAttachBesidePastedText(input: {
268 readonly files: ReadonlyArray<File>;
269 readonly plainText: string;
270 readonly bypassAutoAttachment?: boolean;
271 readonly htmlHasImageAndText: boolean;
272}): File[] {
273 if (input.bypassAutoAttachment || input.plainText.length === 0 || !input.htmlHasImageAndText) {
274 return [];
275 }
276 return input.files.filter(isPastedImageFile);
278​
279export function shouldHandleComposerAttachmentPaste(input: {
280 readonly files: ReadonlyArray<File>;
281 readonly plainText: string;
282 readonly bypassAutoAttachment?: boolean;
283 readonly htmlIsOnlyImage?: boolean;
284}): boolean {
285 // Paste as Text keeps the text even when the source also put an image of it on the clipboard.
286 if (input.bypassAutoAttachment && input.plainText.length > 0) return false;
287 if (
288 input.files.some(
289 (file) => isDngFile(file) || isSvgAttachment({ name: file.name, mimeType: file.type }),
290 )
291 ) {
292 return true;
293 }
294 const hasImage = input.files.some(isPastedImageFile);
295 if (input.plainText.length === 0) {
296 return hasImage || input.files.some((file) => classifyComposerAttachmentFile(file) === "file");
297 }
298 // Rich text sources (Word, Pages) add a picture of the text. Copy Image adds the image URL as
299 // text beside an <img>, and a Finder copy adds the file names.
300 return (
301 hasImage && (input.htmlIsOnlyImage === true || isOnlyFileNames(input.plainText, input.files))
302 );