From dcd02457cb1ab910bbf2f9d4a5852a42dc820fb6 Mon Sep 17 00:00:00 2001 From: fyears <1142836+fyears@users.noreply.github.com> Date: Sat, 30 Mar 2024 14:52:54 +0800 Subject: [PATCH] fix folder segments of dropbox --- package.json | 6 ++--- src/misc.ts | 49 +++++++++++++++++++++++++++++++++++++ src/remoteForDropbox.ts | 3 +++ tests/misc.test.ts | 53 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index aa4a566..4e3a211 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "license": "Apache-2.0", "devDependencies": { "@microsoft/microsoft-graph-types": "^2.40.0", - "@types/chai": "^4.3.11", + "@types/chai": "^4.3.14", "@types/chai-as-promised": "^7.1.8", "@types/jsdom": "^21.1.6", "@types/lodash": "^4.14.202", @@ -34,14 +34,14 @@ "@types/node": "^20.10.4", "@types/qrcode": "^1.5.5", "builtin-modules": "^3.3.0", - "chai": "^4.3.10", + "chai": "^4.4.1", "chai-as-promised": "^7.1.1", "cross-env": "^7.0.3", "dotenv": "^16.3.1", "esbuild": "^0.19.9", "esbuild-plugin-inline-worker": "^0.1.1", "jsdom": "^23.0.1", - "mocha": "^10.2.0", + "mocha": "^10.4.0", "npm-check-updates": "^16.14.12", "obsidian": "^1.4.11", "prettier": "^3.1.1", diff --git a/src/misc.ts b/src/misc.ts index 7a169b4..2dab583 100644 --- a/src/misc.ts +++ b/src/misc.ts @@ -534,3 +534,52 @@ export const changeMobileStatusBar = (op: "enable" | "disable") => { bar.style.removeProperty("margin-bottom"); } }; + +/** + * https://github.com/remotely-save/remotely-save/issues/567 + * https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Case-Sensitivity-in-API-2/td-p/191279 + * @param entities + */ +export const fixEntityListCasesInplace = (entities: { keyRaw: string }[]) => { + entities.sort((a, b) => a.keyRaw.length - b.keyRaw.length); + // console.log(JSON.stringify(entities,null,2)); + + const caseMapping: Record = { "": "" }; + for (const e of entities) { + // console.log(`looking for: ${JSON.stringify(e, null, 2)}`); + + let parentFolder = getParentFolder(e.keyRaw); + if (parentFolder === "/") { + parentFolder = ""; + } + const parentFolderLower = parentFolder.toLocaleLowerCase(); + const segs = e.keyRaw.split("/"); + if (e.keyRaw.endsWith("/")) { + // folder + if (caseMapping.hasOwnProperty(parentFolderLower)) { + const newKeyRaw = `${caseMapping[parentFolderLower]}${segs + .slice(-2) + .join("/")}`; + caseMapping[newKeyRaw.toLocaleLowerCase()] = newKeyRaw; + e.keyRaw = newKeyRaw; + // console.log(JSON.stringify(caseMapping,null,2)); + continue; + } else { + throw Error(`${parentFolder} doesn't have cases record??`); + } + } else { + // file + if (caseMapping.hasOwnProperty(parentFolderLower)) { + const newKeyRaw = `${caseMapping[parentFolderLower]}${segs + .slice(-1) + .join("/")}`; + e.keyRaw = newKeyRaw; + continue; + } else { + throw Error(`${parentFolder} doesn't have cases record??`); + } + } + } + + return entities; +}; diff --git a/src/remoteForDropbox.ts b/src/remoteForDropbox.ts index 112f2c5..c21e147 100644 --- a/src/remoteForDropbox.ts +++ b/src/remoteForDropbox.ts @@ -12,6 +12,7 @@ import { } from "./baseTypes"; import { bufferToArrayBuffer, + fixEntityListCasesInplace, getFolderLevels, hasEmojiInText, headersToRecord, @@ -635,6 +636,8 @@ export const listAllFromRemote = async (client: WrappedDropboxClient) => { unifiedContents.push(...unifiedContents2); } + fixEntityListCasesInplace(unifiedContents); + return unifiedContents; }; diff --git a/tests/misc.test.ts b/tests/misc.test.ts index 606ea31..326c8be 100644 --- a/tests/misc.test.ts +++ b/tests/misc.test.ts @@ -285,3 +285,56 @@ describe("Misc: special char for dir", () => { expect(misc.checkHasSpecialCharForDir("xxx?yyy")).to.be.true; }); }); + +describe("Misc: Dropbox: should fix the folder name cases", () => { + it("should do nothing on empty folders", () => { + const input: any[] = []; + expect(misc.fixEntityListCasesInplace(input)).to.be.empty; + }); + + it("should sort folders by length by side effect", () => { + const input = [ + { keyRaw: "aaaa/" }, + { keyRaw: "bbb/" }, + { keyRaw: "c/" }, + { keyRaw: "dd/" }, + ]; + + const output = [ + { keyRaw: "c/" }, + { keyRaw: "dd/" }, + { keyRaw: "bbb/" }, + { keyRaw: "aaaa/" }, + ]; + expect(misc.fixEntityListCasesInplace(input)).to.deep.equal(output); + }); + + it("should fix folder names", () => { + const input = [ + { keyRaw: "AAA/" }, + { keyRaw: "aaa/bbb/CCC.md" }, + { keyRaw: "aaa/BBB/" }, + + { keyRaw: "ddd/" }, + { keyRaw: "DDD/EEE/fff.md" }, + { keyRaw: "DDD/eee/" }, + + { keyRaw: "Ggg/" }, + { keyRaw: "ggG/hHH你好/Fff世界.md" }, + { keyRaw: "ggG/Hhh你好/" }, + ]; + + const output = [ + { keyRaw: "AAA/" }, + { keyRaw: "ddd/" }, + { keyRaw: "Ggg/" }, + { keyRaw: "AAA/BBB/" }, + { keyRaw: "ddd/eee/" }, + { keyRaw: "Ggg/Hhh你好/" }, + { keyRaw: "AAA/BBB/CCC.md" }, + { keyRaw: "ddd/eee/fff.md" }, + { keyRaw: "Ggg/Hhh你好/Fff世界.md" }, + ]; + expect(misc.fixEntityListCasesInplace(input)).to.deep.equal(output); + }); +});