Просмотр исходного кода

refactor(fs): unused mobile fs fn

Andelf 2 лет назад
Родитель
Сommit
8ed5b983b5

+ 1 - 1
src/main/frontend/config.cljs

@@ -404,10 +404,10 @@
     (fs2-path/path-join (get-repo-dir repo-url) path)
     (util/node-path.join (get-repo-dir repo-url) path)))
 
-;; FIXME: There is another normalize-file-protocol-path at src/main/frontend/fs/capacitor_fs.cljs
 (defn get-file-path
   "Normalization happens here"
   [repo-url relative-path]
+  (js/console.error "::SHOULD-NOT-USE-BUGGY-FN" repo-url relative-path)
   (when (and repo-url relative-path)
     (let [path (cond
                  (demo-graph?)

+ 42 - 71
src/main/frontend/fs/capacitor_fs.cljs

@@ -44,6 +44,16 @@
            (fn [_error]
              false)))
 
+(defn- <dir-exists?
+  [fpath]
+  (p/catch (p/let [fpath (fs2-path/path-normalize fpath)
+                   stat (.stat Filesystem (clj->js {:path fpath}))]
+             (-> stat
+                 bean/->clj
+                 :type
+                 (= "directory")))
+           (fn [_error]
+             false)))
 
 (defn- <write-file-with-utf8
   [path content]
@@ -251,46 +261,6 @@
       path)
     path))
 
-(defn normalize-file-protocol-path [dir path]
-  (prn ::fuck-DO-NOT-CALL-THIS)
-  (js/console.trace)
-  (let [dir             (some-> dir (string/replace #"/+$" ""))
-        dir             (if (and (not-empty dir) (string/starts-with? dir "/"))
-                          (do
-                            (js/console.trace "WARN: detect absolute path, use URL instead")
-                            (str "file://" (js/encodeURI dir)))
-                          dir)
-        path            (some-> path (string/replace #"^/+" ""))
-        normalize-f     gp-util/path-normalize
-        encodeURI-f     js/encodeURI
-        safe-encode-url #(let [encoded-chars?
-                               (and (string? %) (boolean (re-find #"(?i)%[0-9a-f]{2}" %)))]
-                           (cond
-                             (not encoded-chars?)
-                             (encodeURI-f (normalize-f %))
-
-                             :else
-                             (encodeURI-f (normalize-f (js/decodeURI %)))))
-        path' (cond
-                (and path (string/starts-with? path "file:/"))
-                (safe-encode-url path)
-
-                (string/blank? path)
-                (safe-encode-url dir)
-
-                (string/blank? dir)
-                (safe-encode-url path)
-
-                (string/starts-with? path dir)
-                (safe-encode-url path)
-
-                :else
-                (let [path' (safe-encode-url path)]
-                  (str dir "/" path')))]
-    (if (mobile-util/native-ios?)
-      (ios-force-include-private path')
-      path')))
-
 (defn- local-container-path?
   "Check whether `path' is logseq's container `localDocumentsPath' on iOS"
   [path localDocumentsPath]
@@ -327,38 +297,40 @@
                        (not (or (local-container-path? path localDocumentsPath)
                                 (mobile-util/iCloud-container-path? path))))
               (state/pub-event! [:modal/show-instruction]))
+          path (if (mobile-util/native-ios?)
+                  (ios-force-include-private path)
+                  path)
           _ (js/console.log "Opening or Creating graph at directory: " path)
-          files (readdir path)
-          files (js->clj files :keywordize-keys true)]
+          files (readdir path)]
     (into [] (concat [{:path path}] files))))
 
 (defrecord ^:large-vars/cleanup-todo Capacitorfs []
   protocol/Fs
   (mkdir! [_this dir]
-    (let [dir' (fs2-path/path-normalize dir)]
-      (-> (.mkdir Filesystem
-                  (clj->js
-                   {:path dir'}))
-          (p/catch (fn [error]
-                     (log/error :mkdir! {:path  dir'
-                                         :error error}))))))
+    (-> (<dir-exists? dir)
+        (p/then (fn [exists?]
+                  (if exists?
+                    (p/resolved true)
+                    (.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
-                    (clj->js
-                     {:path dir
-                      :recursive true}))
-            (p/catch (fn [error]
-                       (log/error :mkdir-recur! {:path dir
-                                                 :error error}))))
-      stat (<stat dir)]
-      (if (= (:type stat) "directory")
-        (p/resolved true)
-        (p/rejected (js/Error. "mkdir-recur! failed")))))
+    (-> (<dir-exists? dir)
+        (p/then (fn [exists?]
+                  (if exists?
+                    (p/resolved true)
+                    (.mkdir Filesystem
+                            (clj->js
+                             {:path dir
+                              :recursive true})))))
+        (p/catch (fn [error]
+                   (log/error :mkdir-recur! {:path dir
+                                             :error error})))))
   (readdir [_this dir]                  ; recursive
-    (let [dir (if-not (string/starts-with? dir "file://")
-                (str "file://" dir)
-                dir)]
+    (let [dir (fs2-path/path-normalize dir)]
       (readdir dir)))
   (unlink! [this repo fpath _opts]
     (p/let [_ (prn ::unlink fpath)
@@ -401,13 +373,12 @@
   (copy! [_this _repo old-path new-path]
     (let []
       (prn ::copy old-path new-path)
-      (p/catch
-       (p/let [_ (.copy Filesystem
-                        (clj->js
-                         {:from old-path
-                          :to new-path}))])
-       (fn [error]
-         (log/error :copy-file-failed error)))))
+      (-> (.copy Filesystem
+                 (clj->js
+                  {:from old-path
+                   :to new-path}))
+          (p/catch (fn [error]
+                     (log/error :copy-file-failed error))))))
   (stat [_this dir path]
     (let [path (fs2-path/path-join dir path)]
       (p/chain (.stat Filesystem (clj->js {:path path}))

+ 0 - 45
src/test/frontend/fs/capacitor_fs_test.cljs

@@ -1,45 +0,0 @@
-(ns frontend.fs.capacitor-fs-test
-  (:require [frontend.fs.capacitor-fs :as capacitor-fs]
-            [clojure.test :refer [deftest is]]
-            [frontend.mobile.util :as mobile-util]))
-
-(deftest get-file-path
-  (if (mobile-util/native-ios?)
-    (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/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/normalize-file-protocol-path
-              dir
-              "/journals/2002_01_28.md"))
-          "relative path returns as url decoded full path")
-
-      (is (= dir
-             (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/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/normalize-file-protocol-path
-              dir
-              "/journals/2002_01_28.md"))
-          "relative path returns as url decoded full path")
-
-      (is (= dir
-             (capacitor-fs/normalize-file-protocol-path
-              dir
-              nil))
-          "nil path returns url encoded dir"))))