瀏覽代碼

refactor(fs): rework on electron fs

Andelf 2 年之前
父節點
當前提交
d666a6d35a

+ 16 - 14
src/electron/electron/handler.cljs

@@ -157,19 +157,19 @@
       keyword))
 
 (defn- get-files
+  "Returns vec of file-objs"
   [path]
-  (let [result (->>
-                (readdir path)
-                (remove (partial utils/ignored-path? path))
-                (filter #(contains? allowed-formats (get-ext %)))
-                (map (fn [path]
-                       (let [stat (fs/statSync path)]
-                         (when-not (.isDirectory stat)
-                           {:path    (utils/fix-win-path! path)
-                            :content (utils/read-file path)
-                            :stat    stat}))))
-                (remove nil?))]
-    (vec (cons {:path (utils/fix-win-path! path)} result))))
+  (->> (readdir path)
+       (remove (partial utils/ignored-path? path))
+       (filter #(contains? allowed-formats (get-ext %)))
+       (map (fn [path]
+              (let [stat (fs/statSync path)]
+                (when-not (.isDirectory stat)
+                  {:path    (utils/fix-win-path! path)
+                   :content (utils/read-file path)
+                   :stat    stat}))))
+       (remove nil?)
+       vec))
 
 (defn open-dir-dialog []
   (p/let [result (.showOpenDialog dialog (bean/->js
@@ -193,11 +193,13 @@
 
 (defmethod handle :openDir [^js window _messages]
   (logger/info ::open-dir "open folder selection dialog")
-  (p/let [path (open-dir-dialog)]
+  (p/let [path (open-dir-dialog)
+          path (utils/fix-win-path! path)]
     (logger/debug ::open-dir {:path path})
     (if path
       (try
-        (p/resolved (bean/->js (get-files path)))
+        (p/resolved (bean/->js {:path path
+                                :files (get-files path)}))
         (catch js/Error e 
           (do
             (utils/send-to-renderer window "notification" {:type "error"

+ 1 - 1
src/electron/electron/utils.cljs

@@ -37,7 +37,7 @@
 
 (defn fix-win-path!
   [path]
-  (when path
+  (when (not-empty path)
     (if win32?
       (string/replace path "\\" "/")
       path)))

+ 4 - 3
src/main/frontend/components/block.cljs

@@ -155,6 +155,7 @@
 
 (defn- get-file-absolute-path
   [config path]
+  (js/console.error "TODO: buggy path fn")
   (let [path (string/replace path "file:" "")
         block-id (:block/uuid config)
         current-file (and block-id
@@ -275,7 +276,7 @@
   {:will-unmount (fn [state]
                    (reset! *resizing-image? false)
                    state)}
-  [state config title src metadata full_text local?]
+  [state config title src metadata full-text local?]
   (let [size (get state ::size)]
     (ui/resize-provider
      (ui/resize-consumer
@@ -292,7 +293,7 @@
                        (when (and @size @*resizing-image?)
                          (when-let [block-id (:block/uuid config)]
                            (let [size (bean/->clj @size)]
-                             (editor-handler/resize-image! block-id metadata full_text size))))
+                             (editor-handler/resize-image! block-id metadata full-text size))))
                        (when @*resizing-image?
                             ;; TODO: need a better way to prevent the clicking to edit current block
                          (js/setTimeout #(reset! *resizing-image? false) 200)))
@@ -345,7 +346,7 @@
                                                          :repo        (state/get-current-repo)
                                                          :href        src
                                                          :title       title
-                                                         :full-text   full_text}))})]
+                                                         :full-text   full-text}))})]
                      (util/stop e)
                      (state/set-modal! confirm-fn))))}
               (ui/icon "trash")])

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

@@ -76,6 +76,7 @@
     (when (= fs bfs-record)
       (protocol/rmdir! fs dir))))
 
+;; TODO(andelf): distingush from graph file writing and global file write
 (defn write-file!
   [repo dir path content opts]
   (when content
@@ -182,7 +183,7 @@
     (when ok-handler
       (js/console.warn "ok-handler not nil"))
     (p/let [result (protocol/list-files fs-record path-or-handle ok-handler)]
-      (prn :t result)
+      (prn ::list-files (first result) "....")
       (if (or (util/electron?)
               (mobile-util/native-platform?))
         (let [files result ;; TODO(andelf): rm first item from electron
@@ -240,7 +241,7 @@
   (p/let [repo-dir (config/get-repo-dir (state/get-current-repo))
           rpath (fs2-path/relative-path repo-dir href)
           exist? (file-exists? repo-dir rpath)]
-    (prn ::href href exist?)
+    (prn ::href-exists href exist?)
     exist?))
 
 (defn dir-exists?

+ 9 - 8
src/main/frontend/fs/node.cljs

@@ -45,12 +45,12 @@
   )
 
 (defn- write-file-impl!
-  [this repo dir rpath content {:keys [ok-handler error-handler old-content skip-compare?]} stat]
+  [repo dir rpath content {:keys [ok-handler error-handler old-content skip-compare?]} stat]
   (prn ::write-file-impl repo dir rpath)
-  (let [file-path (fs2-path/path-join dir rpath)]
+  (let [file-fpath (fs2-path/path-join dir rpath)]
     (if skip-compare?
       (p/catch
-       (p/let [result (ipc/ipc "writeFile" repo file-path content)]
+       (p/let [result (ipc/ipc "writeFile" repo file-fpath content)]
          (when ok-handler
            (prn ::fuck :why-are-you-using-ok-handler)
            (ok-handler repo rpath result)))
@@ -60,7 +60,8 @@
            (log/error :write-file-failed error))))
 
       (p/let [disk-content (when (not= stat :not-found)
-                             (-> (protocol/read-file this dir file-path nil)
+                             (-> (ipc/ipc "readFile" file-fpath)
+                                 (p/then bean/->clj)
                                  (p/catch (fn [error]
                                             (js/console.error error)
                                             nil))))
@@ -81,7 +82,7 @@
 
           :else
           (->
-           (p/let [result (ipc/ipc "writeFile" repo file-path content)
+           (p/let [result (ipc/ipc "writeFile" repo file-fpath content)
                    mtime (gobj/get result "mtime")]
              (when-not contents-matched?
                (ipc/ipc "backupDbFile" (config/get-local-dir repo) rpath disk-content content))
@@ -139,7 +140,7 @@
                   (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! this repo dir path content opts stat)))
+      (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]
@@ -149,8 +150,8 @@
     (p/then (open-dir dir)
             bean/->clj))
   (list-files [_this dir _ok-handler]
-    (p/then (ipc/ipc "getFiles" dir)
-            bean/->clj))
+    (-> (ipc/ipc "getFiles" dir)
+        (p/then bean/->clj)))
   (watch-dir! [_this dir options]
     (ipc/ipc "addDirWatcher" dir options))
   (unwatch-dir! [_this dir]

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

@@ -14,7 +14,8 @@
   (copy! [this repo old-path new-path])
   (stat [this dir path])
   (open-dir [this dir ok-handler])
-  (list-files [this dir ok-handler])
+  (list-files [this dir ok-handler]
+    "dir => [{:path string :content string}]")
   (watch-dir! [this dir options])
   (unwatch-dir! [this dir])
   ;; Ensure the dir is watched, window agnostic.

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

@@ -2705,7 +2705,7 @@
   Object
   (schedule [this next-state args reason]
     {:pre [(s/valid? ::state next-state)]}
-    (println "[SyncManager" graph-uuid "]"
+    (println (str "[SyncManager " graph-uuid "]")
              (and state (name state)) "->" (and next-state (name next-state)) :reason reason :local-txid @*txid :now (tc/to-string (t/now)))
     (set! state next-state)
     (swap! *sync-state sync-state--update-state next-state)
@@ -3095,7 +3095,7 @@
 (defn <sync-stop []
   (go
     (when-let [sm ^SyncManager (state/get-file-sync-manager (state/get-current-file-sync-graph-uuid))]
-      (println "[SyncManager" (:graph-uuid sm) "]" "stopping")
+      (println (str "[SyncManager " (:graph-uuid sm) "]") "stopping")
 
       (state/clear-file-sync-state! (:graph-uuid sm))
 
@@ -3103,7 +3103,7 @@
 
       (reset! *sync-entered? false)
 
-      (println "[SyncManager" (:graph-uuid sm) "]" "stopped"))
+      (println (str "[SyncManager " (:graph-uuid sm) "]") "stopped"))
 
     (reset! current-sm-graph-uuid nil)))
 

+ 8 - 0
src/main/frontend/fs2/path.cljs

@@ -10,6 +10,7 @@
   (and (string? s)
        (or (string/starts-with? s "file://") ;; mobile platform
            (string/starts-with? s "content://") ;; android only
+           (string/starts-with? s "assets://") ;; FIXME: Electron asset, not urlencoded
            (string/starts-with? s "logseq://") ;; reserved for future fs protocl
            (string/starts-with? s "s3://"))))
 
@@ -199,6 +200,13 @@
         (js/console.error "unhandled trim-base" base-path sub-path)
         sub-path))))
 
+(defn url-to-path
+  "Extract path part of a URL. decoded"
+  [original-url]
+  (let [^js url (js/URL. original-url)
+        path (gp-util/safe-decode-uri-component (.-pathname url))]
+    path))
+
 
 (defn relative-path
   "Get relative path from base path.

+ 3 - 2
src/main/frontend/handler/editor.cljs

@@ -1498,11 +1498,12 @@
       (when-let [href (if (util/electron?) href
                           (second (re-find #"\((.+)\)$" full-text)))]
         (let [block-file-rpath (model/get-block-file-path block)
-              asset-fpath (if (string/starts-with? href "asset://")
-                            (throw (js/Error. "unimpl"))
+              asset-fpath (if (string/starts-with? href "assets://")
+                            (fs2-path/url-to-path href)
                             (config/get-repo-fpath
                              repo
                              (fs2-path/resolve-relative-path block-file-rpath href)))]
+          (prn ::deleting href asset-fpath)
           (fs/unlink! repo asset-fpath nil))))))
 
 ;; assets/journals_2021_02_03_1612350230540_0.png

+ 11 - 7
src/main/frontend/handler/web/nfs.cljs

@@ -312,13 +312,17 @@
                     (when (or (and (seq diffs) (seq modified-files))
                               (seq diffs))
                       (comment "re-index a local graph is handled here")
-                      (async/go
-                        (let [_finished? (async/<! (repo-handler/load-repo-to-db! repo
-                                                                                  {:diffs     diffs
-                                                                                   :nfs-files modified-files
-                                                                                   :refresh? (not re-index?)
-                                                                                   :new-graph? re-index?}))]
-                          (ok-handler))))
+                      (-> (repo-handler/load-repo-to-db! repo
+                                                         {:diffs     diffs
+                                                          :nfs-files modified-files
+                                                          :refresh? (not re-index?)
+                                                          :new-graph? re-index?})
+                          (p/then (fn [state]
+                                    (prn :load-repo-to-db! state)
+                                    (ok-handler)))
+                          (p/catch (fn [error]
+                                     (js/console.error "load-repo-to-db" error)))))
+
                     (when (and (util/electron?) (not re-index?))
                       (db/transact! repo new-files))))))))