Skip to content

Commit

Permalink
fix: readdir with path-only? option
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Oct 13, 2022
1 parent 127a4d1 commit 91ceae4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
8 changes: 6 additions & 2 deletions src/main/frontend/fs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@
(protocol/mkdir-recur! (get-fs dir) dir))

(defn readdir
[dir]
(protocol/readdir (get-fs dir) dir))
[dir & {:keys [path-only?]}]
(p/let [result (protocol/readdir (get-fs dir) dir)
result (bean/->clj result)]
(if (and path-only? (map? (first result)))
(map :uri result)
result)))

(defn unlink!
"Should move the path to logseq/recycle instead of deleting it."
Expand Down
25 changes: 14 additions & 11 deletions src/main/frontend/fs/capacitor_fs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,14 @@
(defn- truncate-old-versioned-files!
"reserve the latest 6 version files"
[dir]
(p/let [files (readdir dir)
files (js->clj files :keywordize-keys true)
old-versioned-files (drop 6 (reverse (sort-by :mtime files)))]
(mapv (fn [file]
(.deleteFile Filesystem (clj->js {:path (:uri file)})))
old-versioned-files)))
(->
(p/let [files (readdir dir)
files (js->clj files :keywordize-keys true)
old-versioned-files (drop 6 (reverse (sort-by :mtime files)))]
(mapv (fn [file]
(.deleteFile Filesystem (clj->js {:path (:uri file)})))
old-versioned-files))
(p/catch (fn [_]))))

;; TODO: move this to FS protocol
(defn backup-file
Expand All @@ -139,13 +141,14 @@
:version-file-dir = `version-file-dir`"
[repo dir path content]
{:pre [(contains? #{:backup-dir :version-file-dir} dir)]}
(let [ext (util/get-file-ext path)
(let [repo-dir (config/get-local-dir repo)
ext (util/get-file-ext path)
dir (case dir
:backup-dir (get-backup-dir repo path backup-dir ext)
:version-file-dir (get-backup-dir repo path version-file-dir ext))
new-path (util/safe-path-join dir (str (string/replace (.toISOString (js/Date.)) ":" "_") "." ext))]
:backup-dir (get-backup-dir repo-dir path backup-dir ext)
:version-file-dir (get-backup-dir repo-dir path version-file-dir ext))
new-path (util/safe-path-join dir (str (string/replace (.toISOString (js/Date.)) ":" "_") "." (mobile-util/platform) "." ext))]
(<write-file-with-utf8 new-path content)
(truncate-old-versioned-files! backup-dir)))
(truncate-old-versioned-files! dir)))

(defn backup-file-handle-changed!
[repo-dir file-path content]
Expand Down
14 changes: 3 additions & 11 deletions src/main/frontend/handler/file_sync.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,9 @@
(#(js->clj % :keywordize-keys true))
((juxt :dir :name))
(apply path/join base-path))
version-file-paths* (<! (p->c (fs/readdir version-files-dir)))]
(when-not (instance? ExceptionInfo version-file-paths*)
(let [version-file-paths
(filterv
;; filter dir
(fn [dir-or-file]
(-> (path/parse dir-or-file)
(js->clj :keywordize-keys true)
:ext
seq))
(js->clj (<! (p->c (fs/readdir version-files-dir)))))]
version-file-paths (<! (p->c (fs/readdir version-files-dir :path-only? true)))]
(when-not (instance? ExceptionInfo version-file-paths)
(when (seq version-file-paths)
(mapv
(fn [path]
(let [create-time
Expand Down

0 comments on commit 91ceae4

Please sign in to comment.