Browse Source

enhance: save files to logseq/bak when changes detected from the disk

Tienson Qin 4 years ago
parent
commit
5d5d88f8df
2 changed files with 14 additions and 7 deletions
  1. 11 0
      src/electron/electron/handler.cljs
  2. 3 7
      src/main/frontend/fs/watcher_handler.cljs

+ 11 - 0
src/electron/electron/handler.cljs

@@ -54,6 +54,17 @@
         new-path (str recycle-dir "/" file-name)]
     (fs/renameSync path new-path)))
 
+(defmethod handle :backupDbFile [_window [_ repo path db-content]]
+  (let [basename (path/basename path)
+        file-name (-> (string/replace path (str repo "/") "")
+                      (string/replace "/" "_")
+                      (string/replace "\\" "_"))
+        bak-dir (str repo "/logseq/bak")
+        _ (fs-extra/ensureDirSync bak-dir)
+        new-path (str bak-dir "/" file-name "." (.toISOString (js/Date.)))]
+    (fs/writeFileSync new-path db-content)
+    (fs/statSync new-path)))
+
 (defmethod handle :readFile [_window [_ path]]
   (utils/read-file path))
 

+ 3 - 7
src/main/frontend/fs/watcher_handler.cljs

@@ -55,14 +55,10 @@
                       (= (string/trim content) "-")
                       (= (string/trim content) "*")))
             (p/let [
-                    ;; save the previous content in Logseq first and commit it to avoid
-                    ;; any data-loss.
-                    _ (file-handler/alter-file repo path db-content {:re-render-root? false
-                                                                     :reset? false
-                                                                     :skip-compare? true})
-                    _ (ipc/ipc "gitCommitAll" "Save the file from Logseq's database")
+                    ;; 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
-                                                                  :skip-compare? true})]
+                                                                  :from-disk? true})]
               (set-missing-block-ids! content)
               (db/set-file-last-modified-at! repo path mtime)))