improved file uploads

This commit is contained in:
2025-07-13 12:35:48 +02:00
parent 548dd7e9ef
commit 596d7ac35d
12 changed files with 370 additions and 87 deletions

View File

@@ -131,7 +131,8 @@ export async function deleteWarrenEntry(
export async function uploadToWarren(
path: string,
files: FileList
files: FileList,
onProgress: ((loaded: number, total: number) => void) | undefined
): Promise<{ success: boolean }> {
const body = new FormData();
for (const file of files) {
@@ -147,16 +148,31 @@ export async function uploadToWarren(
rest = '/' + rest;
}
const { status } = await useFetch(
getApiUrl(`warrens/${warrenId}/upload${rest}`),
{
method: 'POST',
body,
key: 'upload-' + new Date().getTime().toString(),
}
);
const xhr = new XMLHttpRequest();
xhr.open('POST', getApiUrl(`warrens/${warrenId}/upload${rest}`));
xhr.upload.onprogress = (e) => {
onProgress?.(e.loaded, e.total);
};
if (status.value !== 'success') {
const promise = new Promise<void>((res, rej) => {
xhr.onreadystatechange = (_) => {
if (xhr.readyState !== 4) {
return;
}
if (xhr.status === 200) {
res();
} else {
rej();
}
};
});
xhr.send(body);
try {
await promise;
} catch {
toast.error('Upload', {
id: 'UPLOAD_FILE_TOAST',
description: `Failed to upload`,