fix: notify users to switch to the new filename format

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.
This commit is contained in:
Tienson Qin
2022-11-06 21:17:29 +08:00
parent a8eb7810a9
commit dd0169b068
15 changed files with 84 additions and 28 deletions

View File

@@ -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

View File

@@ -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
@@ -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)

View File

@@ -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'}))))

View File

@@ -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)))

View File

@@ -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?

View File

@@ -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]

View File

@@ -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)
@@ -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))
@@ -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)
@@ -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)
@@ -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)))))

View File

@@ -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)))

View File

@@ -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]
@@ -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

View File

@@ -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]
@@ -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!)))

View File

@@ -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))
@@ -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

View File

@@ -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]

View File

@@ -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)
@@ -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]

View File

@@ -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

View File

@@ -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))