fix: stop uploading on vertd cancel

oops, still uploaded file if during upload phase
This commit is contained in:
Maya 2026-03-28 17:33:49 +03:00
parent 5241b2c117
commit f8f54da581
No known key found for this signature in database
1 changed files with 57 additions and 4 deletions

View File

@ -239,14 +239,19 @@ const processSettings = (settings: ConversionSettings): ConversionSettings => {
return newSettings; return newSettings;
}; };
const uploadFile = async (file: VertFile): Promise<UploadResponse> => { interface UploadTask {
promise: Promise<UploadResponse>;
abort: () => void;
}
const createUploadTask = async (file: VertFile): Promise<UploadTask> => {
const apiUrl = await VertdInstance.instance.url(); const apiUrl = await VertdInstance.instance.url();
const formData = new FormData(); const formData = new FormData();
formData.append("file", file.file, file.name); formData.append("file", file.file, file.name);
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open("POST", `${apiUrl}/api/upload`, true); xhr.open("POST", `${apiUrl}/api/upload`, true);
return new Promise((resolve, reject) => { const promise = new Promise<UploadResponse>((resolve, reject) => {
xhr.upload.addEventListener("progress", (e) => { xhr.upload.addEventListener("progress", (e) => {
console.log(e); console.log(e);
if (e.lengthComputable) { if (e.lengthComputable) {
@ -276,9 +281,18 @@ const uploadFile = async (file: VertFile): Promise<UploadResponse> => {
reject(xhr.statusText); reject(xhr.statusText);
}; };
xhr.onabort = () => {
reject(new Error("Conversion cancelled"));
};
xhr.send(formData); xhr.send(formData);
console.log("sent!"); console.log("sent!");
}); });
return {
promise,
abort: () => xhr.abort(),
};
}; };
const downloadFile = async (url: string, file: VertFile): Promise<Blob> => { const downloadFile = async (url: string, file: VertFile): Promise<Blob> => {
@ -326,6 +340,10 @@ export class VertdConverter extends Converter {
} }
>(); >();
private activeUploads = new Map<string, UploadTask>();
private cancelledConversions = new Set<string>();
public supportedFormats = [ public supportedFormats = [
new FormatInfo("mp4", true, true), new FormatInfo("mp4", true, true),
new FormatInfo("mkv", true, true), new FormatInfo("mkv", true, true),
@ -784,7 +802,18 @@ export class VertdConverter extends Converter {
); );
} }
const uploadRes = await uploadFile(fileUpload); const uploadTask = await createUploadTask(fileUpload);
this.activeUploads.set(input.id, uploadTask);
let uploadRes: UploadResponse;
try {
uploadRes = await uploadTask.promise;
} finally {
this.activeUploads.delete(input.id);
}
if (this.cancelledConversions.has(input.id))
throw new Error("Conversion cancelled");
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let settled = false; let settled = false;
@ -805,6 +834,8 @@ export class VertdConverter extends Converter {
if (settled) return; if (settled) return;
settled = true; settled = true;
clearTimeout(connectTimeout); clearTimeout(connectTimeout);
this.cancelledConversions.delete(input.id);
this.activeUploads.delete(input.id);
this.activeConversions.delete(input.id); this.activeConversions.delete(input.id);
if ( if (
ws.readyState === WebSocket.CONNECTING || ws.readyState === WebSocket.CONNECTING ||
@ -819,6 +850,8 @@ export class VertdConverter extends Converter {
if (settled) return; if (settled) return;
settled = true; settled = true;
clearTimeout(connectTimeout); clearTimeout(connectTimeout);
this.cancelledConversions.delete(input.id);
this.activeUploads.delete(input.id);
this.activeConversions.delete(input.id); this.activeConversions.delete(input.id);
resolve(value); resolve(value);
}; };
@ -830,6 +863,11 @@ export class VertdConverter extends Converter {
}); });
ws.onopen = () => { ws.onopen = () => {
if (this.cancelledConversions.has(input.id)) {
rejectConversion(new Error("Conversion cancelled"));
return;
}
clearTimeout(connectTimeout); clearTimeout(connectTimeout);
this.log( this.log(
`opened ws connection to vertd for file ${input.name}`, `opened ws connection to vertd for file ${input.name}`,
@ -855,6 +893,11 @@ export class VertdConverter extends Converter {
ws.onclose = (event) => { ws.onclose = (event) => {
if (settled) return; if (settled) return;
if (this.cancelledConversions.has(input.id)) {
rejectConversion(new Error("Conversion cancelled"));
return;
}
this.error( this.error(
`ws closed unexpectedly for file ${input.name} (code: ${event.code})`, `ws closed unexpectedly for file ${input.name} (code: ${event.code})`,
); );
@ -968,9 +1011,19 @@ export class VertdConverter extends Converter {
} }
public async cancel(input: VertFile): Promise<void> { public async cancel(input: VertFile): Promise<void> {
this.cancelledConversions.add(input.id);
const activeUpload = this.activeUploads.get(input.id);
if (activeUpload) {
this.log(`cancelling upload for file ${input.name}`);
activeUpload.abort();
this.activeUploads.delete(input.id);
}
const activeConversion = this.activeConversions.get(input.id); const activeConversion = this.activeConversions.get(input.id);
if (!activeConversion) { if (!activeConversion) {
this.error(`no active conversion found for file ${input.name}`); if (!activeUpload)
this.error(`no active conversion found for file ${input.name}`);
return; return;
} }