fix(electron): assets:// url handling

This commit is contained in:
Andelf
2023-03-11 13:04:38 +08:00
parent d666a6d35a
commit 6082663f4c
6 changed files with 15 additions and 9 deletions

View File

@@ -68,8 +68,8 @@
(fn [^js request callback]
(let [url (.-url request)
url (decode-protected-assets-schema-path url)
path (js/decodeURI url)
path (string/replace path "assets://" "")]
path (string/replace url "assets://" "")
path (js/decodeURIComponent path)]
(callback #js {:path path}))))
(.registerFileProtocol

View File

@@ -81,7 +81,8 @@
[reitit.frontend.easy :as rfe]
[rum.core :as rum]
[shadow.loader :as loader]
[datascript.impl.entity :as e]))
[datascript.impl.entity :as e]
[frontend.fs2.path :as fs2-path]))
(defn safe-read-string
([s]
@@ -199,7 +200,7 @@
(if (and (gp-config/local-protocol-asset? src)
(file-sync/current-graph-sync-on?))
(let [*exist? (::exist? state)
asset-path (gp-config/remove-asset-protocol src)]
asset-path (fs2-path/url-to-path src)]
(if (string/blank? asset-path)
(reset! *exist? false)
;; FIXME(andelf): possible bug here

View File

@@ -474,6 +474,7 @@
(defn expand-relative-assets-path
;; ../assets/xxx -> {assets|file}://{current-graph-root-path}/xxx
[source]
(js/console.error "BUG: assets:// url handling")
(when-let [protocol (and (string? source)
(not (string/blank? source))
(if (util/electron?) "assets" "file"))]

View File

@@ -69,7 +69,6 @@
ext (string/lower-case (util/get-file-ext rpath))
db-content (or old-content (db/get-file repo rpath) "")
contents-matched? (contents-matched? disk-content db-content)]
(prn ::disk disk-content ::db db-content ::new content)
(cond
(and
(not= stat :not-found) ; file on the disk was deleted
@@ -77,7 +76,6 @@
(not (contains? #{"excalidraw" "edn" "css"} ext))
(not (string/includes? rpath "/.recycle/")))
(do
(prn ::?????)
(state/pub-event! [:file/not-matched-from-disk rpath disk-content content]))
:else

View File

@@ -203,9 +203,13 @@
(defn url-to-path
"Extract path part of a URL. decoded"
[original-url]
(let [^js url (js/URL. original-url)
path (gp-util/safe-decode-uri-component (.-pathname url))]
path))
(if (is-file-url original-url)
;; NOTE: URL type is not consistent across all protocols
;; Check file:// and assets://, pathname behavior is different
(let [^js url (js/URL. (string/replace original-url "assets://" "file://"))
path (gp-util/safe-decode-uri-component (.-pathname url))]
path)
original-url))
(defn relative-path
@@ -213,6 +217,7 @@
Works for both path and URL."
[base-path sub-path]
(prn :rel-path base-path sub-path)
;; (js/console.trace)
(let [base-path (path-normalize base-path)
sub-path (path-normalize sub-path)
is-url? (is-file-url base-path)]