Correctly cache paths

This commit is contained in:
blank X 2021-08-23 00:18:32 +07:00
parent bf4bceea00
commit 5df63a7828
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 7 additions and 5 deletions

View File

@ -152,15 +152,17 @@ async function pathToId(path, drive) {
let paths = path.split("/");
let id = drive.folderId;
let mimeType = null;
let buildingPath = [];
for (let i=0; i < paths.length; i++) {
path = paths[i];
if (path === "") {
continue;
}
path = unescapeURL(path).replaceAll("\\", "\\\\").replaceAll("'", "\\'");
if (drive.useCache && drive.pathCache.exists(path)) {
id = drive.pathCache[path].id;
mimeType = drive.pathCache[path].mimeType;
buildingPath.push(path);
if (drive.useCache && drive.pathCache.exists(buildingPath.join("/"))) {
id = drive.pathCache[buildingPath.join("/")].id;
mimeType = drive.pathCache[buildingPath.join("/")].mimeType;
continue;
}
let query = `driveId=${drive.teamDriveId}&q=${escapeURL("name = '" + path + "' and '" + id + "' in parents")}&corpora=drive`;
@ -173,13 +175,13 @@ async function pathToId(path, drive) {
}
let url = `https://www.googleapis.com/drive/v3/files?${query}&includeItemsFromAllDrives=true&pageSize=1&supportsAllDrives=true&fields=files(id,mimeType)`;
let resp = await (await fetch(url, {"headers": {"Authorization": "Bearer " + saBearer}})).json();
if (resp.files.length < 1) {
if (typeof resp.files !== "object" || resp.files.length < 1) {
return null, null;
}
id = resp.files[0].id;
mimeType = resp.files[0].mimeType;
if (drive.useCache) {
drive.pathCache[path] = {"id": id, "mimeType": mimeType};
drive.pathCache[buildingPath.join("/")] = {"id": id, "mimeType": mimeType};
}
}
return {"id": id, "mimeType": mimeType};