瀏覽代碼

fix(mobile): unlink did not create parent dir correctly

Andelf 3 年之前
父節點
當前提交
f71a80341e
共有 1 個文件被更改,包括 18 次插入17 次删除
  1. 18 17
      src/main/frontend/fs/capacitor_fs.cljs

+ 18 - 17
src/main/frontend/fs/capacitor_fs.cljs

@@ -82,7 +82,7 @@
                      (p/let [d (first dirs)
                              files (<readdir d)
                              files (->> files
-                                        (remove (fn [{:keys [name  type] :as file-info}]
+                                        (remove (fn [{:keys [name  type]}]
                                                   (or (string/starts-with? name ".")
                                                       (and (= type "directory")
                                                            (or (= name "bak")
@@ -211,7 +211,7 @@
 
 (defn get-file-path [dir path]
   (let [dir (some-> dir (string/replace #"/+$" ""))
-        dir (if (string/starts-with? dir "/")
+        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)))
@@ -261,13 +261,18 @@
                    (log/error :mkdir! {:path dir
                                        :error error})))))
   (mkdir-recur! [_this dir]
-    (p/let [result (.mkdir Filesystem
-                           (clj->js
-                            {:path dir
-                             ;; :directory (.-ExternalStorage Directory)
-                             :recursive true}))]
-      (js/console.log result)
-      result))
+    (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")))))
   (readdir [_this dir]                  ; recursive
     (let [dir (if-not (string/starts-with? dir "file://")
                 (str "file://" dir)
@@ -276,13 +281,13 @@
   (unlink! [this repo path _opts]
     (p/let [path (get-file-path nil path)
             repo-url (config/get-local-dir repo)
-            recycle-dir (str repo-url config/app-name "/.recycle") ;; logseq/.recycle
+            recycle-dir (util/safe-path-join repo-url config/app-name ".recycle") ;; logseq/.recycle
             ;; convert url to pure path
             file-name (-> (string/replace path repo-url "")
                           (string/replace "/" "_")
                           (string/replace "\\" "_"))
-            new-path (str recycle-dir "/" file-name)]
-      (protocol/mkdir! this recycle-dir)
+            new-path (str recycle-dir "/" file-name)
+            _ (protocol/mkdir-recur! this recycle-dir)]
       (protocol/rename! this repo path new-path)))
   (rmdir! [_this _dir]
     ;; Too dangerous!!! We'll never implement this.
@@ -320,11 +325,7 @@
          (log/error :copy-file-failed error)))))
   (stat [_this dir path]
     (let [path (get-file-path dir path)]
-      (p/let [result (.stat Filesystem (clj->js
-                                        {:path path
-                                         ;; :directory (.-ExternalStorage Directory)
-                                         }))]
-        result)))
+      (<stat path)))
   (open-dir [_this _ok-handler]
     (p/let [_ (when (mobile-util/native-android?) (android-check-permission))
             {:keys [path localDocumentsPath]} (-> (.pickFolder mobile-util/folder-picker)