캐시 목록 페이지 추가

This commit is contained in:
static
2025-01-14 03:07:54 +09:00
parent ea0f0e4a71
commit f37df53991
10 changed files with 194 additions and 36 deletions

View File

@@ -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);
};

View File

@@ -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
View 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`;
};