Skip to content

Commit

Permalink
fix: notify users to switch to the new filename format
Browse files Browse the repository at this point in the history
Notify users to switch to the new filename format if there're reserved
chars when syncing.

Also, this commit replace all `js/decodeURIComponent` with
`safe-decode-uri-component` to avoid UI crashes.
  • Loading branch information
tiensonqin committed Nov 7, 2022
1 parent a8eb781 commit dd0169b
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 28 deletions.
2 changes: 1 addition & 1 deletion deps/graph-parser/src/logseq/graph_parser/extract.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
(let [result (first (gp-util/split-last "." file-name))
ext (string/lower-case (gp-util/get-file-ext filepath))]
(if (or (gp-config/mldoc-support? ext) (= "edn" ext))
(js/decodeURIComponent (string/replace result "." "/"))
(gp-util/safe-decode-uri-component (string/replace result "." "/"))
result))))

(defn- get-page-name
Expand Down
16 changes: 12 additions & 4 deletions deps/graph-parser/src/logseq/graph_parser/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
[clojure.walk :as walk]
[logseq.graph-parser.log :as log]))

(defn safe-decode-uri-component
[uri]
(try
(js/decodeURIComponent uri)
(catch :default e
(println "decodeURIComponent failed: " uri)
(js/console.error e)
uri)))

(defn safe-url-decode
[string]
(if (string/includes? string "%")
(try (some-> string str (js/decodeURIComponent))
(catch :default _
string))
(some-> string str safe-decode-uri-component)
string))

(defn path-normalize
Expand Down Expand Up @@ -220,7 +227,8 @@
;; Source: https://github.com/logseq/logseq/blob/e7110eea6790eda5861fdedb6b02c2a78b504cd9/deps/graph-parser/src/logseq/graph_parser/extract.cljc#L35
(defn legacy-title-parsing
[file-name-body]
(js/decodeURIComponent (string/replace file-name-body "." "/")))
(let [title (string/replace file-name-body "." "/")]
(or (safe-decode-uri-component title) title)))

;; Register sanitization / parsing fns in:
;; logseq.graph-parser.util (parsing only)
Expand Down
2 changes: 1 addition & 1 deletion src/electron/electron/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
[STATIC_URL js/__dirname])

path' (.-pathname url')
path' (js/decodeURIComponent path')
path' (utils/safe-decode-uri-component path')
path' (.join path ROOT path')]

(callback #js {:path path'}))))
Expand Down
9 changes: 9 additions & 0 deletions src/electron/electron/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,12 @@
(defn normalize-lc
[s]
(normalize (string/lower-case s)))

(defn safe-decode-uri-component
[uri]
(try
(js/decodeURIComponent uri)
(catch :default e
(println "decodeURIComponent failed: " uri)
(js/console.error e)
uri)))
2 changes: 1 addition & 1 deletion src/electron/electron/window.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
new-win-handler
(fn [e url]
(let [url (if (string/starts-with? url "file:")
(js/decodeURIComponent url) url)
(utils/safe-decode-uri-component url) url)
url (if-not win32? (string/replace url "file://" "") url)]
(logger/info "new-window" url)
(if (some #(string/includes?
Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@
[label]
(when (and (= 1 (count label))
(string? (last (first label))))
(js/decodeURIComponent (last (first label)))))
(gp-util/safe-decode-uri-component (last (first label)))))

(defn- get-page
[label]
Expand Down
17 changes: 11 additions & 6 deletions src/main/frontend/components/file_sync.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
[rum.core :as rum]
[cljs-time.core :as t]
[cljs-time.coerce :as tc]
[goog.functions :refer [debounce]]))
[goog.functions :refer [debounce]]
[logseq.graph-parser.util :as gp-util]))

(declare maybe-onboarding-show)
(declare open-icloud-graph-clone-picker)
Expand Down Expand Up @@ -78,7 +79,7 @@

[:div.folder-tip.flex.flex-col.items-center
[:h3
[:span (ui/icon "folder") [:label.pl-0.5 (js/decodeURIComponent graph-name)]]]
[:span (ui/icon "folder") [:label.pl-0.5 (gp-util/safe-decode-uri-component graph-name)]]]
[:h4.px-6 (config/get-string-repo-dir repo)]

(when (not (string/blank? selected-path))
Expand Down Expand Up @@ -441,7 +442,7 @@

(map (fn [f] {:title [:div.file-item
{:key (str "downloading-" f)}
(js/decodeURIComponent f)]
(gp-util/safe-decode-uri-component f)]
:key (str "downloading-" f)
:icon (if enabled-progress-panel?
(let [progress (get sync-progress f)
Expand All @@ -460,13 +461,17 @@
path (fs-sync/relative-path e)]
{:title [:div.file-item
{:key (str "queue-" path)}
(js/decodeURIComponent path)]
(try
(gp-util/safe-decode-uri-component path)
(catch :default e
(prn "Wrong path: " path)
path))]
:key (str "queue-" path)
:icon (ui/icon icon)})) (take 10 queuing-files))

(map (fn [f] {:title [:div.file-item
{:key (str "uploading-" f)}
(js/decodeURIComponent f)]
(gp-util/safe-decode-uri-component f)]
:key (str "uploading-" f)
:icon (if enabled-progress-panel?
(let [progress (get sync-progress f)
Expand All @@ -489,7 +494,7 @@
(if page-name
(rfe/push-state :page {:name page-name})
(rfe/push-state :file {:path full-path})))}
[:span.file-sync-item (js/decodeURIComponent (:path f))]
[:span.file-sync-item (gp-util/safe-decode-uri-component (:path f))]
[:div.opacity-50 (ui/humanity-time-ago (:time f) nil)]]})))
(take 10 history-files)))))

Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/config.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@
"Local"))
(->> (string/split repo-dir "Documents/")
last
js/decodeURIComponent
gp-util/safe-decode-uri-component
(str "/" (string/capitalize app-name) "/")))
(get-repo-dir repo-dir)))

