mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-18 07:58:57 +00:00
파일 및 디렉터리 목록을 IndexedDB에 캐싱하도록 구현
This commit is contained in:
@@ -1,3 +1,2 @@
|
||||
export * from "./cache";
|
||||
export * from "./info";
|
||||
export * from "./upload";
|
||||
|
||||
@@ -1,104 +0,0 @@
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
import { callGetApi } from "$lib/hooks";
|
||||
import { unwrapDataKey, decryptString } from "$lib/modules/crypto";
|
||||
import type { DirectoryInfoResponse, FileInfoResponse } from "$lib/server/schemas";
|
||||
import {
|
||||
directoryInfoStore,
|
||||
fileInfoStore,
|
||||
type DirectoryInfo,
|
||||
type FileInfo,
|
||||
} from "$lib/stores/file";
|
||||
|
||||
const fetchDirectoryInfo = async (
|
||||
directoryId: "root" | number,
|
||||
masterKey: CryptoKey,
|
||||
infoStore: Writable<DirectoryInfo | null>,
|
||||
) => {
|
||||
const res = await callGetApi(`/api/directory/${directoryId}`);
|
||||
if (!res.ok) throw new Error("Failed to fetch directory information");
|
||||
const { metadata, subDirectories, files }: DirectoryInfoResponse = await res.json();
|
||||
|
||||
let newInfo: DirectoryInfo;
|
||||
if (directoryId === "root") {
|
||||
newInfo = {
|
||||
id: "root",
|
||||
subDirectoryIds: subDirectories,
|
||||
fileIds: files,
|
||||
};
|
||||
} else {
|
||||
const { dataKey } = await unwrapDataKey(metadata!.dek, masterKey);
|
||||
newInfo = {
|
||||
id: directoryId,
|
||||
dataKey,
|
||||
dataKeyVersion: new Date(metadata!.dekVersion),
|
||||
name: await decryptString(metadata!.name, metadata!.nameIv, dataKey),
|
||||
subDirectoryIds: subDirectories,
|
||||
fileIds: files,
|
||||
};
|
||||
}
|
||||
|
||||
infoStore.update(() => newInfo);
|
||||
};
|
||||
|
||||
export const getDirectoryInfo = (directoryId: "root" | number, masterKey: CryptoKey) => {
|
||||
// TODO: MEK rotation
|
||||
|
||||
let info = directoryInfoStore.get(directoryId);
|
||||
if (!info) {
|
||||
info = writable(null);
|
||||
directoryInfoStore.set(directoryId, info);
|
||||
}
|
||||
|
||||
fetchDirectoryInfo(directoryId, masterKey, info);
|
||||
return info;
|
||||
};
|
||||
|
||||
const decryptDate = async (ciphertext: string, iv: string, dataKey: CryptoKey) => {
|
||||
return new Date(parseInt(await decryptString(ciphertext, iv, dataKey), 10));
|
||||
};
|
||||
|
||||
const fetchFileInfo = async (
|
||||
fileId: number,
|
||||
masterKey: CryptoKey,
|
||||
infoStore: Writable<FileInfo | null>,
|
||||
) => {
|
||||
const res = await callGetApi(`/api/file/${fileId}`);
|
||||
if (!res.ok) {
|
||||
if (res.status === 404) {
|
||||
infoStore.update(() => null);
|
||||
return;
|
||||
}
|
||||
throw new Error("Failed to fetch file information");
|
||||
}
|
||||
const metadata: FileInfoResponse = await res.json();
|
||||
|
||||
const { dataKey } = await unwrapDataKey(metadata.dek, masterKey);
|
||||
const newInfo: FileInfo = {
|
||||
id: fileId,
|
||||
dataKey,
|
||||
dataKeyVersion: new Date(metadata.dekVersion),
|
||||
contentType: metadata.contentType,
|
||||
contentIv: metadata.contentIv,
|
||||
name: await decryptString(metadata.name, metadata.nameIv, dataKey),
|
||||
createdAt:
|
||||
metadata.createdAt && metadata.createdAtIv
|
||||
? await decryptDate(metadata.createdAt, metadata.createdAtIv, dataKey)
|
||||
: undefined,
|
||||
lastModifiedAt: await decryptDate(metadata.lastModifiedAt, metadata.lastModifiedAtIv, dataKey),
|
||||
};
|
||||
|
||||
infoStore.update(() => newInfo);
|
||||
};
|
||||
|
||||
export const getFileInfo = (fileId: number, masterKey: CryptoKey) => {
|
||||
// TODO: MEK rotation
|
||||
|
||||
let info = fileInfoStore.get(fileId);
|
||||
if (!info) {
|
||||
info = writable(null);
|
||||
fileInfoStore.set(fileId, info);
|
||||
}
|
||||
|
||||
fetchFileInfo(fileId, masterKey, info);
|
||||
return info;
|
||||
};
|
||||
@@ -191,7 +191,7 @@ export const uploadFile = async (
|
||||
form.set(
|
||||
"metadata",
|
||||
JSON.stringify({
|
||||
parentId,
|
||||
parent: parentId,
|
||||
mekVersion: masterKey.version,
|
||||
dek: dataKeyWrapped,
|
||||
dekVersion: dataKeyVersion.toISOString(),
|
||||
|
||||
Reference in New Issue
Block a user