Skip to content

armgit / VERT

Public Imported from GitHub
0

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

fix: code review suggestions

JovannMCe4d00499d571
diff --git a/src/lib/converters/converter.svelte.ts b/src/lib/converters/converter.svelte.ts
index 95c76f7..8afd09e 100644
--- a/src/lib/converters/converter.svelte.ts
+++ b/src/lib/converters/converter.svelte.ts
@@ -48,6 +48,7 @@ export class Converter {
public readonly reportsProgress: boolean = false;
private timeoutId?: ReturnType<typeof setTimeout>;
+ private activeInput?: VertFile;
constructor(public readonly timeout: number = 10) {
this.startTimeout();
@@ -94,10 +95,20 @@ export class Converter {
private startTimeout() {
this.timeoutId = setTimeout(() => {
- if (this.status !== "ready") this.status = "not-ready";
+ if (this.status === "ready") return;
+ this.status = "not-ready";
+ if (this.activeInput) void this.cancel(this.activeInput);
}, this.timeout * 1000);
}
+ protected trackConversion(input: VertFile) {
+ this.activeInput = input;
+ }
+
+ protected clearTrackedConversion(input: VertFile) {
+ if (this.activeInput?.id === input.id) this.activeInput = undefined;
+ }
+
protected clearTimeout() {
if (this.timeoutId) {
clearTimeout(this.timeoutId);
diff --git a/src/lib/converters/ffmpeg/ffmpeg.svelte.ts b/src/lib/converters/ffmpeg/ffmpeg.svelte.ts
index f41167f..4a42b9f 100644
--- a/src/lib/converters/ffmpeg/ffmpeg.svelte.ts
+++ b/src/lib/converters/ffmpeg/ffmpeg.svelte.ts
@@ -214,6 +214,7 @@ export class FFmpegConverter extends Converter {
to: string,
settings: ConversionSettings,
): Promise<VertFile> {
+ this.trackConversion(input);
if (!to.startsWith(".")) to = `.${to}`;
const conversionSettings =
@@ -323,6 +324,7 @@ export class FFmpegConverter extends Converter {
return new VertFile(new File([outBuf], outputFileName), to);
}
} finally {
+ this.clearTrackedConversion(input);
ffmpeg.off("log", errorListener);
ffmpeg.off("log", logListener);
this.activeConversions.delete(input.id);
diff --git a/src/lib/converters/magick/magick.svelte.ts b/src/lib/converters/magick/magick.svelte.ts
index a9fdcfd..01dbb8a 100644
--- a/src/lib/converters/magick/magick.svelte.ts
+++ b/src/lib/converters/magick/magick.svelte.ts
@@ -249,6 +249,7 @@ export class MagickConverter extends Converter {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...args: any[]
): Promise<VertFile> {
+ this.trackConversion(input);
this.log(`converting ${input.name} to ${to}`);
// handle converting from SVG manually because magick-wasm doesn't support it
@@ -348,6 +349,7 @@ export class MagickConverter extends Converter {
throw new Error("Unknown message type");
} finally {
+ this.clearTrackedConversion(input);
this.activeConversions.delete(input.id);
worker.terminate();
}
diff --git a/src/lib/converters/mediabunny/mediabunny.svelte.ts b/src/lib/converters/mediabunny/mediabunny.svelte.ts
index 8b2a829..ec86e2c 100644
--- a/src/lib/converters/mediabunny/mediabunny.svelte.ts
+++ b/src/lib/converters/mediabunny/mediabunny.svelte.ts
@@ -391,6 +391,7 @@ export class MediabunnyConverter extends Converter {
to: string,
settings: ConversionSettings,
): Promise<VertFile> {
+ this.trackConversion(file);
const toFormat = to.startsWith(".") ? to.slice(1) : to;
const originalName = file.file.name.split(".").slice(0, -1).join(".");
const outputFilename = `${originalName}.${toFormat}`;
@@ -507,6 +508,7 @@ export class MediabunnyConverter extends Converter {
}
throw err;
} finally {
+ this.clearTrackedConversion(file);
if (slowConversionTimer) clearTimeout(slowConversionTimer);
this.activeConversions.delete(file.id);
}
diff --git a/src/lib/converters/pandoc/pandoc.svelte.ts b/src/lib/converters/pandoc/pandoc.svelte.ts
index bd161d0..6000aa4 100644
--- a/src/lib/converters/pandoc/pandoc.svelte.ts
+++ b/src/lib/converters/pandoc/pandoc.svelte.ts
@@ -43,6 +43,7 @@ export class PandocConverter extends Converter {
}
public async convert(file: VertFile, to: string): Promise<VertFile> {
+ this.trackConversion(file);
const worker = new Worker(PandocWorker, {
type: "module",
});
@@ -110,6 +111,7 @@ export class PandocConverter extends Converter {
result.isZip ? ".zip" : to,
);
} finally {
+ this.clearTrackedConversion(file);
this.activeConversions.delete(file.id);
worker.terminate();
}
diff --git a/src/lib/converters/vertd/vertd.svelte.ts b/src/lib/converters/vertd/vertd.svelte.ts
index 73ccaef..9344a07 100644
--- a/src/lib/converters/vertd/vertd.svelte.ts
+++ b/src/lib/converters/vertd/vertd.svelte.ts
@@ -620,6 +620,7 @@ export class VertdConverter extends Converter {
to: string,
settings: ConversionSettings,
): Promise<VertFile> {
+ this.trackConversion(input);
if (to.startsWith(".")) to = to.slice(1);
const fileUpload = input;
@@ -852,6 +853,7 @@ export class VertdConverter extends Converter {
}
};
});
+ this.clearTrackedConversion(input);
}
public async cancel(input: VertFile): Promise<void> {
diff --git a/src/lib/types/file.svelte.ts b/src/lib/types/file.svelte.ts
index f2470b3..5e2a4b8 100644
--- a/src/lib/types/file.svelte.ts
+++ b/src/lib/types/file.svelte.ts
@@ -547,6 +547,12 @@ export class VertFile {
const totalFiles = entries.length;
const fileProgress: number[] = new Array(totalFiles).fill(0);
const convertedFiles: File[] = [];
+ const progressFiles = new Map<number, VertFile>();
+ const progressInterval = setInterval(() => {
+ for (const [index, file] of progressFiles)
+ fileProgress[index] = file.progress;
+ updateProgress();
+ }, 100);
const queue = new PQueue({
concurrency: navigator.hardwareConcurrency || 4,
@@ -557,68 +563,53 @@ export class VertFile {
this.progress = Math.round(totalProgress / totalFiles);
};
- // convert all files in the zip
- await queue.addAll(
- entries.map(({ filename, data }, index) => async () => {
- if (this.cancelled) {
- throw new Error("Conversion cancelled");
- }
-
- const file = new File([new Uint8Array(data)], filename, {
- type: "application/octet-stream",
- });
- const tempVFile = new VertFile(file, this.to);
- tempVFile.converters = [converter];
+ try {
+ // convert all files in the zip
+ await queue.addAll(
+ entries.map(({ filename, data }, index) => async () => {
+ if (this.cancelled) {
+ throw new Error("Conversion cancelled");
+ }
- if (converter.reportsProgress) {
- // track progress of individual files
- const progressInterval = setInterval(() => {
- fileProgress[index] = tempVFile.progress;
- updateProgress();
- }, 100);
+ const file = new File([new Uint8Array(data)], filename, {
+ type: "application/octet-stream",
+ });
+ const tempVFile = new VertFile(file, this.to);
+ tempVFile.converters = [converter];
+
+ if (converter.reportsProgress) {
+ progressFiles.set(index, tempVFile);
+ try {
+ const converted = await converter.convert(
+ tempVFile,
+ this.to,
+ settings,
+ );
- try {
+ convertedFiles[index] = converted.file;
+ fileProgress[index] = 100;
+ updateProgress();
+ } finally {
+ progressFiles.delete(index);
+ }
+ } else {
+ // else track progress via completions only
const converted = await converter.convert(
tempVFile,
this.to,
settings,
);
- let outputExt = this.to;
- if (!outputExt.startsWith("."))
- outputExt = `.${outputExt}`;
-
- convertedFiles[index] = new File(
- [await converted.file.arrayBuffer()],
- converted.name,
- );
+ convertedFiles[index] = converted.file;
fileProgress[index] = 100;
updateProgress();
- } finally {
- clearInterval(progressInterval);
}
- } else {
- // else track progress via completions only
- const converted = await converter.convert(
- tempVFile,
- this.to,
- settings,
- );
-
- let outputExt = this.to;
- if (!outputExt.startsWith(".")) outputExt = `.${outputExt}`;
-
- convertedFiles[index] = new File(
- [await converted.file.arrayBuffer()],
- converted.name,
- );
-
- fileProgress[index] = 100;
- updateProgress();
- }
- }),
- );
+ }),
+ );
+ } finally {
+ clearInterval(progressInterval);
+ }
// return zip of converted files
const resultArray = await createZip(convertedFiles);