mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 06:58:46 +00:00
OPFS에 캐시된 썸네일을 모두 삭제하는 기능 추가
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { LRUCache } from "lru-cache";
|
||||
import { readFile, writeFile, deleteFile } from "$lib/modules/opfs";
|
||||
import { readFile, writeFile, deleteFile, deleteDirectory } from "$lib/modules/opfs";
|
||||
import { getThumbnailUrl } from "$lib/modules/thumbnail";
|
||||
|
||||
const loadedThumbnails = new LRUCache<number, string>({ max: 100 });
|
||||
@@ -27,3 +27,8 @@ export const deleteFileThumbnail = async (fileId: number) => {
|
||||
loadedThumbnails.delete(fileId);
|
||||
await deleteFile(`/thumbnails/${fileId}`);
|
||||
};
|
||||
|
||||
export const deleteAllFileThumbnails = async () => {
|
||||
loadedThumbnails.clear();
|
||||
await deleteDirectory("/thumbnails");
|
||||
};
|
||||
|
||||
@@ -59,3 +59,39 @@ export const deleteFile = async (path: string) => {
|
||||
|
||||
await parentHandle.removeEntry(filename);
|
||||
};
|
||||
|
||||
const getDirectoryHandle = async (path: string) => {
|
||||
if (!rootHandle) {
|
||||
throw new Error("OPFS not prepared");
|
||||
} else if (path[0] !== "/") {
|
||||
throw new Error("Path must be absolute");
|
||||
}
|
||||
|
||||
const parts = path.split("/");
|
||||
if (parts.length <= 1) {
|
||||
throw new Error("Invalid path");
|
||||
}
|
||||
|
||||
try {
|
||||
let directoryHandle = rootHandle;
|
||||
let parentHandle;
|
||||
for (const part of parts.slice(1)) {
|
||||
if (!part) continue;
|
||||
parentHandle = directoryHandle;
|
||||
directoryHandle = await directoryHandle.getDirectoryHandle(part);
|
||||
}
|
||||
return { directoryHandle, parentHandle };
|
||||
} catch (e) {
|
||||
if (e instanceof DOMException && e.name === "NotFoundError") {
|
||||
return {};
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteDirectory = async (path: string) => {
|
||||
const { directoryHandle, parentHandle } = await getDirectoryHandle(path);
|
||||
if (!parentHandle) return;
|
||||
|
||||
await parentHandle.removeEntry(directoryHandle.name, { recursive: true });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user