The next-generation file converter. Open source, fully local* and free forever.
fix: file type detection on upload, read file types of files in zip
JovannMC483bc7d74761
diff --git a/src/lib/converters/mediabunny/mediabunny.svelte.ts b/src/lib/converters/mediabunny/mediabunny.svelte.tsindex ec86e2c..8e2ed52 100644--- a/src/lib/converters/mediabunny/mediabunny.svelte.ts+++ b/src/lib/converters/mediabunny/mediabunny.svelte.ts@@ -397,7 +397,6 @@ export class MediabunnyConverter extends Converter {const outputFilename = `${originalName}.${toFormat}`;const input = new Input({- // TODO: add settings & special handling for certain formats & codecsformats: [MP4, QTFF, MATROSKA, WEBM, MPEG_TS],source: new BlobSource(file.file),});diff --git a/src/lib/store/index.svelte.ts b/src/lib/store/index.svelte.tsindex f13c535..89ae486 100644--- a/src/lib/store/index.svelte.ts+++ b/src/lib/store/index.svelte.ts@@ -194,7 +194,8 @@ class Files {}}- private async _handleZipFile(file: File): Promise<void> {+ private async _handleZipFile(input: VertFile): Promise<void> {+ const file = input.file;try {log(["files"], `extracting zip file: ${file.name}`);ToastManager.add({@@ -213,9 +214,18 @@ class Files {// check if all files in zip use the same converter and are compatibleconst convertersUsed = new Set<string>();let incompatibleFiles = false;+ const archiveFiles: VertFile[] = [];- for (const { filename } of entries) {- const format = "." + filename.split(".").pop()?.toLowerCase();+ for (const { filename, data } of entries) {+ const member = new VertFile(+ new File([new Uint8Array(data)], filename, {+ type: "application/octet-stream",+ }),+ ".zip",+ );+ await member.checkFileType();+ archiveFiles.push(member);+ const format = member.from;if (!format || format === ".zip") {incompatibleFiles = true;continue;@@ -239,7 +249,9 @@ class Files {if (canConvertAsOne) {// all files use same converter - add zip as a single VertFile file- const vf = new VertFile(file, ".zip");+ const vf = input;+ vf.to = ".zip";+ vf.archiveFormats = archiveFiles.map((member) => member.from);vf.converters = converters.filter((c) => c.name === Array.from(convertersUsed)[0],);@@ -273,12 +285,9 @@ class Files {});} else {// mixed converters/incompatible files - extract all individually- for (const { filename, data } of entries) {- this._add(- new File([new Uint8Array(data)], filename, {- type: "application/octet-stream",- }),- );+ for (const file of archiveFiles) {+ file.to = file.from;+ await this._add(file);}ToastManager.add({@@ -297,35 +306,32 @@ class Files {}private async _add(file: VertFile | File) {+ const input =+ file instanceof VertFile ? file : new VertFile(file, ".zip");+ await input.checkFileType();+ // if zip, extract and add contents+ const isZip = input.from === ".zip";+ if (isZip && !input.archiveFormats) {+ try {+ await this._handleZipFile(input);+ } catch (err) {+ error(["files"], `error extracting zip file: ${err}`);+ ToastManager.add({+ type: "error",+ message: m["convert.archive_file.extract_error"]({+ filename: input.file.name,+ error: String(err),+ }),+ });+ }+ return;+ }if (file instanceof VertFile) {this.files.push(file);this._addThumbnail(file);} else {- // if zip, extract and add contents- const isZip =- file.name.toLowerCase().endsWith(".zip") ||- file.type === "application/zip" ||- file.type === "application/x-zip-compressed";-- if (isZip) {- try {- await this._handleZipFile(file);- return;- } catch (err) {- error(["files"], `error extracting zip file: ${err}`);- ToastManager.add({- type: "error",- message: m["convert.archive_file.extract_error"]({- filename: file.name,- error: String(err),- }),- });- return;- }- }-// regular files- const format = "." + file.name.split(".").pop()?.toLowerCase();+ const format = input.from;if (!format) {log(["files"], `no extension found for ${file.name}`);return;@@ -337,7 +343,8 @@ class Files {);if (!converter) {log(["files"], `no converter found for ${file.name}`);- this.files.push(new VertFile(file, format));+ input.to = format;+ this.files.push(input);return;}const category = Object.keys(categories).find((name) =>@@ -350,7 +357,8 @@ class Files {log(["files"], `no output format found for ${file.name}`);return;}- const vf = new VertFile(file, to);+ const vf = input;+ vf.to = to;this.files.push(vf);this._addThumbnail(vf);@@ -383,12 +391,22 @@ class Files {VertFile | File | VertFile[] | File[] | FileList | null | undefined,) {if (!file) return;+ const addFile = (input: VertFile | File) => {+ void this._add(input).catch((err) => {+ error(["files"], `error adding file ${input.name}: ${err}`);+ ToastManager.add({+ type: "error",+ message: m["workers.errors.general"]({+ file: input.name,+ message: String(err),+ }),+ });+ });+ };if (Array.isArray(file) || file instanceof FileList) {- for (const f of file) {- this._add(f);- }+ for (const f of file) addFile(f);} else {- this._add(file);+ addFile(file);}}diff --git a/src/lib/types/file.svelte.ts b/src/lib/types/file.svelte.tsindex 4c3738e..70e1f17 100644--- a/src/lib/types/file.svelte.ts+++ b/src/lib/types/file.svelte.ts@@ -13,12 +13,13 @@ import type {NormalizedSettings,SettingCategories,} from "./conversion-settings";-import { log } from "$lib/util/logger";+import { error, 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+const FILE_TYPE_HEADER_SIZE = 4100;type ServerWarningRequest = {filename: string;@@ -118,6 +119,7 @@ export class VertFile {public vertdSizeWarningShown = false;public converters: Converter[] = [];+ public archiveFormats?: string[];private fallbackToastId: number | null = null;private slowConversionToastId: number | null = null;private attemptedConverters = new Set<string>();@@ -125,6 +127,7 @@ export class VertFile {private postDownload: (() => Promise<void>) | null = null;private activeConverterName: string | null = null;private fileTypeMismatchShown = false;+ private fileTypeCheck: Promise<void> | null = null;constructor(file: File, to: string, blobUrl?: string) {const ext = file.name.split(".").pop();@@ -280,10 +283,14 @@ export class VertFile {);}- public async checkFileType() {+ public checkFileType(): Promise<void> {+ return (this.fileTypeCheck ??= this.detectFileType());+ }++ private async detectFileType() {try {this.fileType = await fileTypeFromBuffer(- await this.file.arrayBuffer(),+ await this.file.slice(0, FILE_TYPE_HEADER_SIZE).arrayBuffer(),);if (!this.fileType) return;@@ -303,15 +310,22 @@ export class VertFile {mpv: "mpeg",};const fileExtension = this.originalFrom.slice(1);+ const isPngContainer =+ this.fileType.ext === "png" || this.fileType.ext === "apng";+ // preserve file type for png and apng so they can convert+ // TODO: apng can't be converted with magick-wasm - use another library for itconst detectedExtension = forceKeep.includes(fileExtension)? fileExtension- : (aliases[this.fileType.ext] ?? this.fileType.ext);+ : isPngContainer &&+ (fileExtension === "png" || fileExtension === "apng")+ ? fileExtension+ : (aliases[this.fileType.ext] ?? this.fileType.ext);const expectedExtension = aliases[fileExtension] ?? fileExtension;if (detectedExtension !== expectedExtension) {this.from = `.${detectedExtension}`;if (!this.fileTypeMismatchShown) {- console.warn(+ error(`file type mismatched: expected ${expectedExtension}, detected ${detectedExtension}`,);ToastManager.add({@@ -343,11 +357,10 @@ export class VertFile {// 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();- console.log(+ log(`Starting conversion for ${this.file.name} from ${this.from} to ${this.to}`,);if (!this.converters.length) throw new Error("No converters found");@@ -579,10 +592,14 @@ export class VertFile {throw new Error("Conversion cancelled");}- const file = new File([new Uint8Array(data)], filename, {- type: "application/octet-stream",- });- const tempVFile = new VertFile(file, this.to);+ const tempVFile = new VertFile(+ new File([new Uint8Array(data)], filename, {+ type: "application/octet-stream",+ }),+ this.to,+ );+ tempVFile.from =+ this.archiveFormats?.[index] ?? tempVFile.from;tempVFile.converters = [converter];if (converter.reportsProgress) {