watcher_handler.cljs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. (ns frontend.fs.watcher-handler
  2. (:require [clojure.string :as string]
  3. [frontend.config :as config]
  4. [frontend.db :as db]
  5. [frontend.db.model :as model]
  6. [frontend.handler.editor :as editor]
  7. [frontend.handler.file :as file-handler]
  8. [frontend.handler.page :as page-handler]
  9. [frontend.handler.repo :as repo-handler]
  10. [frontend.handler.ui :as ui-handler]
  11. [logseq.graph-parser.util :as gp-util]
  12. [logseq.graph-parser.util.block-ref :as block-ref]
  13. [lambdaisland.glogi :as log]
  14. [promesa.core :as p]
  15. [frontend.state :as state]
  16. [frontend.encrypt :as encrypt]
  17. [frontend.fs :as fs]))
  18. ;; all IPC paths must be normalized! (via gp-util/path-normalize)
  19. (defn- set-missing-block-ids!
  20. [content]
  21. (when (string? content)
  22. (doseq [block-id (block-ref/get-all-block-ref-ids content)]
  23. (when-let [block (try
  24. (model/get-block-by-uuid block-id)
  25. (catch js/Error _e
  26. nil))]
  27. (let [id-property (:id (:block/properties block))]
  28. (when-not (= (str id-property) (str block-id))
  29. (editor/set-block-property! block-id "id" block-id)))))))
  30. (defn- handle-add-and-change!
  31. [repo path content db-content mtime backup?]
  32. (p/let [
  33. ;; save the previous content in a versioned bak file to avoid data overwritten.
  34. _ (when backup?
  35. (-> (when-let [repo-dir (config/get-local-dir repo)]
  36. (file-handler/backup-file! repo-dir path db-content content))
  37. (p/catch #(js/console.error "❌ Bak Error: " path %))))
  38. _ (file-handler/alter-file repo path content {:re-render-root? true
  39. :from-disk? true})]
  40. (set-missing-block-ids! content)
  41. (db/set-file-last-modified-at! repo path mtime)))
  42. (defn handle-changed!
  43. [type {:keys [dir path content stat] :as payload}]
  44. (when dir
  45. (let [path (gp-util/path-normalize path)
  46. repo (config/get-local-repo dir)
  47. pages-metadata-path (config/get-pages-metadata-path)
  48. {:keys [mtime]} stat
  49. db-content (or (db/get-file repo path) "")]
  50. (when (and (or content (contains? #{"unlink" "unlinkDir" "addDir"} type))
  51. (not (encrypt/content-encrypted? content))
  52. (not (:encryption/graph-parsing? @state/state)))
  53. (cond
  54. (and (= "unlinkDir" type) dir)
  55. (state/pub-event! [:graph/dir-gone dir])
  56. (and (= "addDir" type) dir)
  57. (state/pub-event! [:graph/dir-back repo dir])
  58. (contains? (:file/unlinked-dirs @state/state) dir)
  59. nil
  60. (and (= "add" type)
  61. (not= (string/trim content) (string/trim db-content))
  62. (not= path pages-metadata-path))
  63. (let [backup? (not (string/blank? db-content))]
  64. (handle-add-and-change! repo path content db-content mtime backup?))
  65. (and (= "change" type)
  66. (not (db/file-exists? repo path)))
  67. (js/console.error "Can't get file in the db: " path)
  68. (and (= "change" type)
  69. (not= (string/trim content) (string/trim db-content))
  70. (not= path pages-metadata-path))
  71. (when-not (and
  72. (string/includes? path (str "/" (config/get-journals-directory) "/"))
  73. (or
  74. (= (string/trim content)
  75. (string/trim (or (state/get-default-journal-template) "")))
  76. (= (string/trim content) "-")
  77. (= (string/trim content) "*")))
  78. (handle-add-and-change! repo path content db-content mtime true))
  79. (and (= "unlink" type)
  80. (db/file-exists? repo path))
  81. (p/let [dir-exists? (fs/file-exists? dir "")]
  82. (when dir-exists?
  83. (when-let [page-name (db/get-file-page path)]
  84. (println "Delete page: " page-name ", file path: " path ".")
  85. (page-handler/delete! page-name #() :delete-file? false))))
  86. (and (contains? #{"add" "change" "unlink"} type)
  87. (string/ends-with? path "logseq/custom.css"))
  88. (do
  89. (println "reloading custom.css")
  90. (ui-handler/add-style-if-exists!))
  91. ;; When metadata is added to watcher, update timestamps in db accordingly
  92. ;; This event is not triggered on re-index
  93. ;; Persistent metadata is gold standard when db is offline, so it's forced
  94. (and (contains? #{"add"} type)
  95. (= path pages-metadata-path))
  96. (p/do! (repo-handler/update-pages-metadata! repo content true))
  97. ;; Change is triggered by external changes, so update to the db
  98. ;; Don't forced update when db is online, but resolving conflicts
  99. (and (contains? #{"change"} type)
  100. (= path pages-metadata-path))
  101. (p/do! (repo-handler/update-pages-metadata! repo content false))
  102. (contains? #{"add" "change" "unlink"} type)
  103. nil
  104. :else
  105. (log/error :fs/watcher-no-handler {:type type
  106. :payload payload})))
  107. ;; return nil, otherwise the entire db will be transfered by ipc
  108. nil)))