mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 15:08:46 +00:00
파일 다운로드 스케쥴링 및 진행률 표시 기능 구현
This commit is contained in:
84
src/lib/modules/file/download.ts
Normal file
84
src/lib/modules/file/download.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import axios from "axios";
|
||||
import { limitFunction } from "p-limit";
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
import { decryptData } from "$lib/modules/crypto";
|
||||
import { fileDownloadStatusStore, type FileDownloadStatus } from "$lib/stores";
|
||||
|
||||
const requestFileDownload = limitFunction(
|
||||
async (status: Writable<FileDownloadStatus>, id: number) => {
|
||||
status.update((value) => {
|
||||
value.status = "downloading";
|
||||
return value;
|
||||
});
|
||||
|
||||
const res = await axios.get(`/api/file/${id}/download`, {
|
||||
responseType: "arraybuffer",
|
||||
onDownloadProgress: ({ progress, rate, estimated }) => {
|
||||
status.update((value) => {
|
||||
value.progress = progress;
|
||||
value.rate = rate;
|
||||
value.estimated = estimated;
|
||||
return value;
|
||||
});
|
||||
},
|
||||
});
|
||||
const fileEncrypted: ArrayBuffer = res.data;
|
||||
|
||||
status.update((value) => {
|
||||
value.status = "decryption-pending";
|
||||
return value;
|
||||
});
|
||||
return fileEncrypted;
|
||||
},
|
||||
{ concurrency: 1 },
|
||||
);
|
||||
|
||||
const decryptFile = limitFunction(
|
||||
async (
|
||||
status: Writable<FileDownloadStatus>,
|
||||
fileEncrypted: ArrayBuffer,
|
||||
fileEncryptedIv: string,
|
||||
dataKey: CryptoKey,
|
||||
) => {
|
||||
status.update((value) => {
|
||||
value.status = "decrypting";
|
||||
return value;
|
||||
});
|
||||
|
||||
const fileBuffer = await decryptData(fileEncrypted, fileEncryptedIv, dataKey);
|
||||
|
||||
status.update((value) => {
|
||||
value.status = "decrypted";
|
||||
value.result = fileBuffer;
|
||||
return value;
|
||||
});
|
||||
return fileBuffer;
|
||||
},
|
||||
{ concurrency: 4 },
|
||||
);
|
||||
|
||||
export const downloadFile = async (id: number, fileEncryptedIv: string, dataKey: CryptoKey) => {
|
||||
const status = writable<FileDownloadStatus>({
|
||||
id,
|
||||
status: "download-pending",
|
||||
});
|
||||
fileDownloadStatusStore.update((value) => {
|
||||
value.push(status);
|
||||
return value;
|
||||
});
|
||||
|
||||
try {
|
||||
return await decryptFile(
|
||||
status,
|
||||
await requestFileDownload(status, id),
|
||||
fileEncryptedIv,
|
||||
dataKey,
|
||||
);
|
||||
} catch (e) {
|
||||
status.update((value) => {
|
||||
value.status = "error";
|
||||
return value;
|
||||
});
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from "./cache";
|
||||
export * from "./download";
|
||||
export * from "./upload";
|
||||
|
||||
@@ -120,7 +120,7 @@ const encryptFile = limitFunction(
|
||||
{ concurrency: 4 },
|
||||
);
|
||||
|
||||
const uploadFileInternal = limitFunction(
|
||||
const requestFileUpload = limitFunction(
|
||||
async (status: Writable<FileUploadStatus>, form: FormData) => {
|
||||
status.update((value) => {
|
||||
value.status = "uploading";
|
||||
@@ -209,7 +209,7 @@ export const uploadFile = async (
|
||||
);
|
||||
form.set("content", new Blob([fileEncrypted.ciphertext]));
|
||||
|
||||
await uploadFileInternal(status, form);
|
||||
await requestFileUpload(status, form);
|
||||
return true;
|
||||
} catch (e) {
|
||||
status.update((value) => {
|
||||
|
||||
@@ -15,4 +15,22 @@ export interface FileUploadStatus {
|
||||
estimated?: number;
|
||||
}
|
||||
|
||||
export interface FileDownloadStatus {
|
||||
id: number;
|
||||
status:
|
||||
| "download-pending"
|
||||
| "downloading"
|
||||
| "decryption-pending"
|
||||
| "decrypting"
|
||||
| "decrypted"
|
||||
| "canceled"
|
||||
| "error";
|
||||
progress?: number;
|
||||
rate?: number;
|
||||
estimated?: number;
|
||||
result?: ArrayBuffer;
|
||||
}
|
||||
|
||||
export const fileUploadStatusStore = writable<Writable<FileUploadStatus>[]>([]);
|
||||
|
||||
export const fileDownloadStatusStore = writable<Writable<FileDownloadStatus>[]>([]);
|
||||
|
||||
Reference in New Issue
Block a user