mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 06:58:46 +00:00
캐시 목록 페이지 추가
This commit is contained in:
@@ -1,20 +1,28 @@
|
||||
import { getFileCacheIndex, storeFileCacheIndex, type FileCacheIndex } from "$lib/indexedDB";
|
||||
import {
|
||||
getFileCacheIndex as getFileCacheIndexFromIndexedDB,
|
||||
storeFileCacheIndex as storeFileCacheIndexToIndexedDB,
|
||||
type FileCacheIndex,
|
||||
} from "$lib/indexedDB";
|
||||
import { readFileFromOpfs, writeFileToOpfs } from "$lib/modules/opfs";
|
||||
|
||||
const fileCacheIndex = new Map<number, FileCacheIndex>();
|
||||
|
||||
export const prepareFileCache = async () => {
|
||||
for (const cache of await getFileCacheIndex()) {
|
||||
for (const cache of await getFileCacheIndexFromIndexedDB()) {
|
||||
fileCacheIndex.set(cache.fileId, cache);
|
||||
}
|
||||
};
|
||||
|
||||
export const getFileCacheIndex = () => {
|
||||
return Array.from(fileCacheIndex.values());
|
||||
};
|
||||
|
||||
export const getFileCache = async (fileId: number) => {
|
||||
const cacheIndex = fileCacheIndex.get(fileId);
|
||||
if (!cacheIndex) return null;
|
||||
|
||||
cacheIndex.lastRetrievedAt = new Date();
|
||||
storeFileCacheIndex(cacheIndex); // Intended
|
||||
storeFileCacheIndexToIndexedDB(cacheIndex); // Intended
|
||||
return await readFileFromOpfs(`/cache/${fileId}`);
|
||||
};
|
||||
|
||||
@@ -29,5 +37,5 @@ export const storeFileCache = async (fileId: number, fileBuffer: ArrayBuffer) =>
|
||||
size: fileBuffer.byteLength,
|
||||
};
|
||||
fileCacheIndex.set(fileId, cacheIndex);
|
||||
await storeFileCacheIndex(cacheIndex);
|
||||
await storeFileCacheIndexToIndexedDB(cacheIndex);
|
||||
};
|
||||
|
||||
@@ -63,7 +63,13 @@ const fetchFileInfo = async (
|
||||
infoStore: Writable<FileInfo | null>,
|
||||
) => {
|
||||
const res = await callGetApi(`/api/file/${fileId}`);
|
||||
if (!res.ok) throw new Error("Failed to fetch file information");
|
||||
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);
|
||||
|
||||
22
src/lib/modules/util.ts
Normal file
22
src/lib/modules/util.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
const pad2 = (num: number) => num.toString().padStart(2, "0");
|
||||
|
||||
export const formatDate = (date: Date) => {
|
||||
const year = date.getFullYear();
|
||||
const month = date.getMonth() + 1;
|
||||
const day = date.getDate();
|
||||
return `${year}. ${month}. ${day}.`;
|
||||
};
|
||||
|
||||
export const formatDateTime = (date: Date) => {
|
||||
const dateFormatted = formatDate(date);
|
||||
const hours = date.getHours();
|
||||
const minutes = date.getMinutes();
|
||||
return `${dateFormatted} ${pad2(hours)}:${pad2(minutes)}`;
|
||||
};
|
||||
|
||||
export const formatFileSize = (size: number) => {
|
||||
if (size < 1024) return `${size} B`;
|
||||
if (size < 1024 * 1024) return `${(size / 1024).toFixed(1)} KiB`;
|
||||
if (size < 1024 * 1024 * 1024) return `${(size / 1024 / 1024).toFixed(1)} MiB`;
|
||||
return `${(size / 1024 / 1024 / 1024).toFixed(1)} GiB`;
|
||||
};
|
||||
Reference in New Issue
Block a user