mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-15 22:38:47 +00:00
파일 다운로드 스케쥴링 및 진행률 표시 기능 구현
This commit is contained in:
@@ -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`;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user