fix: align cleanup actions with opfs storage

This commit is contained in:
Youzini-afk
2026-04-14 19:55:18 +08:00
parent 78db223833
commit 1867662653
6 changed files with 246 additions and 24 deletions

View File

@@ -340,6 +340,19 @@ async function maybeGetFileHandle(parentHandle, name) {
}
}
async function maybeGetDirectoryHandle(parentHandle, name) {
try {
return await parentHandle.getDirectoryHandle(String(name || ""), {
create: false,
});
} catch (error) {
if (isNotFoundError(error)) {
return null;
}
throw error;
}
}
async function readJsonFile(parentHandle, name, fallbackValue = null) {
const fileHandle = await maybeGetFileHandle(parentHandle, name);
if (!fileHandle) {
@@ -599,6 +612,116 @@ export async function detectOpfsSupport(options = {}) {
}
}
export async function deleteOpfsChatStorage(chatId, options = {}) {
const normalizedChatId = normalizeChatId(chatId);
if (!normalizedChatId) {
return {
deleted: false,
reason: "missing-chat-id",
chatId: "",
};
}
const rootDirectoryFactory =
typeof options.rootDirectoryFactory === "function"
? options.rootDirectoryFactory
: getDefaultOpfsRootDirectory;
try {
const rootDirectory = await rootDirectoryFactory();
if (!rootDirectory || typeof rootDirectory.getDirectoryHandle !== "function") {
return {
deleted: false,
reason: "missing-directory-handle",
chatId: normalizedChatId,
};
}
const opfsRoot = await maybeGetDirectoryHandle(
rootDirectory,
OPFS_ROOT_DIRECTORY_NAME,
);
if (!opfsRoot) {
return {
deleted: false,
reason: "not-found",
chatId: normalizedChatId,
};
}
const chatsDirectory = await maybeGetDirectoryHandle(
opfsRoot,
OPFS_CHATS_DIRECTORY_NAME,
);
if (!chatsDirectory) {
return {
deleted: false,
reason: "not-found",
chatId: normalizedChatId,
};
}
const chatDirectoryName = buildChatDirectoryName(normalizedChatId);
const chatDirectory = await maybeGetDirectoryHandle(chatsDirectory, chatDirectoryName);
if (!chatDirectory) {
return {
deleted: false,
reason: "not-found",
chatId: normalizedChatId,
};
}
await chatsDirectory.removeEntry(chatDirectoryName, {
recursive: true,
});
return {
deleted: true,
reason: "deleted",
chatId: normalizedChatId,
};
} catch (error) {
return {
deleted: false,
reason: "delete-failed",
chatId: normalizedChatId,
error,
};
}
}
export async function deleteAllOpfsStorage(options = {}) {
const rootDirectoryFactory =
typeof options.rootDirectoryFactory === "function"
? options.rootDirectoryFactory
: getDefaultOpfsRootDirectory;
try {
const rootDirectory = await rootDirectoryFactory();
if (!rootDirectory || typeof rootDirectory.getDirectoryHandle !== "function") {
return {
deleted: false,
reason: "missing-directory-handle",
};
}
const opfsRoot = await maybeGetDirectoryHandle(
rootDirectory,
OPFS_ROOT_DIRECTORY_NAME,
);
if (!opfsRoot) {
return {
deleted: false,
reason: "not-found",
};
}
await rootDirectory.removeEntry(OPFS_ROOT_DIRECTORY_NAME, {
recursive: true,
});
return {
deleted: true,
reason: "deleted",
};
} catch (error) {
return {
deleted: false,
reason: "delete-failed",
error,
};
}
}
class LegacyOpfsGraphStore {
constructor(chatId, options = {}) {
this.chatId = normalizeChatId(chatId);