Browse Source

fix(android): app crashes

leizhe 3 years ago
parent
commit
86ea102b89

+ 17 - 6
src/main/frontend/config.cljs

@@ -304,11 +304,15 @@
 (defn get-repo-dir
   [repo-url]
   (cond
-    (or
-     (mobile-util/is-native-platform?)
-     (and (util/electron?) (local-db? repo-url)))
+    (and (util/electron?) (local-db? repo-url))
     (get-local-dir repo-url)
 
+    (and (mobile-util/is-native-platform?) (local-db? repo-url))
+    (let [dir (get-local-dir repo-url)]
+      (if (string/starts-with? dir "file:")
+        dir
+        (str "file:///" (string/replace dir #"^/+" ""))))
+    
     :else
     (str "/"
          (->> (take-last 2 (string/split repo-url #"/"))
@@ -326,7 +330,7 @@
   [repo-url relative-path]
   (when (and repo-url relative-path)
     (let [path (cond
-                 (and (or (util/electron?) (mobile-util/native-android?)) (local-db? repo-url))
+                 (and (util/electron?) (local-db? repo-url))
                  (let [dir (get-repo-dir repo-url)]
                    (if (string/starts-with? relative-path dir)
                      relative-path
@@ -334,10 +338,17 @@
                           (string/replace relative-path #"^/" ""))))
 
                  (and (mobile-util/native-ios?) (local-db? repo-url))
-                 (let [dir (-> (get-repo-dir repo-url)
-                               (string/replace "file:///" "file:/"))]
+                 (let [dir (get-repo-dir repo-url)]
                    (js/decodeURI (str dir relative-path)))
 
+                 (and (mobile-util/native-android?) (local-db? repo-url))
+                 (let [dir (get-repo-dir repo-url)
+                       dir (if (or (string/starts-with? dir "file:")
+                                   (string/starts-with? dir "content:"))
+                             dir
+                             (str "file:///" (string/replace dir #"^/+" "")))]
+                   (str (string/replace dir #"/+$" "") "/" relative-path))
+
                  (= "/" (first relative-path))
                  (subs relative-path 1)
 

+ 16 - 23
src/main/frontend/fs/capacitor_fs.cljs

@@ -28,9 +28,7 @@
 (defn- clean-uri
   [uri]
   (when (string? uri)
-    (-> uri
-        (string/replace "file://" "")
-        (util/url-decode))))
+    (util/url-decode uri)))
 
 (defn readdir
   "readdir recursively"
@@ -53,11 +51,11 @@
                                                       (= file "bak")))))
                              files (->> files
                                         (map (fn [file]
-                                               (util/node-path.join
-                                                d
-                                                (if (mobile-util/native-ios?)
-                                                  (util/url-encode file)
-                                                  file)))))
+                                               (str (string/replace d #"/+$" "")
+                                                    "/"
+                                                    (if (mobile-util/native-ios?)
+                                                      (util/url-encode file)
+                                                      file)))))
                              files-with-stats (p/all
                                                (mapv
                                                 (fn [file]
@@ -159,19 +157,18 @@
                       (log/error :write-file-failed error)))))))))
 
 (defn get-file-path [dir path]
-  (let [[dir' path'] (map #(some-> %
-                                  (string/replace "///" "/")
-                                  js/decodeURI)
-                         [dir path])
+  (let [[dir path] (map #(some-> %
+                                 js/decodeURI)
+                        [dir path])
+        dir (string/replace dir #"/+$" "")
         path (cond (nil? path)
-               dir
+                   dir
 
-               (string/starts-with? path' dir')
-               path'
+                   (string/starts-with? path dir)
+                   path
 
-               :else
-               (-> (str dir' path)
-                   (string/replace "//" "/")))]
+                   :else
+                   (str dir path))]
     (if (mobile-util/native-ios?)
       (js/encodeURI (js/decodeURI path))
       path)))
@@ -202,11 +199,7 @@
     ;; Too dangerious!!! We'll never implement this.
     nil)
   (read-file [_this dir path _options]
-    (let [path (str dir path)
-          path (if (or (string/starts-with? path "file:")
-                       (string/starts-with? path "content:"))
-                 path
-                 (str "file:///" (string/replace path #"^/+" "")))]
+    (let [path (get-file-path dir path)]
       (->
        (p/let [content (.readFile Filesystem
                                   (clj->js

+ 2 - 2
src/main/frontend/handler/file.cljs

@@ -130,8 +130,8 @@
                 (str (config/get-repo-dir repo-url) "/" file)
 
                 (and (mobile/native-android?) (not= "/" (first file)))
-                (str (config/get-repo-dir repo-url) "/" file)
-
+                file
+                
                 (and (mobile/native-ios?) (not= "/" (first file)))
                 file
 

+ 1 - 1
src/main/frontend/handler/web/nfs.cljs

@@ -48,7 +48,7 @@
    (cond
      mobile-native?
      (map (fn [{:keys [uri content size mtime]}]
-            {:file/path             (util/path-normalize (string/replace uri "file://" ""))
+            {:file/path             (util/path-normalize uri)
              :file/last-modified-at mtime
              :file/size             size
              :file/content content})

+ 39 - 18
src/test/frontend/fs/capacitor_fs_test.cljs

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