feat(attachments): depolama soyutlaması, içerikten tür tespiti, sharp/HEIC dönüşümü ve yetkili dosya servisi

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-05 07:55:04 +03:00
parent 99fc9dfea6
commit 2e1f4d7929
21 changed files with 1246 additions and 16 deletions
+56
View File
@@ -0,0 +1,56 @@
import { describe, expect, it } from "vitest";
import {
acceptAttribute,
allowedAttachmentKinds,
detectFileType,
formatFileSize,
kindOf,
} from "./attachments";
const bytes = (...parts: (number[] | string)[]) =>
new Uint8Array(
parts.flatMap((p) => (typeof p === "string" ? [...p].map((c) => c.charCodeAt(0)) : p)),
);
describe("detectFileType", () => {
it("imzalardan türü bulur", () => {
expect(detectFileType(bytes([0xff, 0xd8, 0xff, 0xe0]))).toBe("jpeg");
expect(detectFileType(bytes([0x89], "PNG", [0x0d, 0x0a, 0x1a, 0x0a]))).toBe("png");
expect(detectFileType(bytes("RIFF", [0, 0, 0, 0], "WEBPVP8 "))).toBe("webp");
expect(detectFileType(bytes("%PDF-1.7"))).toBe("pdf");
});
it("HEIC: ana veya uyumlu markadan", () => {
expect(detectFileType(bytes([0, 0, 0, 24], "ftypheic", [0, 0, 0, 0], "mif1heic"))).toBe("heic");
expect(detectFileType(bytes([0, 0, 0, 28], "ftypmif1", [0, 0, 0, 0], "mif1heichevc"))).toBe(
"heic",
);
// MP4 videosu HEIC değildir.
expect(detectFileType(bytes([0, 0, 0, 24], "ftypisom", [0, 0, 2, 0], "isomiso2"))).toBeNull();
});
it("uzantı/metin/kısa içerik reddedilir", () => {
expect(detectFileType(bytes("<svg xmlns="))).toBeNull();
expect(detectFileType(bytes("GIF89a"))).toBeNull();
expect(detectFileType(bytes([0xff, 0xd8]))).toBeNull();
expect(detectFileType(new Uint8Array())).toBeNull();
});
});
describe("tür kuralları", () => {
it("gider yalnızca görsel; gelir ve transfer görsel + PDF", () => {
expect(allowedAttachmentKinds("expense")).toEqual(["image"]);
expect(allowedAttachmentKinds("income")).toEqual(["image", "pdf"]);
expect(allowedAttachmentKinds("transfer")).toEqual(["image", "pdf"]);
expect(acceptAttribute(["image"])).not.toContain("pdf");
expect(acceptAttribute(["image", "pdf"])).toContain("application/pdf");
expect(kindOf("heic")).toBe("image");
expect(kindOf("pdf")).toBe("pdf");
});
it("boyut biçimi", () => {
expect(formatFileSize(900)).toBe("900 B");
expect(formatFileSize(2048)).toBe("2 KB");
expect(formatFileSize(1.5 * 1024 * 1024)).toBe("1,5 MB");
});
});