Can download ISO files

This commit is contained in:
2023-09-06 16:57:38 +02:00
parent 8f65196344
commit fbe11af121
8 changed files with 222 additions and 19 deletions

View File

@ -6,7 +6,8 @@ interface RequestParams {
allowFail?: boolean;
jsonData?: any;
formData?: FormData;
progress?: (progress: number) => void;
upProgress?: (progress: number) => void;
downProgress?: (e: { progress: number; total: number }) => void;
}
interface APIResponse {
@ -62,11 +63,11 @@ export class APIClient {
let status: number;
// Make the request with XMLHttpRequest
if (args.progress) {
if (args.upProgress) {
const res: XMLHttpRequest = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", (e) =>
args.progress!(e.loaded / e.total)
args.upProgress!(e.loaded / e.total)
);
xhr.addEventListener("load", () => resolve(xhr));
xhr.addEventListener("error", () =>
@ -104,6 +105,48 @@ export class APIClient {
// Process response
if (res.headers.get("content-type") === "application/json")
data = await res.json();
// Binary file
else if (res.body !== null && args.downProgress) {
// Track download progress
const contentEncoding = res.headers.get("content-encoding");
const contentLength = contentEncoding
? null
: res.headers.get("content-length");
const total = parseInt(contentLength ?? "0", 10);
let loaded = 0;
const resInt = new Response(
new ReadableStream({
start(controller) {
const reader = res.body!.getReader();
const read = async () => {
try {
const ret = await reader.read();
if (ret.done) {
controller.close();
return;
}
loaded += ret.value.byteLength;
args.downProgress!({ progress: loaded, total });
controller.enqueue(ret.value);
read();
} catch (e) {
console.error(e);
controller.error(e);
}
};
read();
},
})
);
data = await resInt.blob();
}
// Do not track progress
else data = await res.blob();
status = res.status;

View File

@ -20,7 +20,7 @@ export class IsoFilesApi {
method: "POST",
uri: "/iso/upload",
formData: fd,
progress: progress,
upProgress: progress,
});
}
@ -47,6 +47,24 @@ export class IsoFilesApi {
).data;
}
/**
* Download an ISO file
*/
static async Download(
file: IsoFile,
progress: (p: number) => void
): Promise<Blob> {
return (
await APIClient.exec({
method: "GET",
uri: `/iso/${file.filename}`,
downProgress(e) {
progress(Math.floor(100 * (e.progress / e.total)));
},
})
).data;
}
/**
* Delete iso file
*/