The next-generation file converter. Open source, fully local* and free forever.
fix: detect file type
JovannMCa9776214c1d3
diff --git a/bun.lock b/bun.lockindex 3cd2201..b690c28 100644--- a/bun.lock+++ b/bun.lock@@ -23,6 +23,7 @@"clsx": "^2.1.1","dicom.ts": "^1.3.0","fflate": "^0.8.3",+ "file-type": "^22.0.2","mediabunny": "^1.55.7","music-metadata": "^11.15.0","overlayscrollbars": "^2.16.0",@@ -559,7 +560,7 @@"file-entry-cache": ["[email protected]", "", { "dependencies": { "flat-cache": "^4.0.0" } }, "sha512-XXTUwCvisa5oacNGRP9SfNtYBNAMi+RPwBFmblZEF7N7swHYQS6/Zfk7SRwx4D5j3CH211YNRco1DEMNVfZCnQ=="],- "file-type": ["[email protected]", "", { "dependencies": { "@tokenizer/inflate": "^0.4.1", "strtok3": "^10.3.4", "token-types": "^6.1.1", "uint8array-extras": "^1.4.0" } }, "sha512-Ievi/yy8DS3ygGvT47PjSfdFoX+2isQueoYP1cntFW1JLYAuS4GD7NUPGg4zv2iZfV52uDyk5w5Z0TdpRS6Q1g=="],+ "file-type": ["[email protected]", "", { "dependencies": { "@tokenizer/inflate": "^0.4.1", "strtok3": "^10.3.5", "token-types": "^6.1.2", "uint8array-extras": "^1.5.0" } }, "sha512-0H8TsCUGBLx+V5adH3EY52hTAcyLKbV1D4gq5cIOJ6DnQAHeV9Z2Hhuc5CoBX4YmvB2oL+JIC84z0qO7JsCoNw=="],"fill-range": ["[email protected]", "", { "dependencies": { "to-regex-range": "^5.0.1" } }, "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg=="],@@ -907,6 +908,8 @@"micromatch/picomatch": ["[email protected]", "", {}, "sha512-V7+vQEJ06Z+c5tSye8S+nHUfI51xoXIXjHQ99cQtKUkQqqO1kO/KCJUfZXuB47h/YBlDhah2H3hdUGXn8ie0oA=="],+ "music-metadata/file-type": ["[email protected]", "", { "dependencies": { "@tokenizer/inflate": "^0.4.1", "strtok3": "^10.3.4", "token-types": "^6.1.1", "uint8array-extras": "^1.4.0" } }, "sha512-Ievi/yy8DS3ygGvT47PjSfdFoX+2isQueoYP1cntFW1JLYAuS4GD7NUPGg4zv2iZfV52uDyk5w5Z0TdpRS6Q1g=="],+"postcss-load-config/lilconfig": ["[email protected]", "", {}, "sha512-utWOt/GHzuUxnLKxB6dk81RoOeoNeHgbrXiuGk4yyF5qlRz+iIVWu56E2fqGHFrXz0QNUhLB/8nKqvRH66JKGQ=="],"riff-file/byte-data": ["[email protected]", "", { "dependencies": { "endianness": "^8.0.2", "ieee754-buffer": "^2.0.0", "utf8-buffer": "^1.0.0" } }, "sha512-Kv/B0r7adgnCcrs/y703sac2XFLdHW5kPfis1j8+Ij/hmEcWhBKf+1pNTv+vsNqXb207Uiyri8bpnogNxR/4Lg=="],diff --git a/package.json b/package.jsonindex d9cb016..7eedfff 100644--- a/package.json+++ b/package.json@@ -57,6 +57,7 @@"clsx": "^2.1.1","dicom.ts": "^1.3.0","fflate": "^0.8.3",+ "file-type": "^22.0.2","mediabunny": "^1.55.7","music-metadata": "^11.15.0","overlayscrollbars": "^2.16.0",diff --git a/src/lib/types/file.svelte.ts b/src/lib/types/file.svelte.tsindex 57828b2..c01b6e2 100644--- a/src/lib/types/file.svelte.ts+++ b/src/lib/types/file.svelte.ts@@ -13,6 +13,7 @@ import type {import { log } from "$lib/util/logger";import { readSettings } from "$lib/util/settings";import { formatFilename } from "$lib/util/file";+import { fileTypeFromBuffer } from "file-type";const LARGE_FILE = 2 * 1024 * 1024 * 1024; // 2GB@@ -20,24 +21,17 @@ export class VertFile {public id: string = Math.random().toString(36).slice(2, 8);public readonly file: File;- public get from() {- return ("." + this.file.name.split(".").pop() || "").toLowerCase();- }-- public get name() {- return this.file.name;- }+ public from = $state("");+ public name = $state("");+ public to = $state("");+ public fileType = $state<Awaited<ReturnType<typeof fileTypeFromBuffer>>>();+ public isZip = $state(() => this.from === ".zip");public conversionSettings = $state<ConversionSettings>({}); // empty object / key = defaultpublic progress = $state(0);public result = $state<VertFile | null>(null);-- public to = $state("");-public blobUrl = $state<string>();-public processing = $state(false);-public cancelled = $state(false);public converters: Converter[] = [];@@ -48,7 +42,30 @@ export class VertFile {private postDownload: (() => Promise<void>) | null = null;private activeConverterName: string | null = null;- public isZip = $state(() => this.from === ".zip");+ constructor(file: File, to: string, blobUrl?: string) {+ const ext = file.name.split(".").pop();+ const newFile = new File(+ [file],+ `${file.name.split(".").slice(0, -1).join(".")}.${ext?.toLowerCase()}`,+ );+ this.file = newFile;+ this.name = newFile.name;+ this.from = ("." + ext || "").toLowerCase();+ this.to = to.startsWith(".") ? to : `.${to}`;+ this.converters = converters.filter((c) =>+ c.formatStrings().includes(this.from),+ );+ this.convert = this.convert.bind(this);+ this.download = this.download.bind(this);+ this.blobUrl = blobUrl;++ log(+ ["file", "init"],+ `findConverters: ${this.findConverters()+ .map((c) => c.name)+ .join(", ")}`,+ );+ }public setPostDownload(cleanup: (() => Promise<void>) | null) {this.postDownload = cleanup;@@ -144,32 +161,45 @@ export class VertFile {);}- constructor(file: File, to: string, blobUrl?: string) {- const ext = file.name.split(".").pop();- const newFile = new File(- [file],- `${file.name.split(".").slice(0, -1).join(".")}.${ext?.toLowerCase()}`,- );- this.file = newFile;- this.to = to.startsWith(".") ? to : `.${to}`;- this.converters = converters.filter((c) =>- c.formatStrings().includes(this.from),- );- this.convert = this.convert.bind(this);- this.download = this.download.bind(this);- this.blobUrl = blobUrl;+ public async checkFileType() {+ try {+ this.fileType = await fileTypeFromBuffer(+ await this.file.arrayBuffer(),+ );- log(- ["file", "init"],- `findConverters: ${this.findConverters()- .map((c) => c.name)- .join(", ")}`,- );+ if (!this.fileType) return;++ const aliases: Record<string, string> = {+ jpg: "jpeg",+ jfif: "jpeg",+ tif: "tiff",+ // TODO: is there more stuff+ };+ const fileExtension = this.from.slice(1);+ const detectedExtension =+ aliases[this.fileType.ext] ?? this.fileType.ext;+ const expectedExtension = aliases[fileExtension] ?? fileExtension;++ if (detectedExtension !== expectedExtension) {+ // TODO: show a warning modal or message if detected type doesn't match name+ }++ this.from = `.${this.fileType.ext}`;+ this.converters = converters.filter((converter) =>+ converter.formatStrings().includes(this.from),+ );+ } catch (error) {+ log(+ ["file", "type"],+ `failed to detect file type for ${this.file.name}: ${error}`,+ );+ }}// eslint-disable-next-line @typescript-eslint/no-explicit-anypublic async convert(...args: any[]) {await this.runPostDownload();+ await this.checkFileType();if (!this.retryingFallback) this.attemptedConverters.clear();diff --git a/src/lib/workers/magick.ts b/src/lib/workers/magick.tsindex c7bd102..7288868 100644--- a/src/lib/workers/magick.ts+++ b/src/lib/workers/magick.ts@@ -96,10 +96,7 @@ const handleMessage = async (};}default:- return {- type: "error",- error: `Unknown message type: ${message.type}`,- };+ return { type: "error", error: "Unknown message type" };}};