파일 다운로드 스케쥴링 및 진행률 표시 기능 구현

This commit is contained in:
static
2025-01-18 08:20:09 +09:00
parent 620d174e9b
commit bde090c464
7 changed files with 214 additions and 73 deletions

View File

@@ -1,5 +1,5 @@
import { getFileCache, storeFileCache } from "$lib/modules/file";
import { decryptData } from "$lib/modules/crypto";
import { getFileCache, storeFileCache, downloadFile } from "$lib/modules/file";
import { formatFileSize } from "$lib/modules/util";
export const requestFileDownload = async (
fileId: number,
@@ -9,28 +9,15 @@ export const requestFileDownload = async (
const cache = await getFileCache(fileId);
if (cache) return cache;
return new Promise<ArrayBuffer>((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.responseType = "arraybuffer";
xhr.addEventListener("load", async () => {
if (xhr.status !== 200) {
reject(new Error("Failed to download file"));
return;
}
const fileDecrypted = await decryptData(
xhr.response as ArrayBuffer,
fileEncryptedIv,
dataKey,
);
resolve(fileDecrypted);
await storeFileCache(fileId, fileDecrypted);
});
// TODO: Progress, ...
xhr.open("GET", `/api/file/${fileId}/download`);
xhr.send();
});
const fileBuffer = await downloadFile(fileId, fileEncryptedIv, dataKey);
storeFileCache(fileId, fileBuffer); // Intended
return fileBuffer;
};
export const formatDownloadProgress = (progress?: number) => {
return `${Math.floor((progress ?? 0) * 100)}%`;
};
export const formatDownloadRate = (rate?: number) => {
return `${formatFileSize((rate ?? 0) / 8)}/s`;
};