Explorar o código

mobile: open dir && read/write files

Tienson Qin %!s(int64=4) %!d(string=hai) anos
pai
achega
328dfa841e

+ 9 - 5
src/main/frontend/fs.cljs

@@ -125,11 +125,15 @@
 
                  :else
                  nfs-record)]
-    (p/let [result (protocol/open-dir record ok-handler)]
-      (if (util/electron?)
-        (let [[dir & paths] (bean/->clj result)]
-          [(:path dir) paths])
-        result))))
+    (->
+     (p/let [result (protocol/open-dir record ok-handler)]
+       (if (or (util/electron?)
+               (mobile-util/is-native-platform?))
+         (let [[dir & paths] (bean/->clj result)]
+           [(:path dir) paths])
+         result))
+     (p/catch (fn [error]
+                (js/console.error error))))))
 
 (defn get-files
   [path-or-handle ok-handler]

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

@@ -84,22 +84,20 @@
                                 :directory (.-ExternalStorage Directory)
                                 :encoding (.-UTF8 Encoding)}))]
         content)))
-  (write-file! [this repo dir path content {:keys [ok-handler error-handler] :as opts}]
-    (let [path (str dir path)]
-      (p/catch
-          (p/let [result (.writeFile Filesystem
-                                     (clj->js
-                                      {:path path
-                                       :data content
-                                       :directory (.-ExternalStorage Directory)
-                                       :encoding (.-UTF8 Encoding)
-                                       :recursive true}))]
-            (when ok-handler
-              (ok-handler repo path result)))
-          (fn [error]
-            (if error-handler
-              (error-handler error)
-              (log/error :write-file-failed error))))))
+  (write-file! [this repo _dir path content {:keys [ok-handler error-handler] :as opts}]
+    (p/catch
+        (p/let [result (.writeFile Filesystem
+                                   (clj->js
+                                    {:path path
+                                     :data content
+                                     :encoding (.-UTF8 Encoding)
+                                     :recursive true}))]
+          (when ok-handler
+            (ok-handler repo path result)))
+        (fn [error]
+          (if error-handler
+            (error-handler error)
+            (log/error :write-file-failed error)))))
   (rename! [this repo old-path new-path]
     nil)
   (stat [this dir path]
@@ -112,7 +110,7 @@
             files (readdir path)]
       (js/console.log path)
       (js/console.log files)
-      (into [] (concat [path] files))))
+      (into [] (concat [{:path path}] files))))
   (get-files [this path-or-handle ok-handler]
     nil)
   (watch-dir! [this dir]

+ 25 - 8
src/main/frontend/handler/web/nfs.cljs

@@ -19,7 +19,8 @@
             [frontend.util :as util]
             [goog.object :as gobj]
             [lambdaisland.glogi :as log]
-            [promesa.core :as p]))
+            [promesa.core :as p]
+            [frontend.mobile.util :as mobile-util]))
 
 (defn remove-ignore-files
   [files]
@@ -42,9 +43,18 @@
       (p/resolved files))))
 
 (defn- ->db-files
-  [electron? dir-name result]
+  [mobile-native? electron? dir-name result]
   (->>
-   (if electron?
+   (cond
+     mobile-native?
+     (map (fn [{:keys [uri content type size mtime]}]
+            {:file/path             uri
+             :file/last-modified-at mtime
+             :file/size             size
+             :file/content content})
+       result)
+
+     electron?
      (map (fn [{:keys [path stat content]}]
             (let [{:keys [mtime size]} stat]
               {:file/path             path
@@ -52,6 +62,8 @@
                :file/size             size
                :file/content content}))
        result)
+
+     :else
      (let [result (flatten (bean/->clj result))]
        (map (fn [file]
               (let [handle (gobj/get file "handle")
@@ -107,7 +119,9 @@
   [ok-handler]
   (let [path-handles (atom {})
         electron? (util/electron?)
-        nfs? (not electron?)]
+        mobile-native? (mobile-util/is-native-platform?)
+        nfs? (and (not electron?)
+                  (not mobile-native?))]
     ;; TODO: add ext filter to avoid loading .git or other ignored file handlers
     (->
      (p/let [result (fs/open-dir (fn [path handle]
@@ -126,7 +140,7 @@
                  (idb/set-item! root-handle-path root-handle)
                  (nfs/add-nfs-file-handle! root-handle-path root-handle))
              result (nth result 1)
-             files (-> (->db-files electron? dir-name result)
+             files (-> (->db-files mobile-native? electron? dir-name result)
                        remove-ignore-files)
              _ (when nfs?
                  (let [file-paths (set (map :file/path files))]
@@ -170,7 +184,8 @@
      (p/catch (fn [error]
                 (if (contains? #{"AbortError" "Error"} (gobj/get error "name"))
                   (state/set-loading-files! false)
-                  (log/error :nfs/open-dir-error error)))))))
+                  ;; (log/error :nfs/open-dir-error error)
+                  (log/error :exception error)))))))
 
 (defn- compute-diffs
   [old-files new-files]
@@ -258,7 +273,9 @@
            handle-path (str config/local-handle-prefix dir-name)
            path-handles (atom {})
            electron? (util/electron?)
-           nfs? (not electron?)]
+           mobile-native? (mobile-util/is-native-platform?)
+           nfs? (and (not electron?)
+                     (not mobile-native?))]
        (when re-index?
          (state/set-graph-syncing? true))
        (->
@@ -270,7 +287,7 @@
                                                (fn [path handle]
                                                  (when nfs?
                                                    (swap! path-handles assoc path handle))))
-                    new-files (-> (->db-files electron? dir-name files-result)
+                    new-files (-> (->db-files mobile-native? electron? dir-name files-result)
                                   remove-ignore-files)
                     _ (when nfs?
                         (let [file-paths (set (map :file/path new-files))]

+ 0 - 5
yarn.lock

@@ -7852,11 +7852,6 @@ react-grid-layout@^0.16.6:
     react-draggable "3.x"
     react-resizable "1.x"
 
[email protected]:
-  version "2.1.0"
-  resolved "https://registry.yarnpkg.com/react-icon-base/-/react-icon-base-2.1.0.tgz#a196e33fdf1e7aaa1fda3aefbb68bdad9e82a79d"
-  integrity sha1-oZbjP98eeqof2jrvu2i9rZ6Cp50=
-
 react-icons@^2.2.7:
   version "2.2.7"
   resolved "https://registry.yarnpkg.com/react-icons/-/react-icons-2.2.7.tgz#d7860826b258557510dac10680abea5ca23cf650"