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 } 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 */
35export function 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
62export function classifyComposerAttachmentFile(
63 file: Pick<File, "name" | "type">,
64): ComposerAttachmentFileKind {
65 if (isHeicImageFile(file)) {
66 return "image";
67 }
68 if (inferImageMimeTypeForUnknownFile(file)) {
69 return "image";
70 }
71 if (!file.type.toLowerCase().startsWith("image/")) {
72 return "file";
73 }
74 return isProviderSendTurnSupportedImageMimeType(file.type) ? "image" : "unsupported-image";
75}
76
77export function isPreviewableComposerVideo(
78 file: ComposerFileAttachment,
79 environmentId: EnvironmentId,
80): boolean {
81 return (
82 isVideoAttachment(file) &&
83 (file.file !== null ||
84 (file.uploadedAttachmentId !== undefined && file.uploadEnvironmentId === environmentId))
85 );
86}
87
88export function composerOtherFilesForPresentation(
89 files: ReadonlyArray<ComposerFileAttachment>,
90 environmentId: EnvironmentId,
91 inlineFileIds: ReadonlySet<string>,
92): ComposerFileAttachment[] {
93 return files.filter(
94 (file) => !isPreviewableComposerVideo(file, environmentId) && !inlineFileIds.has(file.id),
95 );
96}
97
98export function fileAttachmentStagingLimit(input: FileAttachmentCapabilityState): number | null {
99 if (!input.attachmentUploadsCapabilityKnown) {
100 return PROVIDER_SEND_TURN_MAX_FILE_BYTES;
101 }
102 if (!input.supportsAttachmentUploads || input.maxFileAttachmentBytes === null) {
103 return null;
104 }
105 return clampFileAttachmentUploadBytes(input.maxFileAttachmentBytes);
106}
107
108export function fileAttachmentCapabilityBlockReason(
109 input: FileAttachmentCapabilityState & {
110 readonly files: ReadonlyArray<{ readonly name: string; readonly sizeBytes: number }>;
111 },
112): string | null {
113 if (input.files.length === 0) {
114 return null;
115 }
116 if (!input.attachmentUploadsCapabilityKnown) {
117 return "Waiting for the server before file attachments can send";
118 }
119 const maxFileAttachmentBytes = fileAttachmentStagingLimit(input);
120 if (maxFileAttachmentBytes === null) {
121 return "This server does not accept file attachments right now. Remove the files to send.";
122 }
123 const oversizedFile = input.files.find((file) => file.sizeBytes > maxFileAttachmentBytes);
124 if (oversizedFile) {
125 return fileAttachmentTooLargeMessage(oversizedFile.name, maxFileAttachmentBytes);
126 }
127 return null;
128}
129
130/**
131 * When `capabilities.attachmentUploads` flips off (reconnect, version skew),
132 * tear down only uploads that have not been persisted onto a draft file.
133 * Once `uploadedAttachmentId` is stamped, the draft references that server
134 * copy after reload even if its local `File` is still available in memory.
135 */
136export function attachmentsToReleaseOnUploadCapabilityLoss(
137 attachments: ReadonlyArray<ComposerImageAttachment | ComposerFileAttachment>,
138): Array<ComposerImageAttachment | ComposerFileAttachment> {
139 return attachments.filter(
140 (attachment) => !(attachment.type === "file" && attachment.uploadedAttachmentId !== undefined),
141 );
142}
143
144export function shouldHandleComposerAttachmentPaste(input: {
145 readonly files: ReadonlyArray<File>;
146 readonly plainText: string;
147}): boolean {
148 if (
149 input.files.some((file) => {
150 const classification = classifyComposerAttachmentFile(file);
151 return classification === "image" || classification === "unsupported-image";
152 })
153 ) {
154 return true;
155 }
156
157 if (input.plainText.length > 0) {
158 return false;
159 }
160
161 return input.files.some((file) => classifyComposerAttachmentFile(file) === "file");
162}