file.cljs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. (ns frontend.handler.file
  2. (:refer-clojure :exclude [load-file])
  3. (:require ["/frontend/utils" :as utils]
  4. [borkdude.rewrite-edn :as rewrite]
  5. [cljs.core.async.interop :refer [<p!]]
  6. [clojure.core.async :as async]
  7. [frontend.config :as config]
  8. [frontend.db :as db]
  9. [frontend.fs :as fs]
  10. [frontend.fs.nfs :as nfs]
  11. [frontend.handler.common :as common-handler]
  12. [frontend.handler.ui :as ui-handler]
  13. [frontend.state :as state]
  14. [frontend.util :as util]
  15. [logseq.graph-parser.util :as gp-util]
  16. [lambdaisland.glogi :as log]
  17. [promesa.core :as p]
  18. [frontend.mobile.util :as mobile]
  19. [clojure.set :as set]
  20. [frontend.modules.outliner.file :as outliner-file]
  21. [logseq.graph-parser.config :as gp-config]
  22. [logseq.graph-parser :as graph-parser]))
  23. ;; TODO: extract all git ops using a channel
  24. (defn load-file
  25. [repo-url path]
  26. (->
  27. (p/let [content (fs/read-file (config/get-repo-dir repo-url) path)]
  28. content)
  29. (p/catch
  30. (fn [e]
  31. (println "Load file failed: " path)
  32. (js/console.error e)))))
  33. (defn load-multiple-files
  34. [repo-url paths]
  35. (doall
  36. (mapv #(load-file repo-url %) paths)))
  37. (defn- keep-formats
  38. [files formats]
  39. (filter
  40. (fn [file]
  41. (let [format (gp-util/get-format file)]
  42. (contains? formats format)))
  43. files))
  44. (defn- only-text-formats
  45. [files]
  46. (keep-formats files (gp-config/text-formats)))
  47. (defn- only-image-formats
  48. [files]
  49. (keep-formats files (gp-config/img-formats)))
  50. (defn restore-config!
  51. ([repo-url project-changed-check?]
  52. (restore-config! repo-url nil project-changed-check?))
  53. ([repo-url config-content _project-changed-check?]
  54. (let [config-content (if config-content config-content
  55. (common-handler/get-config repo-url))]
  56. (when config-content
  57. (common-handler/reset-config! repo-url config-content)))))
  58. (defn load-files-contents!
  59. [repo-url files ok-handler]
  60. (let [images (only-image-formats files)
  61. files (only-text-formats files)]
  62. (-> (p/all (load-multiple-files repo-url files))
  63. (p/then (fn [contents]
  64. (let [file-contents (cond->
  65. (zipmap files contents)
  66. (seq images)
  67. (merge (zipmap images (repeat (count images) ""))))
  68. file-contents (for [[file content] file-contents]
  69. {:file/path (gp-util/path-normalize file)
  70. :file/content content})]
  71. (ok-handler file-contents))))
  72. (p/catch (fn [error]
  73. (log/error :nfs/load-files-error repo-url)
  74. (log/error :exception error))))))
  75. (defn- page-exists-in-another-file
  76. "Conflict of files towards same page"
  77. [repo-url page file]
  78. (when-let [page-name (:block/name page)]
  79. (let [current-file (:file/path (db/get-page-file repo-url page-name))]
  80. (when (not= file current-file)
  81. current-file))))
  82. (defn- get-delete-blocks [repo-url first-page file]
  83. (let [delete-blocks (->
  84. (concat
  85. (db/delete-file-blocks! repo-url file)
  86. (when first-page (db/delete-page-blocks repo-url (:block/name first-page))))
  87. (distinct))]
  88. (when-let [current-file (page-exists-in-another-file repo-url first-page file)]
  89. (when (not= file current-file)
  90. (let [error (str "Page already exists with another file: " current-file ", current file: " file)]
  91. (state/pub-event! [:notification/show
  92. {:content error
  93. :status :error
  94. :clear? false}]))))
  95. delete-blocks))
  96. (defn reset-file!
  97. ([repo-url file content]
  98. (reset-file! repo-url file content {}))
  99. ([repo-url file content options]
  100. (let [electron-local-repo? (and (util/electron?)
  101. (config/local-db? repo-url))
  102. file (cond
  103. (and electron-local-repo?
  104. util/win32?
  105. (utils/win32 file))
  106. file
  107. (and electron-local-repo? (or
  108. util/win32?
  109. (not= "/" (first file))))
  110. (str (config/get-repo-dir repo-url) "/" file)
  111. (and (mobile/native-android?) (not= "/" (first file)))
  112. file
  113. (and (mobile/native-ios?) (not= "/" (first file)))
  114. file
  115. :else
  116. file)
  117. file (gp-util/path-normalize file)
  118. new? (nil? (db/entity [:file/path file]))]
  119. (graph-parser/parse-file
  120. (db/get-db repo-url false)
  121. file
  122. content
  123. (merge options
  124. {:new? new?
  125. :delete-blocks-fn (partial get-delete-blocks repo-url)
  126. :extract-options {:user-config (state/get-config)
  127. :date-formatter (state/get-date-formatter)
  128. :page-name-order (state/page-name-order)
  129. :block-pattern (config/get-block-pattern (gp-util/get-format file))
  130. :supported-formats (gp-config/supported-formats)}})))))
  131. ;; TODO: Remove this function in favor of `alter-files`
  132. (defn alter-file
  133. [repo path content {:keys [reset? re-render-root? from-disk? skip-compare? new-graph?]
  134. :or {reset? true
  135. re-render-root? false
  136. from-disk? false
  137. skip-compare? false}}]
  138. (let [original-content (db/get-file repo path)
  139. write-file! (if from-disk?
  140. #(p/resolved nil)
  141. #(fs/write-file! repo (config/get-repo-dir repo) path content
  142. (assoc (when original-content {:old-content original-content})
  143. :skip-compare? skip-compare?)))]
  144. (if reset?
  145. (do
  146. (when-let [page-id (db/get-file-page-id path)]
  147. (db/transact! repo
  148. [[:db/retract page-id :block/alias]
  149. [:db/retract page-id :block/tags]]))
  150. (reset-file! repo path content {:new-graph? new-graph?
  151. :from-disk? from-disk?}))
  152. (db/set-file-content! repo path content))
  153. (util/p-handle (write-file!)
  154. (fn [_]
  155. (when (= path (config/get-config-path repo))
  156. (restore-config! repo true))
  157. (when (= path (config/get-custom-css-path repo))
  158. (ui-handler/add-style-if-exists!))
  159. (when re-render-root? (ui-handler/re-render-root!)))
  160. (fn [error]
  161. (println "Write file failed, path: " path ", content: " content)
  162. (log/error :write/failed error)))))
  163. (defn set-file-content!
  164. [repo path new-content]
  165. (alter-file repo path new-content {:reset? false
  166. :re-render-root? false}))
  167. (defn alter-files
  168. [repo files {:keys [reset? update-db?]
  169. :or {reset? false
  170. update-db? true}
  171. :as opts}]
  172. ;; old file content
  173. (let [file->content (let [paths (map first files)]
  174. (zipmap paths
  175. (map (fn [path] (db/get-file repo path)) paths)))]
  176. ;; update db
  177. (when update-db?
  178. (doseq [[path content] files]
  179. (if reset?
  180. (reset-file! repo path content)
  181. (db/set-file-content! repo path content))))
  182. (when-let [chan (state/get-file-write-chan)]
  183. (let [chan-callback (:chan-callback opts)]
  184. (async/put! chan [repo files opts file->content])
  185. (when chan-callback
  186. (chan-callback))))))
  187. (defn alter-files-handler!
  188. [repo files {:keys [finish-handler chan]} file->content]
  189. (let [write-file-f (fn [[path content]]
  190. (when path
  191. (let [original-content (get file->content path)]
  192. (-> (p/let [_ (or
  193. (util/electron?)
  194. (nfs/check-directory-permission! repo))]
  195. (fs/write-file! repo (config/get-repo-dir repo) path content
  196. {:old-content original-content}))
  197. (p/catch (fn [error]
  198. (state/pub-event! [:notification/show
  199. {:content (str "Failed to save the file " path ". Error: "
  200. (str error))
  201. :status :error
  202. :clear? false}])
  203. (state/pub-event! [:instrument {:type :write-file/failed
  204. :payload {:path path
  205. :content-length (count content)
  206. :error-str (str error)
  207. :error error}}])
  208. (log/error :write-file/failed {:path path
  209. :content content
  210. :error error})))))))
  211. finish-handler (fn []
  212. (when finish-handler
  213. (finish-handler))
  214. (ui-handler/re-render-file!))]
  215. (-> (p/all (map write-file-f files))
  216. (p/then (fn []
  217. (finish-handler)
  218. (when chan
  219. (async/put! chan true))))
  220. (p/catch (fn [error]
  221. (println "Alter files failed:")
  222. (js/console.error error)
  223. (async/put! chan false))))))
  224. (defn run-writes-chan!
  225. []
  226. (let [chan (state/get-file-write-chan)]
  227. (async/go-loop []
  228. (let [args (async/<! chan)]
  229. ;; return a channel
  230. (try
  231. (<p! (apply alter-files-handler! args))
  232. (catch js/Error e
  233. (log/error :file/write-failed e))))
  234. (recur))
  235. (outliner-file/ratelimit-file-writes!)
  236. chan))
  237. (defn watch-for-current-graph-dir!
  238. []
  239. (when-let [repo (state/get-current-repo)]
  240. (when-let [dir (config/get-repo-dir repo)]
  241. (fs/watch-dir! dir))))
  242. (defn create-metadata-file
  243. [repo-url encrypted?]
  244. (let [repo-dir (config/get-repo-dir repo-url)
  245. path (str config/app-name "/" config/metadata-file)
  246. file-path (str "/" path)
  247. default-content (if encrypted? "{:db/encrypted? true}" "{}")]
  248. (p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" config/app-name))
  249. file-exists? (fs/create-if-not-exists repo-url repo-dir file-path default-content)]
  250. (when-not file-exists?
  251. (reset-file! repo-url path default-content)))))
  252. (defn create-pages-metadata-file
  253. [repo-url]
  254. (let [repo-dir (config/get-repo-dir repo-url)
  255. path (str config/app-name "/" config/pages-metadata-file)
  256. file-path (str "/" path)
  257. default-content "{}"]
  258. (p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" config/app-name))
  259. file-exists? (fs/create-if-not-exists repo-url repo-dir file-path default-content)]
  260. (when-not file-exists?
  261. (reset-file! repo-url path default-content)))))
  262. (defn edn-file-set-key-value
  263. [path k v]
  264. (when-let [repo (state/get-current-repo)]
  265. (when-let [content (db/get-file path)]
  266. (common-handler/read-config content)
  267. (let [result (common-handler/parse-config content)
  268. ks (if (vector? k) k [k])
  269. new-result (rewrite/assoc-in result ks v)
  270. new-content (str new-result)]
  271. (set-file-content! repo path new-content)))))