file.cljs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. (ns frontend.handler.common.file
  2. "Common file related fns for handlers"
  3. (:require [frontend.config :as config]
  4. [frontend.state :as state]
  5. [frontend.db :as db]
  6. [logseq.graph-parser :as graph-parser]
  7. [logseq.graph-parser.util :as gp-util]
  8. [logseq.graph-parser.config :as gp-config]
  9. [frontend.fs :as fs]
  10. [frontend.context.i18n :refer [t]]
  11. [clojure.string :as string]
  12. [promesa.core :as p]))
  13. (defn- page-exists-in-another-file
  14. "Conflict of files towards same page"
  15. [repo-url page file]
  16. (when-let [page-name (:block/name page)]
  17. (let [current-file (:file/path (db/get-page-file repo-url page-name))]
  18. (when (not= file current-file)
  19. current-file))))
  20. (defn- validate-existing-file
  21. [repo-url file-page file-path]
  22. (when-let [current-file (page-exists-in-another-file repo-url file-page file-path)]
  23. (when (not= file-path current-file)
  24. (cond
  25. (= (string/lower-case current-file)
  26. (string/lower-case file-path))
  27. ;; case renamed
  28. (when-let [file (db/pull [:file/path current-file])]
  29. (p/let [disk-content (fs/read-file "" current-file)]
  30. (fs/backup-db-file! repo-url current-file (:file/content file) disk-content))
  31. (db/transact! repo-url [{:db/id (:db/id file)
  32. :file/path file-path}]))
  33. :else
  34. (let [error (t :file/validate-existing-file-error current-file file-path)]
  35. (state/pub-event! [:notification/show
  36. {:content error
  37. :status :error
  38. :clear? false}]))))))
  39. (defn- validate-and-get-blocks-to-delete
  40. [repo-url db file-page file-path retain-uuid-blocks]
  41. (validate-existing-file repo-url file-page file-path)
  42. (graph-parser/get-blocks-to-delete db file-page file-path retain-uuid-blocks))
  43. (defn reset-file!
  44. "Main fn for updating a db with the results of a parsed file"
  45. ([repo-url file-path content]
  46. (reset-file! repo-url file-path content {}))
  47. ([repo-url file-path content {:keys [verbose] :as options}]
  48. (let [new? (nil? (db/entity [:file/path file-path]))
  49. options (merge (dissoc options :verbose)
  50. {:new? new?
  51. :delete-blocks-fn (partial validate-and-get-blocks-to-delete repo-url)
  52. ;; Options here should also be present in gp-cli/parse-graph
  53. :extract-options (merge
  54. {:user-config (state/get-config)
  55. :date-formatter (state/get-date-formatter)
  56. :block-pattern (config/get-block-pattern (gp-util/get-format file-path))
  57. :supported-formats (gp-config/supported-formats)
  58. :filename-format (state/get-filename-format repo-url)
  59. :extracted-block-ids (:extracted-block-ids options)}
  60. (when (some? verbose) {:verbose verbose}))})]
  61. (:tx (graph-parser/parse-file (db/get-db repo-url false) file-path content options)))))