feat: chrome native compatible fs api

This commit is contained in:
Tienson Qin
2020-11-23 17:10:29 +08:00
parent 3a81b5e856
commit 50a7a7c585
7 changed files with 152 additions and 82 deletions

View File

@@ -16,51 +16,56 @@
(defn ls-dir-files
[]
(->
(p/let [result (utils/openDirectory #js {:recursive true})
root-handle (nth result 0)
dir-name (gobj/get root-handle "name")
repo (str config/local-db-prefix dir-name)
root-handle-path (str "handle-" repo)
_ (idb/set-item! root-handle-path root-handle)
_ (fs/add-nfs-file-handle! root-handle-path root-handle)
result (nth result 1)
result (flatten (bean/->clj result))
files (doall
(map (fn [file]
(let [handle (gobj/get file "handle")
get-attr #(gobj/get file %)]
{:file/path (get-attr "webkitRelativePath")
:file/last-modified-at (get-attr "lastModified")
:file/size (get-attr "size")
:file/type (get-attr "type")
:file/file file
:file/handle handle})) result))
text-files (filter (fn [file] (contains? #{"org" "md" "markdown"} (util/get-file-ext (:file/path file)))) files)]
(doseq [file text-files]
(let [handle-path (str "handle-" repo "/" (:file/path file))
handle (:file/handle file)]
(idb/set-item! handle-path handle)
(fs/add-nfs-file-handle! handle-path handle)))
(-> (p/all (map (fn [file]
(p/let [content (.text (:file/file file))]
(assoc file :file/content content))) text-files))
(p/then (fn [result]
(let [files (map #(dissoc % :file/file) result)]
(repo-handler/start-repo-db-if-not-exists! repo {:db-type :local-native-fs})
(repo-handler/load-repo-to-db! repo
{:first-clone? true
:nfs-files (map :file/path files)
:nfs-contents (mapv (fn [f] [(:file/path f) (:file/content f)]) files)
:additional-files-info files})
;; create default directories and files
)))
(p/catch (fn [error]
(println "Load files content error: ")
(js/console.dir error)))))
(p/catch (fn [error]
(println "Open directory error: ")
(js/console.dir error)))))
(let [path-handles (atom {})]
(->
(p/let [result (utils/openDirectory #js {:recursive true}
(fn [path handle]
(swap! path-handles assoc path handle)))
root-handle (nth result 0)
dir-name (gobj/get root-handle "name")
repo (str config/local-db-prefix dir-name)
root-handle-path (str config/local-handle-prefix dir-name)
_ (idb/set-item! root-handle-path root-handle)
_ (fs/add-nfs-file-handle! root-handle-path root-handle)
_ (doseq [[path handle] @path-handles]
(let [handle-path (str config/local-handle-prefix path)]
(idb/set-item! handle-path handle)
(fs/add-nfs-file-handle! handle-path handle)))
result (nth result 1)
result (flatten (bean/->clj result))
files (doall
(map (fn [file]
(let [handle (gobj/get file "handle")
get-attr #(gobj/get file %)]
{:file/path (get-attr "webkitRelativePath")
:file/last-modified-at (get-attr "lastModified")
:file/size (get-attr "size")
:file/type (get-attr "type")
:file/file file
:file/handle handle})) result))
markup-files (filter (fn [file]
(contains? config/markup-formats
(keyword (util/get-file-ext (:file/path file)))))
files)]
(-> (p/all (map (fn [file]
(p/let [content (.text (:file/file file))]
(assoc file :file/content content))) markup-files))
(p/then (fn [result]
(let [files (map #(dissoc % :file/file) result)]
(repo-handler/start-repo-db-if-not-exists! repo {:db-type :local-native-fs})
(repo-handler/load-repo-to-db! repo
{:first-clone? true
:nfs-files (map :file/path files)
:nfs-contents (mapv (fn [f] [(:file/path f) (:file/content f)]) files)
:additional-files-info files})
;; create default directories and files
)))
(p/catch (fn [error]
(println "Load files content error: ")
(js/console.dir error)))))
(p/catch (fn [error]
(println "Open directory error: ")
(js/console.dir error))))))
(defn open-file-picker
"Shows a file picker that lets a user select a single existing file, returning a handle for the selected file. "