Skip to content

armgit / VERT

Public Imported from GitHub
0

The next-generation file converter. Open source, fully local* and free forever.

fix: preserve source extensions for file-type detection, output directly if single-frame animated sequence

JovannMC79bf48df13d3
diff --git a/src/lib/types/file.svelte.ts b/src/lib/types/file.svelte.ts
index 5e2a4b8..4c3738e 100644
--- a/src/lib/types/file.svelte.ts
+++ b/src/lib/types/file.svelte.ts
@@ -97,6 +97,7 @@ export type UnavailableReasons = "vertd-size-limit" | "other-reason"; // find mo
export class VertFile {
public id: string = Math.random().toString(36).slice(2, 8);
public readonly file: File;
+ public readonly originalFrom: string;
public from = $state("");
public name = $state("");
@@ -123,6 +124,7 @@ export class VertFile {
private retryingFallback = false;
private postDownload: (() => Promise<void>) | null = null;
private activeConverterName: string | null = null;
+ private fileTypeMismatchShown = false;
constructor(file: File, to: string, blobUrl?: string) {
const ext = file.name.split(".").pop();
@@ -133,6 +135,7 @@ export class VertFile {
this.file = newFile;
this.name = newFile.name;
this.from = ("." + ext || "").toLowerCase();
+ this.originalFrom = this.from;
this.to = to.startsWith(".") ? to : `.${to}`;
this.converters = converters.filter((c) =>
c.formatStrings().includes(this.from),
@@ -299,28 +302,33 @@ export class VertFile {
mpe: "mpeg",
mpv: "mpeg",
};
- const fileExtension = this.from.slice(1);
+ const fileExtension = this.originalFrom.slice(1);
const detectedExtension = forceKeep.includes(fileExtension)
? fileExtension
: (aliases[this.fileType.ext] ?? this.fileType.ext);
const expectedExtension = aliases[fileExtension] ?? fileExtension;
if (detectedExtension !== expectedExtension) {
- console.warn(
- `file type mismatched: expected ${expectedExtension}, detected ${detectedExtension}`,
- );
- ToastManager.add({
- type: "warning",
- disappearing: false,
- message: m["workers.warnings.file_type_mismatch"]({
- filename: this.file.name,
- expected: expectedExtension,
- actual: detectedExtension,
- }),
- });
+ this.from = `.${detectedExtension}`;
+ if (!this.fileTypeMismatchShown) {
+ console.warn(
+ `file type mismatched: expected ${expectedExtension}, detected ${detectedExtension}`,
+ );
+ ToastManager.add({
+ type: "warning",
+ disappearing: false,
+ message: m["workers.warnings.file_type_mismatch"]({
+ filename: this.file.name,
+ expected: expectedExtension,
+ actual: detectedExtension,
+ }),
+ });
+ this.fileTypeMismatchShown = true;
+ }
+ } else {
+ this.from = this.originalFrom;
}
- this.from = `.${detectedExtension}`;
this.converters = converters.filter((converter) =>
converter.formatStrings().includes(this.from),
);
diff --git a/src/lib/workers/magick.ts b/src/lib/workers/magick.ts
index e38e645..be50de9 100644
--- a/src/lib/workers/magick.ts
+++ b/src/lib/workers/magick.ts
@@ -8,6 +8,7 @@ import {
MagickReadSettings,
AlphaAction,
type IMagickImage,
+ type IMagickImageCollection,
} from "@imagemagick/magick-wasm";
import { makeZip } from "client-zip";
import type { WorkerMessage } from "$lib/types";
@@ -117,34 +118,22 @@ const readToEnd = async (reader: ReadableStreamDefaultReader<Uint8Array>) => {
};
const convertCollectionToZip = async (
- buffer: ArrayBuffer,
- format: MagickFormat,
+ collection: IMagickImageCollection,
to: string,
conversionSettings: ConversionSettings,
): Promise<Uint8Array> => {
- const collection = MagickImageCollection.create();
- try {
- collection.read(
- new Uint8Array(buffer),
- new MagickReadSettings({ format }),
- );
- console.log(`read ${collection.length} images from ${format}`);
-
- const convertedImages = await Promise.all(
- collection.map((img) => magickConvert(img, to, conversionSettings)),
- );
- const zip = makeZip(
- convertedImages.map(
- (img, i) =>
- new File([new Uint8Array(img)], `image${i}.${to.slice(1)}`),
- ),
- "images.zip",
- );
+ const convertedImages = await Promise.all(
+ collection.map((img) => magickConvert(img, to, conversionSettings)),
+ );
+ const zip = makeZip(
+ convertedImages.map(
+ (img, i) =>
+ new File([new Uint8Array(img)], `image${i}.${to.slice(1)}`),
+ ),
+ "images.zip",
+ );
- return await readToEnd(zip.getReader());
- } finally {
- collection.dispose();
- }
+ return await readToEnd(zip.getReader());
};
const loadDicomHelpers = async () =>
@@ -191,39 +180,53 @@ const handleSpecialOutput = async (
};
const collectionFormat = collectionFormats[from];
if (collectionFormat !== undefined) {
- const zipBytes = await convertCollectionToZip(
- buffer,
- collectionFormat,
- to,
- conversionSettings,
- );
-
- return {
- type: "finished",
- output: zipBytes,
- zip: true,
- };
- }
+ const collection = MagickImageCollection.create();
+ try {
+ collection.read(
+ new Uint8Array(buffer),
+ new MagickReadSettings({ format: collectionFormat }),
+ );
+ console.log(
+ `read ${collection.length} images from ${collectionFormat}`,
+ );
- // converting from animated format to another animated format
- // APNG does not work on magick-wasm since it needs ffmpeg built-in (not in magick-wasm) - should pass into ffmpeg.wasm instead
- if (
- (from === ".webp" || from === ".gif") &&
- (to === ".gif" || to === ".webp")
- ) {
- const collection = MagickImageCollection.create(new Uint8Array(buffer));
- const format = to === ".gif" ? MagickFormat.Gif : MagickFormat.WebP;
- const result = await new Promise<Uint8Array>((resolve) => {
- collection.write(format, (output) => {
- resolve(structuredClone(output));
- });
- });
- collection.dispose();
+ if (from === ".gif" || from === ".webp") {
+ if (collection.length === 1)
+ return {
+ type: "finished",
+ output: await magickConvert(
+ collection[0],
+ to,
+ conversionSettings,
+ ),
+ };
+
+ // converting from animated format to another animated format
+ // APNG does not work on magick-wasm since it needs ffmpeg built-in (not in magick-wasm) - should pass into ffmpeg.wasm instead
+ if (to === ".gif" || to === ".webp") {
+ const format =
+ to === ".gif" ? MagickFormat.Gif : MagickFormat.WebP;
+ const result = await new Promise<Uint8Array>((resolve) => {
+ collection.write(format, (output) => {
+ resolve(structuredClone(output));
+ });
+ });
+ return { type: "finished", output: result };
+ }
+ }
- return {
- type: "finished",
- output: result,
- };
+ return {
+ type: "finished",
+ output: await convertCollectionToZip(
+ collection,
+ to,
+ conversionSettings,
+ ),
+ zip: true,
+ };
+ } finally {
+ collection.dispose();
+ }
}
if (from === ".ani") {