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.
This commit is contained in:
dswbx
2025-09-29 09:55:02 +02:00
parent 800f14ede2
commit 0d74625270
2 changed files with 28 additions and 2 deletions

View File

@@ -71,6 +71,7 @@ describe("media/mime-types", () => {
["application/zip", "zip"], ["application/zip", "zip"],
["text/tab-separated-values", "tsv"], ["text/tab-separated-values", "tsv"],
["application/zip", "zip"], ["application/zip", "zip"],
["application/pdf", "pdf"],
] as const; ] as const;
for (const [mime, ext] of tests) { for (const [mime, ext] of tests) {
@@ -88,6 +89,7 @@ describe("media/mime-types", () => {
["image.jpeg", "jpeg"], ["image.jpeg", "jpeg"],
["-473Wx593H-466453554-black-MODEL.jpg", "jpg"], ["-473Wx593H-466453554-black-MODEL.jpg", "jpg"],
["-473Wx593H-466453554-black-MODEL.avif", "avif"], ["-473Wx593H-466453554-black-MODEL.avif", "avif"],
["file.pdf", "pdf"],
] as const; ] as const;
for (const [filename, ext] of tests) { for (const [filename, ext] of tests) {
@@ -102,4 +104,28 @@ describe("media/mime-types", () => {
const [, ext] = getRandomizedFilename(file).split("."); const [, ext] = getRandomizedFilename(file).split(".");
expect(ext).toBe("jpg"); 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");
}
});
}); });

View File

@@ -3,7 +3,7 @@ export const Q = {
audio: ["ogg"], audio: ["ogg"],
image: ["jpeg", "png", "gif", "webp", "bmp", "tiff", "avif", "heic", "heif"], image: ["jpeg", "png", "gif", "webp", "bmp", "tiff", "avif", "heic", "heif"],
text: ["html", "css", "mdx", "yaml", "vcard", "csv", "vtt"], 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"], font: ["woff", "woff2", "ttf", "otf"],
} as const; } as const;
@@ -19,7 +19,7 @@ const c = {
export const M = new Map<string, string>([ export const M = new Map<string, string>([
["7z", c.z], ["7z", c.z],
["7zip", c.z], ["7zip", c.z],
["ai", c.a("pdf")], ["ai", c.a("postscript")],
["apk", c.a("vnd.android.package-archive")], ["apk", c.a("vnd.android.package-archive")],
["doc", c.a("msword")], ["doc", c.a("msword")],
["docx", `${c.vnd}.wordprocessingml.document`], ["docx", `${c.vnd}.wordprocessingml.document`],