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

fix: backup file and notify existing one

Tienson Qin 8 месяцев назад
Родитель
Сommit
a2936ba5ef

+ 12 - 0
src/main/frontend/handler/file_based/events.cljs

@@ -367,3 +367,15 @@
             nil
             template
             {:target page})))))))
+
+(defmethod events/handle :graph/backup-file [[_ repo file-path db-content]]
+  (p/let [disk-content (fs/read-file "" file-path)]
+    (fs/backup-db-file! repo file-path db-content disk-content)))
+
+(defmethod events/handle :graph/notify-existing-file [[_ data]]
+  (let [{:keys [current-file-path file-path]} data
+        error (t :file/validate-existing-file-error current-file-path file-path)]
+    (state/pub-event! [:notification/show
+                       {:content error
+                        :status :error
+                        :clear? false}])))

+ 6 - 0
src/main/frontend/handler/worker.cljs

@@ -49,6 +49,12 @@
 (defmethod handle :capture-error [_ _worker data]
   (state/pub-event! [:capture-error data]))
 
+(defmethod handle :backup-file [_ _worker data]
+  (state/pub-event! [:graph/backup-file data]))
+
+(defmethod handle :notify-existing-file  [_ _worker data]
+  (state/pub-event! [:graph/notify-existing-file data]))
+
 (defmethod handle :default [_ _worker data]
   (prn :debug "Worker data not handled: " data))
 

+ 14 - 17
src/main/frontend/worker/file/reset.cljs

@@ -3,6 +3,7 @@
   (:require [clojure.string :as string]
             [datascript.core :as d]
             [frontend.worker.state :as worker-state]
+            [frontend.worker.util :as worker-util]
             [logseq.common.config :as common-config]
             [logseq.common.util :as common-util]
             [logseq.db :as ldb]
@@ -23,33 +24,29 @@
      bugs on some OS
      e.g. on macOS, it doesn't fire the file change event when renaming between
        caps and non-caps"
-  [conn file-page file-path]
-  (when-let [current-file (page-exists-in-another-file @conn file-page file-path)]
-    (when (not= file-path current-file)
+  [repo conn file-page file-path]
+  (when-let [current-file-path (page-exists-in-another-file @conn file-page file-path)]
+    (when (not= file-path current-file-path)
       (cond
         ;; TODO: handle case sensitive file system
-        (= (common-util/path-normalize (string/lower-case current-file))
+        (= (common-util/path-normalize (string/lower-case current-file-path))
            (common-util/path-normalize (string/lower-case file-path)))
         ;; case renamed
-        (when-let [file (d/entity @conn [:file/path current-file])]
-          ;; (p/let [disk-content (fs/read-file "" current-file)]
-            ;;   (fs/backup-db-file! repo current-file (:file/content file) disk-content))
+        (when-let [file (d/entity @conn [:file/path current-file-path])]
+          (worker-util/post-message :backup-file
+                                    [repo current-file-path (:file/content file)])
           (ldb/transact! conn [{:db/id (:db/id file)
                                 :file/path file-path}]))
 
         :else
-        nil
-          ;; (let [error (t :file/validate-existing-file-error current-file file-path)]
-          ;;   (state/pub-event! [:notification/show
-          ;;                      {:content error
-          ;;                       :status :error
-          ;;                       :clear? false}]))
-        ))))
+        (worker-util/post-message :notify-existing-file
+                                  [{:current-file-path current-file-path
+                                    :file-path file-path}])))))
 
 (defn- validate-and-get-blocks-to-delete
   "An implementation for the delete-blocks-fn in graph-parser/parse-file"
-  [conn file-page file-path retain-uuid-blocks]
-  (validate-existing-file conn file-page file-path)
+  [repo conn file-page file-path retain-uuid-blocks]
+  (validate-existing-file repo conn file-page file-path)
   (graph-parser/get-blocks-to-delete @conn file-page file-path retain-uuid-blocks))
 
 (defn- reset-file!*
@@ -69,7 +66,7 @@
   ([repo conn file-path content {:keys [verbose _ctime _mtime] :as options}]
    (let [config (worker-state/get-config repo)
          options (merge (dissoc options :verbose)
-                        {:delete-blocks-fn (partial validate-and-get-blocks-to-delete conn)
+                        {:delete-blocks-fn (partial validate-and-get-blocks-to-delete repo conn)
                          ;; Options here should also be present in gp-cli/parse-graph
                          :extract-options (merge
                                            {:user-config config