Răsfoiți Sursa

enhance: backup when `add` is notified and db content is not empty

Tienson Qin 4 ani în urmă
părinte
comite
ec289c788f
1 a modificat fișierele cu 13 adăugiri și 11 ștergeri
  1. 13 11
      src/main/frontend/fs/watcher_handler.cljs

+ 13 - 11
src/main/frontend/fs/watcher_handler.cljs

@@ -25,6 +25,16 @@
           (when-not (= (str id-property) (str block-id))
             (editor/set-block-property! block-id "id" block-id)))))))
 
+(defn- handle-add-and-change!
+  [repo path content db-content mtime backup?]
+  (p/let [
+          ;; save the previous content in a bak file to avoid data overwritten.
+          _ (when backup? (ipc/ipc "backupDbFile" (config/get-local-dir repo) path db-content))
+          _ (file-handler/alter-file repo path content {:re-render-root? true
+                                                        :from-disk? true})]
+    (set-missing-block-ids! content)
+    (db/set-file-last-modified-at! repo path mtime)))
+
 (defn handle-changed!
   [type {:keys [dir path content stat] :as payload}]
   (when dir
@@ -38,10 +48,8 @@
           (and (= "add" type)
                (not= (string/trim content) (string/trim db-content))
                (not (string/includes? path "logseq/pages-metadata.edn")))
-          (p/let [_ (file-handler/alter-file repo path content {:re-render-root? true
-                                                                :from-disk? true})]
-            (set-missing-block-ids! content)
-            (db/set-file-last-modified-at! repo path mtime))
+          (let [backup? (not (string/blank? db-content))]
+            (handle-add-or-changed! repo path content db-content mtime backup?))
 
           (and (= "change" type)
                (not (db/file-exists? repo path)))
@@ -57,13 +65,7 @@
                          (string/trim (or (state/get-default-journal-template) "")))
                       (= (string/trim content) "-")
                       (= (string/trim content) "*")))
-            (p/let [
-                    ;; save the previous content in a bak file to avoid data overwritten.
-                    _ (ipc/ipc "backupDbFile" (config/get-local-dir repo) path db-content)
-                    _ (file-handler/alter-file repo path content {:re-render-root? true
-                                                                  :from-disk? true})]
-              (set-missing-block-ids! content)
-              (db/set-file-last-modified-at! repo path mtime)))
+            (handle-add-or-changed! repo path content db-content mtime true))
 
           (and (= "unlink" type)
                (db/file-exists? repo path))