Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 52 additions & 28 deletions app/src/components/channels/composer/picked-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,22 +223,20 @@ describe("screenPickedFiles", () => {
);
});

test("still refuses the older Office formats by name", () => {
const result = screenPickedFiles(
[
file("old.doc", "application/msword"),
file("old.xls", "application/vnd.ms-excel"),
file("macros.xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12"),
],
{ alreadyStaged: 0, limits },
);
/*
* The older Office formats go to the server, which refuses them by their bytes (an OLE file, or a
* package naming a macro-enabled main part). The file dialog still does not offer them.
*/
test("leaves the older Office formats to the server, and the dialog does not offer them", () => {
const picked = [
file("old.doc", "application/msword"),
file("old.xls", "application/vnd.ms-excel"),
file("macros.xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12"),
];
const result = screenPickedFiles(picked, { alreadyStaged: 0, limits });

expect(result.accepted).toEqual([]);
expect(result.rejected.map(({ reason }) => reason)).toEqual([
"'old.doc' isn't a file type you can attach.",
"'old.xls' isn't a file type you can attach.",
"'macros.xlsm' isn't a file type you can attach.",
]);
expect(result.rejected).toEqual([]);
expect(result.accepted).toEqual(picked);
const offered = FILE_PICKER_ACCEPT.split(",");
for (const legacy of [".doc", ".xls", ".ppt", ".xlsm"])
expect(offered).not.toContain(legacy);
Expand Down Expand Up @@ -288,33 +286,59 @@ describe("screenPickedFiles", () => {
expect(result.rejected[0].reason).toMatch(/too large|larger than/);
});

test("a claim that names a format it does not accept is still refused here", () => {
// The fall-through is only for claims that name NOTHING. `application/zip` names a format, and
// `sniffMimeType` hands that name straight back for the server to refuse by name, so refusing
// it at pick time is the two halves agreeing rather than disagreeing.
const zip = file("archive.zip", "application/zip");
const result = screenPickedFiles([zip], { alreadyStaged: 0, limits });
/**
* EvoPointBio's protein structures and molecules. Each is plain text, and the browser's type for
* it is whatever the computer registered for the extension: Linux calls a `.pdb`
* `chemical/x-pdb`, and a Windows machine with chemistry software installed names `.mol` and
* `.cif` too. The server takes every one of them as text, so this screen must not refuse them by
* that name.
*/
test("lets a text file the browser names as another format through for the server to read", () => {
const picked = [
file("model2.pdb", "chemical/x-pdb"),
file("model2.pdb", "application/x-pilot"),
file("1crn.cif", "chemical/x-cif"),
file("water.xyz", "chemical/x-xyz"),
file("water.mol", "chemical/x-mdl-molfile"),
file("plate-layout.xml", "application/xml"),
file("pipeline.yaml", "application/yaml"),
file("run.sh", "application/x-sh"),
];
const result = screenPickedFiles(picked, { alreadyStaged: 0, limits });

expect(result.rejected).toEqual([]);
expect(result.accepted).toEqual(picked);
});

test("holds a file the browser names as another format to the larger ceiling", () => {
const huge = sized("model2.pdb", "chemical/x-pdb", MAX_IMAGE_BYTES + 1);
const result = screenPickedFiles([huge], { alreadyStaged: 0, limits });

expect(result.accepted).toEqual([]);
expect(result.rejected).toHaveLength(1);
expect(result.rejected[0].reason).toBe(
"'model2.pdb' is too large. The limit is 8MB.",
);
});

test("rejects an unsupported file type with a reason distinct from the SVG one", () => {
test("a zip goes to the server, which refuses it by its bytes", () => {
const zip = file("archive.zip", "application/zip");
const result = screenPickedFiles([zip], { alreadyStaged: 0, limits });

expect(result.accepted).toEqual([]);
expect(result.rejected[0].reason).not.toMatch(/SVG/);
expect(result.rejected).toEqual([]);
expect(result.accepted).toEqual([zip]);
});

test("reports one rejection per bad file, each naming its own file", () => {
const svg = file("a.svg", "image/svg+xml");
const zip = file("b.zip", "application/zip");
const result = screenPickedFiles([svg, zip], { alreadyStaged: 0, limits });
const heic = file("b.heic", "image/heic");
const result = screenPickedFiles([svg, heic], { alreadyStaged: 0, limits });

expect(result.rejected).toHaveLength(2);
expect(result.rejected[0].name).toBe("a.svg");
expect(result.rejected[1].name).toBe("b.zip");
expect(result.rejected[1].name).toBe("b.heic");
expect(result.rejected[1].reason).toBe(
"'b.heic' isn't a supported image format.",
);
});

test("counts the cap after kind and size, against already-staged files", () => {
Expand Down
32 changes: 14 additions & 18 deletions app/src/components/channels/composer/picked-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
largestAttachmentBytes,
MAX_ATTACHMENTS_PER_MESSAGE,
mediaTypeOf,
namesNoFormat,
} from "@/lib/channels/attachments";
import { newId } from "@/lib/new-id";
import type { RejectedFile } from "./rejected-files";
Expand Down Expand Up @@ -69,28 +68,25 @@ export function screenPickedFiles(
}

/*
* THE BROWSER TOLD US NOTHING, SO THE SERVER GETS TO LOOK — AND ONLY THEN.
* A TYPE OPENBOT DOES NOT TAKE IS NOT A REFUSAL HERE: THE SERVER READS THE BYTES.
*
* A claim that names no format is not a refusal, it is an absence: `sniffMimeType` throws
* exactly these claims away and reads the bytes, so the server accepts the plain text file
* behind an `application/octet-stream` that this screen used to turn away with "not a file type
* this chat accepts" — a sentence written by the half of the system that had not looked at it.
* The browser's type for a file is whatever the computer has registered for its extension, and
* for a plain text file that is often the name of something else. A `.pdb` protein structure
* is `chemical/x-pdb` on Linux, and a Windows machine with chemistry software installed names
* `.mol` and `.cif` too. Every one of those is text, and the server takes it as text: it keeps
* only a claim OpenBot acts on and reads the bytes for the rest, so a text file is stored as
* `text/plain` and anything else is refused by its bytes. Refusing the name here turned the
* person away from a file the server would have taken.
*
* A claim that DOES name a format is still refused here, because there the two halves already
* agree: `sniffMimeType` hands such a name straight back for the server to refuse by name.
* The same holds for a browser that named nothing (`application/octet-stream`, `""`), which is
* where this branch began: `sniffMimeType` throws those claims away and reads the bytes.
*
* The cost, stated because it is real: a genuinely unreadable file the browser could not name
* The cost, stated because it is real: a genuinely unreadable file (a `.zip`, an old `.doc`)
* now takes a round trip to be refused, in the server's words rather than ours. That is the
* right way round. The server has the bytes; this screen has a string somebody else wrote.
* Images are still refused here by name, as the server refuses them: an SVG, a HEIC.
*/
const unnamed = kind === "unsupported" && namesNoFormat(mediaType);

if (kind === "unsupported" && !unnamed) {
rejected.push(
reject(file, `'${file.name}' isn't a file type you can attach.`),
);
continue;
}
const unnamed = kind === "unsupported";

if (kind === "image" && file.size > limits.imageBytes) {
rejected.push(
Expand Down Expand Up @@ -122,7 +118,7 @@ export function screenPickedFiles(
continue;
}

// The LOOSE ceiling for a file nobody has named yet, because this screen does not know which
// The LOOSE ceiling for a file whose kind only its bytes will tell, because this screen does not know which
// of the limits applies to it. Guessing the tight one would put back exactly the refusal this
// branch exists to remove; the server applies the right limit once it has the bytes.
const loosest = largestAttachmentBytes(limits);
Expand Down