Skip to content

Commit

Permalink
enhance(fs): check graph folder for bad name and nested graph
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf authored and tiensonqin committed Sep 4, 2023
1 parent 2d34af9 commit 5058f59
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion deps/common/src/logseq/common/path.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
;; compat
(defn basename
[path]
(let [path (string/replace path #"/$" "")]
(let [path (string/replace path #"/+$" "")]
(filename path)))

(defn dirname
Expand Down
30 changes: 29 additions & 1 deletion src/main/frontend/handler/web/nfs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
[goog.object :as gobj]
[lambdaisland.glogi :as log]
[logseq.graph-parser.util :as gp-util]
[promesa.core :as p]))
[promesa.core :as p]
[logseq.common.path :as path]))

(defn remove-ignore-files
[files dir-name nfs?]
Expand Down Expand Up @@ -86,6 +87,32 @@
(keyword (util/get-file-ext (:file/path file)))))
files))

(defn- precheck-graph-dir
"Check graph dir, notify user if:
- Grame dir name is `logseq`, the same as app, which might cause confusion
- Graph dir contains a nested graph, which should be avoided
- Over 10000 files found in graph dir, which might cause performance issues"
[dir files]
(when (= (string/lower-case (path/basename dir))
"logseq")
(state/pub-event!
[:notification/show {:content [:div "The folder name "
[:code "logseq"]
" is not suitable for a graph name. Please unlink this graph and choose a different name."]
:status :warning
:clear? false}]))
(when (some #(string/ends-with? (:path %) "/logseq/config.edn") files)
(state/pub-event!
[:notification/show {:content "It seems that you are trying to open a Logseq graph folder with nested graph. Please unlink this graph and choose a correct folder."
:status :warning
:clear? false}]))
(when (>= (count files) 10000)
(state/pub-event!
[:notification/show {:content "It seems that you are trying to open a Logseq graph folder that contains an excessive number of files, This might lead to performance issues."
:status :warning
:clear? true}])))

;; TODO: extract code for `ls-dir-files` and `reload-dir!`
(defn ls-dir-files-with-handler!
"Read files from directory and setup repo (for the first time setup a repo)"
Expand Down Expand Up @@ -113,6 +140,7 @@
(reset! *repo repo)
(when-not (string/blank? root-dir)
(p/let [files (:files result)
_ (precheck-graph-dir root-dir (:files result))
files (-> (->db-files files nfs?)
;; filter again, in case fs backend does not handle this
(remove-ignore-files root-dir nfs?))
Expand Down

0 comments on commit 5058f59

Please sign in to comment.