|
@@ -1,13 +1,13 @@
|
|
|
(ns frontend.handler.file
|
|
|
(:refer-clojure :exclude [load-file])
|
|
|
- (:require ["/frontend/utils" :as utils]
|
|
|
- [borkdude.rewrite-edn :as rewrite]
|
|
|
- [frontend.config :as config]
|
|
|
+ (:require [frontend.config :as config]
|
|
|
[frontend.db :as db]
|
|
|
[frontend.fs :as fs]
|
|
|
[frontend.fs.nfs :as nfs]
|
|
|
[frontend.fs.capacitor-fs :as capacitor-fs]
|
|
|
- [frontend.handler.common :as common-handler]
|
|
|
+ [frontend.handler.common.file :as file-common-handler]
|
|
|
+ [frontend.handler.repo-config :as repo-config-handler]
|
|
|
+ [frontend.handler.global-config :as global-config-handler]
|
|
|
[frontend.handler.ui :as ui-handler]
|
|
|
[frontend.state :as state]
|
|
|
[frontend.util :as util]
|
|
@@ -15,9 +15,8 @@
|
|
|
[electron.ipc :as ipc]
|
|
|
[lambdaisland.glogi :as log]
|
|
|
[promesa.core :as p]
|
|
|
- [frontend.mobile.util :as mobile]
|
|
|
+ [frontend.mobile.util :as mobile-util]
|
|
|
[logseq.graph-parser.config :as gp-config]
|
|
|
- [logseq.graph-parser :as graph-parser]
|
|
|
["path" :as path]))
|
|
|
|
|
|
;; TODO: extract all git ops using a channel
|
|
@@ -53,20 +52,6 @@
|
|
|
[files]
|
|
|
(keep-formats files (gp-config/img-formats)))
|
|
|
|
|
|
-(defn restore-config!
|
|
|
- ([repo-url]
|
|
|
- (restore-config! repo-url nil))
|
|
|
- ([repo-url config-content]
|
|
|
- (let [config-content (if config-content config-content
|
|
|
- (common-handler/get-config repo-url))]
|
|
|
- (when config-content
|
|
|
- (common-handler/reset-config! repo-url config-content)))))
|
|
|
-
|
|
|
-(defn set-global-config-state!
|
|
|
- [repo-url]
|
|
|
- (let [config-content (common-handler/get-global-config-content repo-url)]
|
|
|
- (common-handler/reset-global-config! config-content)))
|
|
|
-
|
|
|
(defn load-files-contents!
|
|
|
[repo-url files ok-handler]
|
|
|
(let [images (only-image-formats files)
|
|
@@ -93,82 +78,12 @@
|
|
|
(util/electron?)
|
|
|
(ipc/ipc "backupDbFile" repo-url path db-content content)
|
|
|
|
|
|
- (mobile/native-platform?)
|
|
|
+ (mobile-util/native-platform?)
|
|
|
(capacitor-fs/backup-file-handle-changed! repo-url path db-content)
|
|
|
|
|
|
:else
|
|
|
nil))
|
|
|
|
|
|
-(defn- page-exists-in-another-file
|
|
|
- "Conflict of files towards same page"
|
|
|
- [repo-url page file]
|
|
|
- (when-let [page-name (:block/name page)]
|
|
|
- (let [current-file (:file/path (db/get-page-file repo-url page-name))]
|
|
|
- (when (not= file current-file)
|
|
|
- current-file))))
|
|
|
-
|
|
|
-(defn- get-delete-blocks [repo-url first-page file]
|
|
|
- (let [delete-blocks (->
|
|
|
- (concat
|
|
|
- (db/delete-file-blocks! repo-url file)
|
|
|
- (when first-page (db/delete-page-blocks repo-url (:block/name first-page))))
|
|
|
- (distinct))]
|
|
|
- (when-let [current-file (page-exists-in-another-file repo-url first-page file)]
|
|
|
- (when (not= file current-file)
|
|
|
- (let [error (str "Page already exists with another file: " current-file ", current file: " file)]
|
|
|
- (state/pub-event! [:notification/show
|
|
|
- {:content error
|
|
|
- :status :error
|
|
|
- :clear? false}]))))
|
|
|
- delete-blocks))
|
|
|
-
|
|
|
-(defn reset-file!
|
|
|
- ([repo-url file content]
|
|
|
- (reset-file! repo-url file content {}))
|
|
|
- ([repo-url file content {:keys [verbose] :as options}]
|
|
|
- (try
|
|
|
- (let [electron-local-repo? (and (util/electron?)
|
|
|
- (config/local-db? repo-url))
|
|
|
- file (cond
|
|
|
- (and electron-local-repo?
|
|
|
- util/win32?
|
|
|
- (utils/win32 file))
|
|
|
- file
|
|
|
-
|
|
|
- (and electron-local-repo? (or
|
|
|
- util/win32?
|
|
|
- (not= "/" (first file))))
|
|
|
- (str (config/get-repo-dir repo-url) "/" file)
|
|
|
-
|
|
|
- (and (mobile/native-android?) (not= "/" (first file)))
|
|
|
- file
|
|
|
-
|
|
|
- (and (mobile/native-ios?) (not= "/" (first file)))
|
|
|
- file
|
|
|
-
|
|
|
- :else
|
|
|
- file)
|
|
|
- file (gp-util/path-normalize file)
|
|
|
- new? (nil? (db/entity [:file/path file]))]
|
|
|
- (:tx
|
|
|
- (graph-parser/parse-file
|
|
|
- (db/get-db repo-url false)
|
|
|
- file
|
|
|
- content
|
|
|
- (merge (dissoc options :verbose)
|
|
|
- {:new? new?
|
|
|
- :delete-blocks-fn (partial get-delete-blocks repo-url)
|
|
|
- :extract-options (merge
|
|
|
- {:user-config (state/get-config)
|
|
|
- :date-formatter (state/get-date-formatter)
|
|
|
- :page-name-order (state/page-name-order)
|
|
|
- :block-pattern (config/get-block-pattern (gp-util/get-format file))
|
|
|
- :supported-formats (gp-config/supported-formats)}
|
|
|
- (when (some? verbose) {:verbose verbose}))}))))
|
|
|
- (catch :default e
|
|
|
- (prn "Reset file failed " {:file file})
|
|
|
- (log/error :exception e)))))
|
|
|
-
|
|
|
;; TODO: Remove this function in favor of `alter-files`
|
|
|
(defn alter-file
|
|
|
[repo path content {:keys [reset? re-render-root? from-disk? skip-compare? new-graph? verbose]
|
|
@@ -176,12 +91,11 @@
|
|
|
re-render-root? false
|
|
|
from-disk? false
|
|
|
skip-compare? false}}]
|
|
|
- (prn :ALTER repo path)
|
|
|
(let [original-content (db/get-file repo path)
|
|
|
write-file! (if from-disk?
|
|
|
#(p/resolved nil)
|
|
|
- #(let [path-dir (if (= (path/dirname path) (config/get-global-config-dir))
|
|
|
- (config/get-global-config-dir)
|
|
|
+ #(let [path-dir (if (= (path/dirname path) (global-config-handler/global-config-dir))
|
|
|
+ (global-config-handler/global-config-dir)
|
|
|
(config/get-repo-dir repo))]
|
|
|
(fs/write-file! repo path-dir path content
|
|
|
(assoc (when original-content {:old-content original-content})
|
|
@@ -195,24 +109,24 @@
|
|
|
[[:db/retract page-id :block/alias]
|
|
|
[:db/retract page-id :block/tags]]
|
|
|
opts))
|
|
|
- (reset-file! repo path content (merge opts
|
|
|
- (when (some? verbose) {:verbose verbose}))))
|
|
|
+ (file-common-handler/reset-file! repo path content (merge opts
|
|
|
+ (when (some? verbose) {:verbose verbose}))))
|
|
|
(db/set-file-content! repo path content opts))
|
|
|
(util/p-handle (write-file!)
|
|
|
(fn [_]
|
|
|
(cond
|
|
|
- (= path (config/get-config-path repo))
|
|
|
- (restore-config! repo)
|
|
|
+ (= path (config/get-repo-config-path repo))
|
|
|
+ (repo-config-handler/restore-repo-config! repo)
|
|
|
|
|
|
- (= path (config/get-global-config-path))
|
|
|
- (set-global-config-state! repo)
|
|
|
+ (= path (global-config-handler/global-config-path))
|
|
|
+ (global-config-handler/restore-global-config! repo)
|
|
|
|
|
|
(= path (config/get-custom-css-path repo))
|
|
|
(ui-handler/add-style-if-exists!))
|
|
|
|
|
|
(when re-render-root? (ui-handler/re-render-root!)))
|
|
|
(fn [error]
|
|
|
- (when (= path (config/get-global-config-path))
|
|
|
+ (when (= path (global-config-handler/global-config-path))
|
|
|
(state/pub-event! [:notification/show
|
|
|
{:content (str "Failed to write to file " path)
|
|
|
:status :error}]))
|
|
@@ -272,7 +186,7 @@
|
|
|
(when update-db?
|
|
|
(doseq [[path content] files]
|
|
|
(if reset?
|
|
|
- (reset-file! repo path content)
|
|
|
+ (file-common-handler/reset-file! repo path content)
|
|
|
(db/set-file-content! repo path content))))
|
|
|
(alter-files-handler! repo files opts file->content)))
|
|
|
|
|
@@ -283,15 +197,6 @@
|
|
|
(fs/unwatch-dir! dir)
|
|
|
(fs/watch-dir! dir))))
|
|
|
|
|
|
-(defn watch-for-global-config-dir!
|
|
|
- []
|
|
|
- (let [dir (config/get-global-config-dir)
|
|
|
- repo-dir (config/get-repo-dir (state/get-current-repo))]
|
|
|
- (fs/unwatch-dir! dir)
|
|
|
- ;; Even a global dir needs to know it's current graph in order to send
|
|
|
- ;; change events to the right window and graph db
|
|
|
- (fs/watch-dir! dir {:current-repo-dir repo-dir})))
|
|
|
-
|
|
|
(defn create-metadata-file
|
|
|
[repo-url encrypted?]
|
|
|
(let [repo-dir (config/get-repo-dir repo-url)
|
|
@@ -301,7 +206,7 @@
|
|
|
(p/let [_ (fs/mkdir-if-not-exists (util/safe-path-join repo-dir config/app-name))
|
|
|
file-exists? (fs/create-if-not-exists repo-url repo-dir file-path default-content)]
|
|
|
(when-not file-exists?
|
|
|
- (reset-file! repo-url path default-content)))))
|
|
|
+ (file-common-handler/reset-file! repo-url path default-content)))))
|
|
|
|
|
|
(defn create-pages-metadata-file
|
|
|
[repo-url]
|
|
@@ -312,15 +217,4 @@
|
|
|
(p/let [_ (fs/mkdir-if-not-exists (util/safe-path-join repo-dir config/app-name))
|
|
|
file-exists? (fs/create-if-not-exists repo-url repo-dir file-path default-content)]
|
|
|
(when-not file-exists?
|
|
|
- (reset-file! repo-url path default-content)))))
|
|
|
-
|
|
|
-(defn edn-file-set-key-value
|
|
|
- [path k v]
|
|
|
- (when-let [repo (state/get-current-repo)]
|
|
|
- (when-let [content (db/get-file path)]
|
|
|
- (common-handler/read-config content)
|
|
|
- (let [result (common-handler/parse-config content)
|
|
|
- ks (if (vector? k) k [k])
|
|
|
- new-result (rewrite/assoc-in result ks v)
|
|
|
- new-content (str new-result)]
|
|
|
- (set-file-content! repo path new-content)))))
|
|
|
+ (file-common-handler/reset-file! repo-url path default-content)))))
|