From 0d74625270c5745089f77335941739ec8aa9ac59 Mon Sep 17 00:00:00 2001 From: dswbx Date: Mon, 29 Sep 2025 09:55:02 +0200 Subject: [PATCH] feat: fix PDF mime type detection and enhance filename handling tests Updated the mime types to include "pdf" in the application category. Enhanced the test suite for media mime types to validate filename handling for PDF files, ensuring correct extensions are maintained based on file type and name. This improves the robustness of the file handling logic in the application. --- app/__test__/media/mime-types.spec.ts | 26 ++++++++++++++++++++++++ app/src/media/storage/mime-types-tiny.ts | 4 ++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/__test__/media/mime-types.spec.ts b/app/__test__/media/mime-types.spec.ts index dd13f7c..d326aca 100644 --- a/app/__test__/media/mime-types.spec.ts +++ b/app/__test__/media/mime-types.spec.ts @@ -71,6 +71,7 @@ describe("media/mime-types", () => { ["application/zip", "zip"], ["text/tab-separated-values", "tsv"], ["application/zip", "zip"], + ["application/pdf", "pdf"], ] as const; for (const [mime, ext] of tests) { @@ -88,6 +89,7 @@ describe("media/mime-types", () => { ["image.jpeg", "jpeg"], ["-473Wx593H-466453554-black-MODEL.jpg", "jpg"], ["-473Wx593H-466453554-black-MODEL.avif", "avif"], + ["file.pdf", "pdf"], ] as const; for (const [filename, ext] of tests) { @@ -102,4 +104,28 @@ describe("media/mime-types", () => { const [, ext] = getRandomizedFilename(file).split("."); expect(ext).toBe("jpg"); }); + + test("getRandomizedFilename with body", async () => { + // should keep "pdf" + const [, ext] = getRandomizedFilename( + new File([""], "file.pdf", { type: "application/pdf" }), + ).split("."); + expect(ext).toBe("pdf"); + + { + // no ext, should use "pdf" only for known formats + const [, ext] = getRandomizedFilename( + new File([""], "file", { type: "application/pdf" }), + ).split("."); + expect(ext).toBe("pdf"); + } + + { + // wrong ext, should keep the wrong one + const [, ext] = getRandomizedFilename( + new File([""], "file.what", { type: "application/pdf" }), + ).split("."); + expect(ext).toBe("what"); + } + }); }); diff --git a/app/src/media/storage/mime-types-tiny.ts b/app/src/media/storage/mime-types-tiny.ts index e7c42bb..6a3e241 100644 --- a/app/src/media/storage/mime-types-tiny.ts +++ b/app/src/media/storage/mime-types-tiny.ts @@ -3,7 +3,7 @@ export const Q = { audio: ["ogg"], image: ["jpeg", "png", "gif", "webp", "bmp", "tiff", "avif", "heic", "heif"], text: ["html", "css", "mdx", "yaml", "vcard", "csv", "vtt"], - application: ["zip", "xml", "toml", "json", "json5"], + application: ["zip", "xml", "toml", "json", "json5", "pdf"], font: ["woff", "woff2", "ttf", "otf"], } as const; @@ -19,7 +19,7 @@ const c = { export const M = new Map([ ["7z", c.z], ["7zip", c.z], - ["ai", c.a("pdf")], + ["ai", c.a("postscript")], ["apk", c.a("vnd.android.package-archive")], ["doc", c.a("msword")], ["docx", `${c.vnd}.wordprocessingml.document`],