캐시 삭제 구현

This commit is contained in:
static
2025-01-14 03:26:32 +09:00
parent f37df53991
commit 27d2b83464
7 changed files with 55 additions and 19 deletions

View File

@@ -22,3 +22,7 @@ export const getFileCacheIndex = async () => {
export const storeFileCacheIndex = async (fileCacheIndex: FileCacheIndex) => { export const storeFileCacheIndex = async (fileCacheIndex: FileCacheIndex) => {
await cacheIndex.fileCache.put(fileCacheIndex); await cacheIndex.fileCache.put(fileCacheIndex);
}; };
export const deleteFileCacheIndex = async (fileId: number) => {
await cacheIndex.fileCache.delete(fileId);
};

View File

@@ -1,9 +1,10 @@
import { import {
getFileCacheIndex as getFileCacheIndexFromIndexedDB, getFileCacheIndex as getFileCacheIndexFromIndexedDB,
storeFileCacheIndex as storeFileCacheIndexToIndexedDB, storeFileCacheIndex,
deleteFileCacheIndex,
type FileCacheIndex, type FileCacheIndex,
} from "$lib/indexedDB"; } from "$lib/indexedDB";
import { readFileFromOpfs, writeFileToOpfs } from "$lib/modules/opfs"; import { readFile, writeFile, deleteFile } from "$lib/modules/opfs";
const fileCacheIndex = new Map<number, FileCacheIndex>(); const fileCacheIndex = new Map<number, FileCacheIndex>();
@@ -22,13 +23,13 @@ export const getFileCache = async (fileId: number) => {
if (!cacheIndex) return null; if (!cacheIndex) return null;
cacheIndex.lastRetrievedAt = new Date(); cacheIndex.lastRetrievedAt = new Date();
storeFileCacheIndexToIndexedDB(cacheIndex); // Intended storeFileCacheIndex(cacheIndex); // Intended
return await readFileFromOpfs(`/cache/${fileId}`); return await readFile(`/cache/${fileId}`);
}; };
export const storeFileCache = async (fileId: number, fileBuffer: ArrayBuffer) => { export const storeFileCache = async (fileId: number, fileBuffer: ArrayBuffer) => {
const now = new Date(); const now = new Date();
await writeFileToOpfs(`/cache/${fileId}`, fileBuffer); await writeFile(`/cache/${fileId}`, fileBuffer);
const cacheIndex: FileCacheIndex = { const cacheIndex: FileCacheIndex = {
fileId, fileId,
@@ -37,5 +38,11 @@ export const storeFileCache = async (fileId: number, fileBuffer: ArrayBuffer) =>
size: fileBuffer.byteLength, size: fileBuffer.byteLength,
}; };
fileCacheIndex.set(fileId, cacheIndex); fileCacheIndex.set(fileId, cacheIndex);
await storeFileCacheIndexToIndexedDB(cacheIndex); await storeFileCacheIndex(cacheIndex);
};
export const deleteFileCache = async (fileId: number) => {
await deleteFile(`/cache/${fileId}`);
fileCacheIndex.delete(fileId);
await deleteFileCacheIndex(fileId);
}; };

View File

