Skip to content

Commit

Permalink
fix: only proceed if permission verified
Browse files Browse the repository at this point in the history
  • Loading branch information
gyk authored and tiensonqin committed Dec 11, 2020
1 parent 0b0598c commit 92e791e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/main/frontend/fs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
(local-db? dir)
(let [[root new-dir] (rest (string/split dir "/"))
root-handle (str "handle/" root)]
(p/let [handle (idb/get-item root-handle)]
(when handle (utils/verifyPermission handle true))
(p/let [handle (idb/get-item root-handle)
_ (when handle (utils/verifyPermission handle true))]
(when (and handle new-dir
(not (string/blank? new-dir)))
(-> (p/let [handle (.getDirectoryHandle ^js handle new-dir
Expand Down Expand Up @@ -181,8 +181,8 @@
(if (and local-content old-content new?
(or not-changed? new-created?))
(do
(utils/verifyPermission file-handle true)
(p/let [_ (utils/writeFile file-handle content)
(p/let [_ (utils/verifyPermission file-handle true)
_ (utils/writeFile file-handle content)
file (.getFile file-handle)]
(when file
(nfs-saved-handler repo path file))))
Expand All @@ -194,8 +194,8 @@
(p/let [handle (idb/get-item handle-path)]
(if handle
(do
(utils/verifyPermission handle true)
(p/let [file-handle (.getFileHandle ^js handle basename #js {:create true})
(p/let [_ (utils/verifyPermission handle true)
file-handle (.getFileHandle ^js handle basename #js {:create true})
_ (idb/set-item! basename-handle-path file-handle)
_ (utils/writeFile file-handle content)
file (.getFile file-handle)]
Expand Down
5 changes: 2 additions & 3 deletions src/main/frontend/handler/repo.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@
(create-today-journal-if-not-exists repo-url nil))
([repo-url content]
(spec/validate :repos/url repo-url)
(when (config/local-db? repo-url)
(fs/check-directory-permission! repo-url))
(let [repo-dir (util/get-repo-dir repo-url)
format (state/get-preferred-format repo-url)
title (date/today)
Expand All @@ -134,7 +132,8 @@
empty-blocks? (empty? (db/get-page-blocks-no-cache repo-url (string/lower-case title)))]
(when (or empty-blocks?
(not page-exists?))
(p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" config/default-journals-directory))
(p/let [_ (fs/check-directory-permission! repo-url)
_ (fs/mkdir-if-not-exists (str repo-dir "/" config/default-journals-directory))
file-exists? (fs/create-if-not-exists repo-url repo-dir file-path content)]
(when-not file-exists?
(db/reset-file! repo-url path content)
Expand Down
12 changes: 6 additions & 6 deletions src/main/frontend/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@ export var verifyPermission = async function (handle, readWrite) {
if (readWrite) {
options.mode = 'readwrite';
}
// Check if permission was already granted. If so, return true.
// Check if permission was already granted.
if ((await handle.queryPermission(options)) === 'granted') {
return true;
return;
}
// Request permission. If the user grants permission, return true.
// Request permission. If the user grants permission, just return.
if ((await handle.requestPermission(options)) === 'granted') {
return true;
return;
}
// The user didn't grant permission, so return false.
return false;
// The user didn't grant permission, throw an error.
throw new Error("Permission is not granted");
}

export var openDirectory = async function (options = {}, cb) {
Expand Down

0 comments on commit 92e791e

Please sign in to comment.