Skip to content

Commit 14b828e

Browse files
fix(web): attach DNG originals as generic files (#24625)
1 parent fa1347d commit 14b828e

5 files changed

Lines changed: 148 additions & 7 deletions

File tree

‎apps/web/src/components/chat/composerAttachmentFiles.test.ts‎

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,90 @@
1-
import { EnvironmentId, PROVIDER_SEND_TURN_MAX_FILE_BYTES } from "@t3tools/contracts";
1+
import {
2+
EnvironmentId,
3+
getProviderAttachmentLimitError,
4+
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
5+
} from "@t3tools/contracts";
26
import { describe, expect, it } from "vite-plus/test";
37

8+
import { isFileAttachment, isImageAttachment } from "../../types";
49
import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";
510
import {
611
attachmentsToReleaseOnUploadCapabilityLoss,
12+
classifyComposerAttachmentFile,
13+
composerFileAttachmentMimeType,
714
fileAttachmentCapabilityBlockReason,
815
fileAttachmentStagingLimit,
16+
normalizeComposerImageFileMimeType,
917
shouldHandleComposerAttachmentPaste,
1018
} from "./composerAttachmentFiles";
1119

1220
describe("composer attachment files", () => {
21+
it("keeps DNG originals on the file path without changing their bytes", async () => {
22+
const bytes = new Uint8Array([0x49, 0x49, 0x2a, 0x00, 0x08, 0, 0, 0, 0xfe, 0xff, 0x00, 0x80]);
23+
const files = [
24+
new File([bytes], "photo.DNG", { type: "image/tiff" }),
25+
new File([bytes], "photo.dng", { type: "image/png" }),
26+
new File([bytes], "photo.dng", { type: "image/jpeg" }),
27+
new File([bytes], "clipboard", { type: "image/dng" }),
28+
new File([bytes], "clipboard", { type: "image/x-dng" }),
29+
new File([bytes], "clipboard", { type: "image/x-adobe-dng" }),
30+
];
31+
32+
const attachments = await Promise.all(
33+
files.map(async (file) => ({
34+
kind: classifyComposerAttachmentFile(file),
35+
bytes: new Uint8Array(await normalizeComposerImageFileMimeType(file).arrayBuffer()),
36+
})),
37+
);
38+
39+
expect(attachments).toEqual(files.map(() => ({ kind: "file", bytes })));
40+
});
41+
42+
it("keeps misleading DNG MIME labels out of downstream image detection and budgets", () => {
43+
for (const type of ["image/png", "image/jpeg", "image/tiff", "image/dng", ""]) {
44+
const attachment = {
45+
type: "file" as const,
46+
id: "raw",
47+
name: "photo.DNG",
48+
mimeType: composerFileAttachmentMimeType({ name: "photo.DNG", type }),
49+
sizeBytes: 81 * 1024 * 1024,
50+
};
51+
52+
expect(isImageAttachment(attachment)).toBe(false);
53+
expect(isFileAttachment(attachment)).toBe(true);
54+
expect(getProviderAttachmentLimitError([attachment])).toBeUndefined();
55+
}
56+
});
57+
58+
it("claims a captioned DNG paste even when its MIME is generic", () => {
59+
const file = new File(["raw"], "photo.dng", { type: "application/octet-stream" });
60+
61+
expect(
62+
shouldHandleComposerAttachmentPaste({ files: [file], plainText: "Inspect this original" }),
63+
).toBe(true);
64+
});
65+
66+
it("keeps ordinary images on their existing paths", () => {
67+
const files = [
68+
{ name: "photo.jpg", type: "image/jpeg" },
69+
{ name: "export.dng.png", type: "image/png" },
70+
{ name: "photo.webp", type: "image/webp" },
71+
{ name: "photo.gif", type: "image/gif" },
72+
{ name: "photo.heic", type: "" },
73+
{ name: "scan.tif", type: "image/tiff" },
74+
{ name: "photo.cr2", type: "image/x-canon-cr2" },
75+
];
76+
77+
expect(files.map(classifyComposerAttachmentFile)).toEqual([
78+
"image",
79+
"image",
80+
"image",
81+
"image",
82+
"image",
83+
"unsupported-image",
84+
"unsupported-image",
85+
]);
86+
});
87+
1388
it("preserves text paste when an application adds a synthetic generic file", () => {
1489
const file = new File(["clipboard"], "clipboard.rtf", { type: "application/rtf" });
1590

‎apps/web/src/components/chat/composerAttachmentFiles.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010

1111
import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";
1212
import { isHeicImageFile } from "../../lib/imageCompression";
13-
import { isVideoAttachment } from "../../types";
13+
import { isVideoAttachment, videoMimeType } from "../../types";
1414

1515
type ComposerAttachmentFileKind = "image" | "file" | "unsupported-image";
1616

@@ -59,9 +59,29 @@ export function normalizeComposerImageFileMimeType(file: File): File {
5959
});
6060
}
6161

62+
function 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+
72+
export 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+
6281
export function classifyComposerAttachmentFile(
6382
file: Pick<File, "name" | "type">,
6483
): ComposerAttachmentFileKind {
84+
if (isDngFile(file)) return "file";
6585
if (isHeicImageFile(file)) {
6686
return "image";
6787
}
@@ -147,6 +167,7 @@ export function shouldHandleComposerAttachmentPaste(input: {
147167
}): boolean {
148168
if (
149169
input.files.some((file) => {
170+
if (isDngFile(file)) return true;
150171
const classification = classifyComposerAttachmentFile(file);
151172
return classification === "image" || classification === "unsupported-image";
152173
})

‎apps/web/src/components/chat/useComposerAttachmentIntake.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from "../../questionAttachments";
2626
import {
2727
classifyComposerAttachmentFile,
28+
composerFileAttachmentMimeType,
2829
normalizeComposerImageFileMimeType,
2930
} from "./composerAttachmentFiles";
3031
import {
@@ -45,7 +46,6 @@ import {
4546
import { requestConfirmDialog } from "~/confirmDialog";
4647
import { toastManager } from "../ui/toast";
4748
import { type RefObject, useRef } from "react";
48-
import { videoMimeType } from "../../types";
4949
import {
5050
addPreparingComposerImages,
5151
isPreparingComposerImage,
@@ -162,10 +162,7 @@ export function useComposerAttachmentIntake(input: {
162162
for (const file of files) {
163163
const attachmentKind = classifyComposerAttachmentFile(file);
164164
const fileMimeType =
165-
attachmentKind === "file"
166-
? (videoMimeType({ name: file.name, mimeType: file.type }) ??
167-
(file.type || "application/octet-stream"))
168-
: file.type;
165+
attachmentKind === "file" ? composerFileAttachmentMimeType(file) : file.type;
169166
const matchingReattachMarker =
170167
attachmentKind === "file"
171168
? reattachMarkers.find(

‎apps/web/src/composerDraftStore.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,42 @@ describe("composerDraftStore file attachments", () => {
521521
expect(draft?.files.map((file) => file.id)).toEqual(["file-accepted"]);
522522
});
523523

524+
it("reattaches a DNG with an older generic-MIME marker at attachment capacity", () => {
525+
const store = useComposerDraftStore.getState();
526+
const file = new File(["raw"], "photo.DNG", { type: "image/x-adobe-dng" });
527+
const marker: ComposerFileAttachment = {
528+
type: "file",
529+
id: "dng-marker",
530+
name: file.name,
531+
mimeType: "application/octet-stream",
532+
sizeBytes: file.size,
533+
file: null,
534+
};
535+
store.addFiles(threadRef, [marker]);
536+
store.addImages(
537+
threadRef,
538+
Array.from({ length: PROVIDER_SEND_TURN_MAX_ATTACHMENTS - 1 }, (_, index) =>
539+
makeImage({
540+
id: `image-${index}`,
541+
name: `image-${index}.png`,
542+
previewUrl: `blob:image-${index}`,
543+
}),
544+
),
545+
);
546+
547+
const repicked: ComposerFileAttachment = {
548+
...marker,
549+
id: "dng-repicked",
550+
mimeType: file.type,
551+
file,
552+
};
553+
store.addFiles(threadRef, [repicked]);
554+
555+
const files = store.getComposerDraft(threadRef)?.files;
556+
expect(files).toEqual([repicked]);
557+
expect(files?.some(composerFileNeedsReattach)).toBe(false);
558+
});
559+
524560
it("replaces a needs-reattach marker when the same file is picked again", () => {
525561
const store = useComposerDraftStore.getState();
526562
const marker: ComposerFileAttachment = { ...makeFile("file-marker"), file: null };

‎apps/web/src/composerDraftStore.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { stripInlineContextReferences } from "./lib/composerContextReferences";
2+
import { composerFileAttachmentMimeType } from "./components/chat/composerAttachmentFiles";
23
import { elementContextToPreviewAnnotation } from "./lib/elementContext";
34
import {
45
ElementContextDetails,
@@ -804,6 +805,17 @@ export function composerFileMatchesReattachMarker(
804805
): boolean {
805806
if (marker.name !== file.name || marker.sizeBytes !== file.sizeBytes) return false;
806807
if (marker.mimeType === file.mimeType) return true;
808+
const normalizedMarkerMimeType = composerFileAttachmentMimeType({
809+
name: marker.name,
810+
type: marker.mimeType,
811+
});
812+
if (
813+
normalizedMarkerMimeType === "image/x-adobe-dng" &&
814+
normalizedMarkerMimeType ===
815+
composerFileAttachmentMimeType({ name: file.name, type: file.mimeType })
816+
) {
817+
return true;
818+
}
807819
const markerMimeType = marker.mimeType.toLowerCase();
808820
return (
809821
(markerMimeType === "" || markerMimeType === "application/octet-stream") &&

0 commit comments

Comments
 (0)