@@ -18,31 +18,32 @@ const getFileHandle = async (path: string, create = true) => {
try { try {
let directoryHandle: FileSystemDirectoryHandle = rootHandle; let directoryHandle: FileSystemDirectoryHandle = rootHandle;
for (const part of parts.slice(0, -1)) { for (const part of parts.slice(0, -1)) {
if (!part) continue; if (!part) continue;
directoryHandle = await directoryHandle.getDirectoryHandle(part, { create }); directoryHandle = await directoryHandle.getDirectoryHandle(part, { create });
} }
return directoryHandle.getFileHandle(parts[parts.length - 1]!, { create }); const filename = parts[parts.length - 1]!;
const fileHandle = await directoryHandle.getFileHandle(filename, { create });
return { parentHandle: directoryHandle, filename, fileHandle };
} catch (e) { } catch (e) {
if (e instanceof DOMException && e.name === "NotFoundError") { if (e instanceof DOMException && e.name === "NotFoundError") {
return null; return {};
} }
throw e; throw e;
} }
}; };
export const readFileFromOpfs = async (path: string) => { export const readFile = async (path: string) => {
const fileHandle = await getFileHandle(path, false); const { fileHandle } = await getFileHandle(path, false);
if (!fileHandle) return null; if (!fileHandle) return null;
const file = await fileHandle.getFile(); const file = await fileHandle.getFile();
return await file.arrayBuffer(); return await file.arrayBuffer();
}; };
export const writeFileToOpfs = async (path: string, data: ArrayBuffer) => { export const writeFile = async (path: string, data: ArrayBuffer) => {
const fileHandle = await getFileHandle(path); const { fileHandle } = await getFileHandle(path);
const writable = await fileHandle!.createWritable(); const writable = await fileHandle!.createWritable();
try { try {
@@ -51,3 +52,10 @@ export const writeFileToOpfs = async (path: string, data: ArrayBuffer) => {
await writable.close(); await writable.close();
} }
}; };
export const deleteFile = async (path: string) => {
const { parentHandle, filename } = await getFileHandle(path, false);
if (!parentHandle) return;
await parentHandle.removeEntry(filename);
};

View File

@@ -7,7 +7,7 @@
import { getFileInfo } from "$lib/modules/file"; import { getFileInfo } from "$lib/modules/file";
import { masterKeyStore, type FileInfo } from "$lib/stores"; import { masterKeyStore, type FileInfo } from "$lib/stores";
import File from "./File.svelte"; import File from "./File.svelte";
import { formatFileSize } from "./service"; import { formatFileSize, deleteFileCache as doDeleteFileCache } from "./service";
interface FileCache { interface FileCache {
index: FileCacheIndex; index: FileCacheIndex;
@@ -17,6 +17,11 @@
let fileCache: FileCache[] | undefined = $state(); let fileCache: FileCache[] | undefined = $state();
let fileCacheTotalSize = $state(0); let fileCacheTotalSize = $state(0);
const deleteFileCache = async (fileId: number) => {
await doDeleteFileCache(fileId);
fileCache = fileCache?.filter(({ index }) => index.fileId !== fileId);
};
onMount(() => { onMount(() => {
fileCache = getFileCacheIndex() fileCache = getFileCacheIndex()
.map((index) => ({ .map((index) => ({
@@ -45,7 +50,7 @@
</div> </div>
<div class="space-y-2"> <div class="space-y-2">
{#each fileCache as { index, fileInfo }} {#each fileCache as { index, fileInfo }}
<File {index} info={fileInfo} /> <File {index} info={fileInfo} onDeleteClick={deleteFileCache} />
{/each} {/each}
</div> </div>
</div> </div>

View File

@@ -6,14 +6,15 @@
import IconDraft from "~icons/material-symbols/draft"; import IconDraft from "~icons/material-symbols/draft";
import IconScanDelete from "~icons/material-symbols/scan-delete"; import IconScanDelete from "~icons/material-symbols/scan-delete";
// import IconDelete from "~icons/material-symbols/delete"; import IconDelete from "~icons/material-symbols/delete";
interface Props { interface Props {
index: FileCacheIndex; index: FileCacheIndex;
info: Writable<FileInfo | null>; info: Writable<FileInfo | null>;
onDeleteClick: (fileId: number) => void;
} }
let { index, info }: Props = $props(); let { index, info, onDeleteClick }: Props = $props();
</script> </script>
<div class="flex h-14 items-center gap-x-4 p-2"> <div class="flex h-14 items-center gap-x-4 p-2">
@@ -36,7 +37,10 @@
읽음 {formatDate(index.lastRetrievedAt)} · {formatFileSize(index.size)} 읽음 {formatDate(index.lastRetrievedAt)} · {formatFileSize(index.size)}
</p> </p>
</div> </div>
<!-- <button class="flex-shrink-0 rounded-full p-1 active:bg-gray-100"> <button
onclick={() => setTimeout(() => onDeleteClick(index.fileId), 100)}
class="flex-shrink-0 rounded-full p-1 active:bg-gray-100"
>
<IconDelete class="text-lg text-gray-600" /> <IconDelete class="text-lg text-gray-600" />
</button> --> </button>
</div> </div>

View File

@@ -1 +1,7 @@
import { deleteFileCache as doDeleteFileCache } from "$lib/modules/cache";
export { formatDate, formatFileSize } from "$lib/modules/util"; export { formatDate, formatFileSize } from "$lib/modules/util";
export const deleteFileCache = async (fileId: number) => {
await doDeleteFileCache(fileId);
};

View File

@@ -1,6 +1,7 @@
import ExifReader from "exifreader"; import ExifReader from "exifreader";
import { callGetApi, callPostApi } from "$lib/hooks"; import { callGetApi, callPostApi } from "$lib/hooks";
import { storeHmacSecrets } from "$lib/indexedDB"; import { storeHmacSecrets } from "$lib/indexedDB";
import { deleteFileCache } from "$lib/modules/cache";
import { import {
encodeToBase64, encodeToBase64,
generateDataKey, generateDataKey,
@@ -191,4 +192,5 @@ export const requestDirectoryEntryRename = async (
export const requestDirectoryEntryDeletion = async (entry: SelectedDirectoryEntry) => { export const requestDirectoryEntryDeletion = async (entry: SelectedDirectoryEntry) => {
await callPostApi(`/api/${entry.type}/${entry.id}/delete`); await callPostApi(`/api/${entry.type}/${entry.id}/delete`);
await deleteFileCache(entry.id);
}; };