From 08758fa0eace87fc718658c2d4b94f9f8ad2d0c0 Mon Sep 17 00:00:00 2001 From: charlie Date: Fri, 4 Nov 2022 16:35:47 +0800 Subject: [PATCH] enhance(mobile): normalize url protocol path for capacitor fs all operations --- src/main/frontend/fs/capacitor_fs.cljs | 29 +++++++++++---------- src/test/frontend/fs/capacitor_fs_test.cljs | 12 ++++----- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/main/frontend/fs/capacitor_fs.cljs b/src/main/frontend/fs/capacitor_fs.cljs index 00c0dbd85f..85b38d7e2b 100644 --- a/src/main/frontend/fs/capacitor_fs.cljs +++ b/src/main/frontend/fs/capacitor_fs.cljs @@ -172,7 +172,7 @@ (defn- write-file-impl! [_this repo _dir path content {:keys [ok-handler error-handler old-content skip-compare?]} stat] - (if skip-compare? + (if (or (string/blank? repo) skip-compare?) (p/catch (p/let [result ( dir (string/replace #"/+$" "")) dir (if (and (not-empty dir) (string/starts-with? dir "/")) (do @@ -288,12 +288,13 @@ (defrecord ^:large-vars/cleanup-todo Capacitorfs [] protocol/Fs (mkdir! [_this dir] - (-> (.mkdir Filesystem - (clj->js - {:path dir})) - (p/catch (fn [error] - (log/error :mkdir! {:path dir - :error error}))))) + (let [dir' (normalize-file-protocol-path "" dir)] + (-> (.mkdir Filesystem + (clj->js + {:path dir'})) + (p/catch (fn [error] + (log/error :mkdir! {:path dir' + :error error})))))) (mkdir-recur! [_this dir] (p/let [_ (-> (.mkdir Filesystem @@ -313,7 +314,7 @@ dir)] (readdir dir))) (unlink! [this repo path _opts] - (p/let [path (get-file-path nil path) + (p/let [path (normalize-file-protocol-path nil path) repo-url (config/get-local-dir repo) recycle-dir (util/safe-path-join repo-url config/app-name ".recycle") ;; logseq/.recycle ;; convert url to pure path @@ -327,20 +328,20 @@ ;; Too dangerous!!! We'll never implement this. nil) (read-file [_this dir path _options] - (let [path (get-file-path dir path)] + (let [path (normalize-file-protocol-path dir path)] (-> (js {:path path})) (fn [_e] :not-found))] ;; `path` is full-path (write-file-impl! this repo dir path content opts stat)))) (rename! [_this _repo old-path new-path] - (let [[old-path new-path] (map #(get-file-path "" %) [old-path new-path])] + (let [[old-path new-path] (map #(normalize-file-protocol-path "" %) [old-path new-path])] (p/catch (p/let [_ (.rename Filesystem (clj->js @@ -349,7 +350,7 @@ (fn [error] (log/error :rename-file-failed error))))) (copy! [_this _repo old-path new-path] - (let [[old-path new-path] (map #(get-file-path "" %) [old-path new-path])] + (let [[old-path new-path] (map #(normalize-file-protocol-path "" %) [old-path new-path])] (p/catch (p/let [_ (.copy Filesystem (clj->js @@ -358,7 +359,7 @@ (fn [error] (log/error :copy-file-failed error))))) (stat [_this dir path] - (let [path (get-file-path dir path)] + (let [path (normalize-file-protocol-path dir path)] (p/chain (.stat Filesystem (clj->js {:path path})) #(js->clj % :keywordize-keys true)))) (open-dir [_this dir _ok-handler] diff --git a/src/test/frontend/fs/capacitor_fs_test.cljs b/src/test/frontend/fs/capacitor_fs_test.cljs index 5e59e5ba54..fdd01d7142 100644 --- a/src/test/frontend/fs/capacitor_fs_test.cljs +++ b/src/test/frontend/fs/capacitor_fs_test.cljs @@ -8,38 +8,38 @@ (let [dir "file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~logseq~logseq/Documents/" url-decoded-dir "file:///private/var/mobile/Library/Mobile Documents/iCloud~com~logseq~logseq/Documents/"] (is (= (str url-decoded-dir "pages/pages-metadata.edn") - (capacitor-fs/get-file-path + (capacitor-fs/normalize-file-protocol-path dir "file:///private/var/mobile/Library/Mobile Documents/iCloud~com~logseq~logseq/Documents/pages/pages-metadata.edn")) "full path returns as url decoded full path") (is (= (str url-decoded-dir "journals/2002_01_28.md") - (capacitor-fs/get-file-path + (capacitor-fs/normalize-file-protocol-path dir "/journals/2002_01_28.md")) "relative path returns as url decoded full path") (is (= dir - (capacitor-fs/get-file-path + (capacitor-fs/normalize-file-protocol-path dir nil)) "nil path returns url encoded dir")) (let [dir "file:///storage/emulated/0/Graphs/Test"] (is (= (str dir "/pages/pages-metadata.edn") - (capacitor-fs/get-file-path + (capacitor-fs/normalize-file-protocol-path dir "file:///storage/emulated/0/Graphs/Test/pages/pages-metadata.edn")) "full path returns as url decoded full path") (is (= (str dir "/journals/2002_01_28.md") - (capacitor-fs/get-file-path + (capacitor-fs/normalize-file-protocol-path dir "/journals/2002_01_28.md")) "relative path returns as url decoded full path") (is (= dir - (capacitor-fs/get-file-path + (capacitor-fs/normalize-file-protocol-path dir nil)) "nil path returns url encoded dir"))))