From 5df63a782822d469401c9cd38faf37744ddf4c57 Mon Sep 17 00:00:00 2001 From: blank X Date: Mon, 23 Aug 2021 00:18:32 +0700 Subject: [PATCH] Correctly cache paths --- index.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 04d36ae..e32d8c1 100644 --- a/index.js +++ b/index.js @@ -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};