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

refactor(fs): refine stat fn of Fs protocol

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

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

@@ -486,11 +486,10 @@
 (defn expand-relative-assets-path
   ;; ../assets/xxx -> {assets|file}://{current-graph-root-path}/xxx
   [source]
-  (js/console.error "BUG: assets:// url handling")
   (when-let [protocol (and (string? source)
                            (not (string/blank? source))
                            (if (util/electron?) "assets" "file"))]
-
+    (js/console.error "BUG: assets:// url handling")
     (string/replace
      source "../assets" (util/format "%s://%s/assets" protocol (get-repo-dir (state/get-current-repo))))))
 

+ 6 - 3
src/main/frontend/fs.cljs

@@ -146,8 +146,11 @@
       (protocol/copy! (get-fs old-path) repo old-path new-path))))
 
 (defn stat
-  [dir path]
-  (protocol/stat (get-fs dir) dir path))
+  ([fpath]
+   (protocol/stat (get-fs fpath) fpath))
+  ([dir path]
+   (let [fpath (fs2-path/path-join dir path)]
+     (protocol/stat (get-fs dir) fpath))))
 
 (defn- get-native-backend
   "Native FS backend of current platform"
@@ -213,7 +216,7 @@
   (->
    (when dir
      (util/p-handle
-      (stat dir nil)
+      (stat dir)
       (fn [_stat])
       (fn [_error]
         (mkdir! dir))))

+ 3 - 4
src/main/frontend/fs/capacitor_fs.cljs

@@ -379,10 +379,9 @@
                    :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}))
-               #(js->clj % :keywordize-keys true))))
+  (stat [_this fpath]
+    (p/chain (.stat Filesystem (clj->js {:path fpath}))
+             #(js->clj % :keywordize-keys true)))
   (open-dir [_this dir _ok-handler]
     (open-dir dir))
   (list-files [_this dir _ok-handler]

+ 3 - 8
src/main/frontend/fs/memory_fs.cljs

@@ -49,7 +49,6 @@
 (defrecord Bfs []
   protocol/Fs
   (mkdir! [_this dir]
-    (js/console.trace)
     (when js/window.pfs
       (let [fpath (fs2-path/url-to-path dir)]
         (-> (js/window.pfs.mkdir fpath)
@@ -87,13 +86,9 @@
     (let [old-path (fs2-path/url-to-path old-path)
           new-path (fs2-path/url-to-path new-path)]
       (js/window.pfs.rename old-path new-path)))
-  ;; FIXME(andelf): API sign for dir-only state
-  (stat [_this dir path]
-    (if path
-      (let [fpath (fs2-path/url-to-path (fs2-path/path-join dir path))]
-        (js/window.pfs.stat fpath))
-      (let [fpath (fs2-path/url-to-path dir)]
-        (js/window.pfs.stat fpath))))
+  (stat [_this fpath]
+    (let [fpath (fs2-path/url-to-path fpath)]
+      (js/window.pfs.stat fpath)))
   (open-dir [_this _dir _ok-handler]
     nil)
   (list-files [_this _path-or-handle _ok-handler]

+ 2 - 4
src/main/frontend/fs/nfs.cljs

@@ -214,10 +214,8 @@
             content (.text file)
             _ (protocol/write-file! this repo dir new-path content nil)]
       (protocol/unlink! this repo old-path nil)))
-  (stat [_this dir path]
-    (if-let [file (get-nfs-file-handle (str "handle/"
-                                            (string/replace-first dir "/" "")
-                                            path))]
+  (stat [_this fpath]
+    (if-let [file (get-nfs-file-handle (str "handle/" fpath))]
       (p/let [file (.getFile file)]
         (let [get-attr #(gobj/get file %)]
           {:file/last-modified-at (get-attr "lastModified")

+ 6 - 5
src/main/frontend/fs/node.cljs

@@ -119,17 +119,18 @@
     (let [path (fs2-path/path-join dir path)]
       (ipc/ipc "readFile" path)))
   (write-file! [this repo dir path content opts]
-    (p/let [stat (p/catch
-                  (protocol/stat this dir path)
+    (p/let [fpath (fs2-path/path-join dir path)
+            stat (p/catch
+                  (protocol/stat this fpath)
                   (fn [_e] :not-found))
             sub-dir (first (util/get-dir-and-basename path)) ;; FIXME: todo dirname
             _ (protocol/mkdir-recur! this sub-dir)]
       (write-file-impl! repo dir path content opts stat)))
   (rename! [_this _repo old-path new-path]
     (ipc/ipc "rename" old-path new-path))
-  (stat [_this dir path]
-    (let [path (fs2-path/path-join dir path)]
-      (ipc/ipc "stat" path)))
+  (stat [_this fpath]
+    (-> (ipc/ipc "stat" fpath)
+        (p/then bean/->clj)))
   (open-dir [_this dir _ok-handler]
     (p/then (open-dir dir)
             bean/->clj))

+ 2 - 1
src/main/frontend/fs/protocol.cljs

@@ -12,7 +12,8 @@
   (write-file! [this repo dir path content opts])
   (rename! [this repo old-path new-path])
   (copy! [this repo old-path new-path])
-  (stat [this dir path])
+  (stat [this path]
+    "=> {:type string :size number :mtime number}")
   (open-dir [this dir ok-handler]
     "=> {:path string :files [{...}]}")
   (list-files [this dir ok-handler]

+ 6 - 6
src/test/frontend/fs/test_node.cljs

@@ -6,12 +6,12 @@
 
 ;; Most protocol fns are not defined. Define them as needed for tests
 (defrecord NodeTestfs
-  []
+           []
   protocol/Fs
   (read-file [_this _dir path _options]
-             (p/let [content (fsp/readFile path)]
-                    (str content)))
+    (p/let [content (fsp/readFile path)]
+      (str content)))
   (write-file! [_this _repo _dir path content _opts]
-               (fsp/writeFile path content))
-  (stat [_this _dir path]
-        (fsp/stat path)))
+    (fsp/writeFile path content))
+  (stat [_this fpath]
+    (fsp/stat fpath)))