Sfoglia il codice sorgente

enhance(mobile): normalize url protocol path for capacitor fs all operations

charlie 3 anni fa
parent
commit
a43686d624

+ 15 - 14
src/main/frontend/fs/capacitor_fs.cljs

@@ -168,7 +168,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 (<write-file-with-utf8 path content)]
        (when ok-handler
@@ -212,7 +212,7 @@
                       (error-handler error)
                       (log/error :write-file-failed error)))))))))
 
-(defn get-file-path [dir path]
+(defn normalize-file-protocol-path [dir path]
   (let [dir        (some-> dir (string/replace #"/+$" ""))
         dir        (if (and (not-empty dir) (string/starts-with? dir "/"))
                      (do
@@ -282,12 +282,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
@@ -307,7 +308,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
@@ -321,20 +322,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)]
       (->
        (<read-file-with-utf8 path)
        (p/catch (fn [error]
                   (log/error :read-file-failed error))))))
   (write-file! [this repo dir path content opts]
-    (let [path (get-file-path dir path)]
+    (let [path (normalize-file-protocol-path dir path)]
       (p/let [stat (p/catch
                     (.stat Filesystem (clj->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
@@ -343,7 +344,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
@@ -352,7 +353,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]

+ 6 - 6
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"))))