Expand Down
13 changes: 9 additions & 4 deletions src/main/frontend/fs/sync.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
[frontend.mobile.util :as mobile-util]
[frontend.util :as util]
[frontend.util.persist-var :as persist-var]
[frontend.util.fs :as fs-util]
[frontend.handler.notification :as notification]
[frontend.context.i18n :refer [t]]
[frontend.diff :as diff]
Expand Down Expand Up @@ -791,10 +792,14 @@

(<update-remote-files [this graph-uuid base-path filepaths local-txid]
(go
(<! (<rsapi-cancel-all-requests))
(let [token (<! (<get-token this))]
(<! (<retry-rsapi
#(p->c (ipc/ipc "update-remote-files" graph-uuid base-path filepaths local-txid token)))))))
(let [files-with-reserved-chars (filter fs-util/include-reserved-chars? filepaths)]
(if (seq files-with-reserved-chars)
(state/pub-event! [:ui/notify-files-with-reserved-chars files-with-reserved-chars])
(do
(<! (<rsapi-cancel-all-requests))
(let [token (<! (<get-token this))]
(<! (<retry-rsapi
#(p->c (ipc/ipc "update-remote-files" graph-uuid base-path filepaths local-txid token))))))))))

(<delete-remote-files [this graph-uuid base-path filepaths local-txid]
(go
Expand Down
28 changes: 28 additions & 0 deletions src/main/frontend/handler/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
[frontend.handler.file-sync :as file-sync-handler]
[frontend.components.file-sync :as file-sync]
[frontend.components.encryption :as encryption]
[frontend.components.conversion :as conversion-component]
[goog.dom :as gdom]
[logseq.db.schema :as db-schema]
[promesa.core :as p]
Expand Down Expand Up @@ -705,6 +706,33 @@
(when (= dir (config/get-repo-dir repo))
(fs/watch-dir! dir))))

(defmethod handle :ui/notify-files-with-reserved-chars [[_ paths]]
(sync/<sync-stop)

(notification/show!
[:div
[:div.mb-4
[:div.font-semibold.mb-4.text-xl "It seems that you're using the old filename format."]

[:div
[:p
"We suggest you upgrading now to avoid some potential bugs."]
[:p
"For example, the files below have reserved characters that make them unable to be synced on some platforms."]]
]
(ui/button
"Upgrade filename format"
:on-click (fn []
(state/close-modal!)
(state/set-modal!
(fn [_] (conversion-component/files-breaking-changed))
{:id :filename-format-panel :center? true})))
[:ol.my-2
(for [path paths]
[:li path])]]
:warning
false))

(defmethod handle :file/alter [[_ repo path content]]
(p/let [_ (file-handler/alter-file repo path content {:from-disk? true})]
(ui-handler/re-render-root!)))
Expand Down
5 changes: 3 additions & 2 deletions src/main/frontend/handler/file_sync.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
[frontend.handler.user :as user]
[frontend.fs :as fs]
[cljs-time.coerce :as tc]
[cljs-time.core :as t]))
[cljs-time.core :as t]
[logseq.graph-parser.util :as gp-util]))

(def *beta-unavailable? (volatile! false))

Expand Down Expand Up @@ -150,7 +151,7 @@
(when-let [path (:file/path (db/entity file-id))]
(let [base-path (config/get-repo-dir (state/get-current-repo))
base-path (if (string/starts-with? base-path "file://")
(js/decodeURIComponent base-path)
(gp-util/safe-decode-uri-component base-path)
base-path)
path* (string/replace-first (string/replace-first path base-path "") #"^/" "")]
(go
Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/handler/paste.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
(defn- get-whiteboard-tldr-from-text
[text]
(when-let [matched-text (util/safe-re-find #"<whiteboard-tldr>(.*)</whiteboard-tldr>" text)]
(try-parse-as-json (js/decodeURIComponent (second matched-text)))))
(try-parse-as-json (gp-util/safe-decode-uri-component (second matched-text)))))

(defn- get-whiteboard-shape-refs-text
[text]
Expand Down
8 changes: 4 additions & 4 deletions src/main/frontend/mobile/intent.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@
(-> (string/replace template "{time}" time)
(string/replace "{url}" (or url "")))))

(defn- embed-text-file
"Store external content with url into Logseq repo"
(defn- embed-text-file
"Store external content with url into Logseq repo"
[url title]
(p/let [time (date/get-current-time)
title (some-> (or title (path/basename url))
js/decodeURIComponent
gp-util/safe-decode-uri-component
util/node-path.name
;; make the title more user friendly
gp-util/page-name-sanity)
Expand Down Expand Up @@ -148,7 +148,7 @@

:else
(if (mobile-util/native-ios?)
(js/decodeURIComponent v)
(gp-util/safe-decode-uri-component v)
v))])))

(defn handle-result [result]
Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@
(defn safe-path-join [prefix & paths]
(let [path (apply node-path.join (cons prefix paths))]
(if (and (electron?) (gstring/caseInsensitiveStartsWith path "file://"))
(js/decodeURIComponent (subs path 7))
(gp-util/safe-decode-uri-component (subs path 7))
path))))

(defn trim-safe
Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/util/fs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
(re-pattern (str "[" multiplatform-reserved-chars "]+")))

(defn include-reserved-chars?
"Includes reserved charcters that would broken FS"
"Includes reserved characters that would broken FS"
[s]
(util/safe-re-find reserved-chars-pattern s))

Expand Down

0 comments on commit dd0169b

Please sign in to comment.