TypeScript
fix(web): attach DNG originals as generic files (#24625)
Julius Marminge14b828e58e74
diff --git a/apps/web/src/components/chat/composerAttachmentFiles.test.ts b/apps/web/src/components/chat/composerAttachmentFiles.test.tsindex a32e8e985c9..39477442bd2 100644--- a/apps/web/src/components/chat/composerAttachmentFiles.test.ts+++ b/apps/web/src/components/chat/composerAttachmentFiles.test.ts@@ -1,15 +1,90 @@-import { EnvironmentId, PROVIDER_SEND_TURN_MAX_FILE_BYTES } from "@t3tools/contracts";+import {+ EnvironmentId,+ getProviderAttachmentLimitError,+ PROVIDER_SEND_TURN_MAX_FILE_BYTES,+} from "@t3tools/contracts";import { describe, expect, it } from "vite-plus/test";+import { isFileAttachment, isImageAttachment } from "../../types";import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";import {attachmentsToReleaseOnUploadCapabilityLoss,+ classifyComposerAttachmentFile,+ composerFileAttachmentMimeType,fileAttachmentCapabilityBlockReason,fileAttachmentStagingLimit,+ normalizeComposerImageFileMimeType,shouldHandleComposerAttachmentPaste,} from "./composerAttachmentFiles";describe("composer attachment files", () => {+ it("keeps DNG originals on the file path without changing their bytes", async () => {+ const bytes = new Uint8Array([0x49, 0x49, 0x2a, 0x00, 0x08, 0, 0, 0, 0xfe, 0xff, 0x00, 0x80]);+ const files = [+ new File([bytes], "photo.DNG", { type: "image/tiff" }),+ new File([bytes], "photo.dng", { type: "image/png" }),+ new File([bytes], "photo.dng", { type: "image/jpeg" }),+ new File([bytes], "clipboard", { type: "image/dng" }),+ new File([bytes], "clipboard", { type: "image/x-dng" }),+ new File([bytes], "clipboard", { type: "image/x-adobe-dng" }),+ ];++ const attachments = await Promise.all(+ files.map(async (file) => ({+ kind: classifyComposerAttachmentFile(file),+ bytes: new Uint8Array(await normalizeComposerImageFileMimeType(file).arrayBuffer()),+ })),+ );++ expect(attachments).toEqual(files.map(() => ({ kind: "file", bytes })));+ });++ it("keeps misleading DNG MIME labels out of downstream image detection and budgets", () => {+ for (const type of ["image/png", "image/jpeg", "image/tiff", "image/dng", ""]) {+ const attachment = {+ type: "file" as const,+ id: "raw",+ name: "photo.DNG",+ mimeType: composerFileAttachmentMimeType({ name: "photo.DNG", type }),+ sizeBytes: 81 * 1024 * 1024,+ };++ expect(isImageAttachment(attachment)).toBe(false);+ expect(isFileAttachment(attachment)).toBe(true);+ expect(getProviderAttachmentLimitError([attachment])).toBeUndefined();+ }+ });++ it("claims a captioned DNG paste even when its MIME is generic", () => {+ const file = new File(["raw"], "photo.dng", { type: "application/octet-stream" });++ expect(+ shouldHandleComposerAttachmentPaste({ files: [file], plainText: "Inspect this original" }),+ ).toBe(true);+ });++ it("keeps ordinary images on their existing paths", () => {+ const files = [+ { name: "photo.jpg", type: "image/jpeg" },+ { name: "export.dng.png", type: "image/png" },+ { name: "photo.webp", type: "image/webp" },+ { name: "photo.gif", type: "image/gif" },+ { name: "photo.heic", type: "" },+ { name: "scan.tif", type: "image/tiff" },+ { name: "photo.cr2", type: "image/x-canon-cr2" },+ ];++ expect(files.map(classifyComposerAttachmentFile)).toEqual([+ "image",+ "image",+ "image",+ "image",+ "image",+ "unsupported-image",+ "unsupported-image",+ ]);+ });+it("preserves text paste when an application adds a synthetic generic file", () => {const file = new File(["clipboard"], "clipboard.rtf", { type: "application/rtf" });diff --git a/apps/web/src/components/chat/composerAttachmentFiles.ts b/apps/web/src/components/chat/composerAttachmentFiles.tsindex 1517d4f2c0a..0d7648e3b53 100644--- a/apps/web/src/components/chat/composerAttachmentFiles.ts+++ b/apps/web/src/components/chat/composerAttachmentFiles.ts@@ -10,7 +10,7 @@ import {import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";import { isHeicImageFile } from "../../lib/imageCompression";-import { isVideoAttachment } from "../../types";+import { isVideoAttachment, videoMimeType } from "../../types";type ComposerAttachmentFileKind = "image" | "file" | "unsupported-image";@@ -59,9 +59,29 @@ export function normalizeComposerImageFileMimeType(file: File): File {});}+function isDngFile(file: Pick<File, "name" | "type">): boolean {+ const mimeType = file.type.split(";", 1)[0]?.trim().toLowerCase();+ return (+ file.name.toLowerCase().endsWith(".dng") ||+ mimeType === "image/dng" ||+ mimeType === "image/x-dng" ||+ mimeType === "image/x-adobe-dng"+ );+}++export function composerFileAttachmentMimeType(file: Pick<File, "name" | "type">): string {+ // Downstream image detection uses MIME, even for attachments staged as files.+ if (isDngFile(file)) return "image/x-adobe-dng";+ return (+ videoMimeType({ name: file.name, mimeType: file.type }) ??+ (file.type || "application/octet-stream")+ );+}+export function classifyComposerAttachmentFile(file: Pick<File, "name" | "type">,): ComposerAttachmentFileKind {+ if (isDngFile(file)) return "file";if (isHeicImageFile(file)) {return "image";}@@ -147,6 +167,7 @@ export function shouldHandleComposerAttachmentPaste(input: {}): boolean {if (input.files.some((file) => {+ if (isDngFile(file)) return true;const classification = classifyComposerAttachmentFile(file);return classification === "image" || classification === "unsupported-image";})diff --git a/apps/web/src/components/chat/useComposerAttachmentIntake.ts b/apps/web/src/components/chat/useComposerAttachmentIntake.tsindex 1008b71fa8b..63a865c4fc9 100644--- a/apps/web/src/components/chat/useComposerAttachmentIntake.ts+++ b/apps/web/src/components/chat/useComposerAttachmentIntake.ts@@ -25,6 +25,7 @@ import {} from "../../questionAttachments";import {classifyComposerAttachmentFile,+ composerFileAttachmentMimeType,normalizeComposerImageFileMimeType,} from "./composerAttachmentFiles";import {@@ -45,7 +46,6 @@ import {import { requestConfirmDialog } from "~/confirmDialog";import { toastManager } from "../ui/toast";import { type RefObject, useRef } from "react";-import { videoMimeType } from "../../types";import {addPreparingComposerImages,isPreparingComposerImage,@@ -162,10 +162,7 @@ export function useComposerAttachmentIntake(input: {for (const file of files) {const attachmentKind = classifyComposerAttachmentFile(file);const fileMimeType =- attachmentKind === "file"- ? (videoMimeType({ name: file.name, mimeType: file.type }) ??- (file.type || "application/octet-stream"))- : file.type;+ attachmentKind === "file" ? composerFileAttachmentMimeType(file) : file.type;const matchingReattachMarker =attachmentKind === "file"? reattachMarkers.find(diff --git a/apps/web/src/composerDraftStore.test.ts b/apps/web/src/composerDraftStore.test.tsindex 44f1d5a92a9..a7eb9b1c1a0 100644--- a/apps/web/src/composerDraftStore.test.ts+++ b/apps/web/src/composerDraftStore.test.ts@@ -521,6 +521,42 @@ describe("composerDraftStore file attachments", () => {expect(draft?.files.map((file) => file.id)).toEqual(["file-accepted"]);});+ it("reattaches a DNG with an older generic-MIME marker at attachment capacity", () => {+ const store = useComposerDraftStore.getState();+ const file = new File(["raw"], "photo.DNG", { type: "image/x-adobe-dng" });+ const marker: ComposerFileAttachment = {+ type: "file",+ id: "dng-marker",+ name: file.name,+ mimeType: "application/octet-stream",+ sizeBytes: file.size,+ file: null,+ };+ store.addFiles(threadRef, [marker]);+ store.addImages(+ threadRef,+ Array.from({ length: PROVIDER_SEND_TURN_MAX_ATTACHMENTS - 1 }, (_, index) =>+ makeImage({+ id: `image-${index}`,+ name: `image-${index}.png`,+ previewUrl: `blob:image-${index}`,+ }),+ ),+ );++ const repicked: ComposerFileAttachment = {+ ...marker,+ id: "dng-repicked",+ mimeType: file.type,+ file,+ };+ store.addFiles(threadRef, [repicked]);++ const files = store.getComposerDraft(threadRef)?.files;+ expect(files).toEqual([repicked]);+ expect(files?.some(composerFileNeedsReattach)).toBe(false);+ });+it("replaces a needs-reattach marker when the same file is picked again", () => {const store = useComposerDraftStore.getState();const marker: ComposerFileAttachment = { ...makeFile("file-marker"), file: null };diff --git a/apps/web/src/composerDraftStore.ts b/apps/web/src/composerDraftStore.tsindex 57ffde65949..8c04bf2ae6f 100644--- a/apps/web/src/composerDraftStore.ts+++ b/apps/web/src/composerDraftStore.ts@@ -1,4 +1,5 @@import { stripInlineContextReferences } from "./lib/composerContextReferences";+import { composerFileAttachmentMimeType } from "./components/chat/composerAttachmentFiles";import { elementContextToPreviewAnnotation } from "./lib/elementContext";import {ElementContextDetails,@@ -804,6 +805,17 @@ export function composerFileMatchesReattachMarker(): boolean {if (marker.name !== file.name || marker.sizeBytes !== file.sizeBytes) return false;if (marker.mimeType === file.mimeType) return true;+ const normalizedMarkerMimeType = composerFileAttachmentMimeType({+ name: marker.name,+ type: marker.mimeType,+ });+ if (+ normalizedMarkerMimeType === "image/x-adobe-dng" &&+ normalizedMarkerMimeType ===+ composerFileAttachmentMimeType({ name: file.name, type: file.mimeType })+ ) {+ return true;+ }const markerMimeType = marker.mimeType.toLowerCase();return ((markerMimeType === "" || markerMimeType === "application/octet-stream") &&