캐시 삭제 구현

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

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