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

fix: WIP file bak when external file changed

charlie 3 лет назад
Родитель
Сommit
b6caba48d8

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

@@ -44,7 +44,7 @@
                :version-file-dir (get-version-file-dir repo relative-path))
         new-path (path/join dir*
                             (str (string/replace (.toISOString (js/Date.)) ":" "_")
-                                 ext))]
+                                 ".Desktop" ext))]
     (fs-extra/ensureDirSync dir*)
     (fs/writeFileSync new-path content)
     (fs/statSync new-path)

+ 20 - 0
src/main/frontend/fs/capacitor_fs.cljs

@@ -135,6 +135,25 @@
                                      :recursive true}))
     (truncate-old-versioned-files! backup-dir)))
 
+(defn backup-file-handle-changed!
+  [repo-dir file-path content]
+  (let [repo-dir (js/decodeURI repo-dir)
+        file-path (js/decodeURI file-path)
+        backup-root (util/node-path.join repo-dir backup-dir)
+        backup-dir-parent (util/node-path.dirname file-path)
+        backup-dir-parent (string/replace backup-dir-parent repo-dir "")
+        backup-dir-name (util/node-path.name file-path)
+        file-extname (util/node-path.extname file-path)
+        new-path (util/node-path.join
+                  backup-root backup-dir-parent backup-dir-name
+                  (str (string/replace (.toISOString (js/Date.)) ":" "_") ".Mobile" file-extname))]
+    (prn "====> Ready Backup mobile file::" repo-dir "++++" file-path "++++" new-path)
+    (.writeFile Filesystem (clj->js {:data content
+                                     :path new-path
+                                     :encoding (.-UTF8 Encoding)
+                                     :recursive true}))
+    (truncate-old-versioned-files! backup-dir)))
+
 (defn- write-file-impl!
   [_this repo _dir path content {:keys [ok-handler error-handler old-content skip-compare?]} stat]
   (if skip-compare?
@@ -180,6 +199,7 @@
                                                          :recursive true}))
                  mtime (-> (js->clj stat :keywordize-keys true)
                            :mtime)]
+           (prn "====>>> Write File:" path "\n matched?" contents-matched?)
            (when-not contents-matched?
              (backup-file repo-dir path disk-content ext))
            (db/set-file-last-modified-at! repo path mtime)

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

@@ -1,6 +1,7 @@
 (ns frontend.fs.watcher-handler
   (:require [clojure.string :as string]
             [frontend.config :as config]
+            [frontend.util :as util]
             [frontend.db :as db]
             [frontend.db.model :as model]
             [frontend.handler.editor :as editor]
@@ -15,7 +16,8 @@
             [promesa.core :as p]
             [frontend.state :as state]
             [frontend.encrypt :as encrypt]
-            [frontend.fs :as fs]))
+            [frontend.fs :as fs]
+            [frontend.fs.capacitor-fs :as capacitor-fs]))
 
 ;; all IPC paths must be normalized! (via gp-util/path-normalize)
 
@@ -35,7 +37,10 @@
   [repo path content db-content mtime backup?]
   (p/let [
           ;; save the previous content in a versioned bak file to avoid data overwritten.
-          _ (when backup? (ipc/ipc "backupDbFile" (config/get-local-dir repo) path db-content content))
+          _ (when-let [repo-dir (and backup? (config/get-local-dir repo))]
+              (if (util/electron?)
+                (ipc/ipc "backupDbFile"  repo-dir path db-content content)
+                (capacitor-fs/backup-file-handle-changed! repo-dir path db-content)))
           _ (file-handler/alter-file repo path content {:re-render-root? true
                                                         :from-disk? true})]
     (set-missing-block-ids! content)