🐛 Fix image upload issues on safari with drag&drop

This commit is contained in:
Andrey Antukh 2023-08-08 12:56:23 +02:00
parent 81facd58c9
commit 486c638076
3 changed files with 16 additions and 14 deletions

View file

@ -171,7 +171,8 @@
:opt-un [::id ::name])) :opt-un [::id ::name]))
(sv/defmethod ::create-file-media-object-from-url (sv/defmethod ::create-file-media-object-from-url
{::doc/added "1.17"} {::doc/added "1.17"
::doc/deprecated "1.19"}
[{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id file-id] :as params}] [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id file-id] :as params}]
(let [cfg (update cfg ::sto/storage media/configure-assets-storage)] (let [cfg (update cfg ::sto/storage media/configure-assets-storage)]
(files/check-edition-permissions! pool profile-id file-id) (files/check-edition-permissions! pool profile-id file-id)

View file

@ -73,16 +73,20 @@
(rx/map #(svg/add-svg-shapes (assoc svg-data :image-data %) position)))))) (rx/map #(svg/add-svg-shapes (assoc svg-data :image-data %) position))))))
(defn- process-uris (defn- process-uris
[{:keys [file-id local? name uris mtype on-image on-svg]}] [{:keys [file-id local? name uris mtype on-image on-svg] }]
(letfn [(svg-url? [url] (letfn [(svg-url? [url]
(or (and mtype (= mtype "image/svg+xml")) (or (and mtype (= mtype "image/svg+xml"))
(str/ends-with? url ".svg"))) (str/ends-with? url ".svg")))
(prepare [uri] (upload [uri]
{:file-id file-id (->> (http/send! {:method :get :uri uri :mode :no-cors :response-type :blob})
:is-local local? (rx/map :body)
:name (or name (svg/extract-name uri)) (rx/map (fn [content]
:url uri}) {:file-id file-id
:name (or name (svg/extract-name uri))
:is-local local?
:content content}))
(rx/mapcat #(rp/cmd! :upload-file-media-object %))))
(fetch-svg [name uri] (fetch-svg [name uri]
(->> (http/send! {:method :get :uri uri :mode :no-cors}) (->> (http/send! {:method :get :uri uri :mode :no-cors})
@ -93,8 +97,7 @@
(rx/merge (rx/merge
(->> (rx/from uris) (->> (rx/from uris)
(rx/filter (comp not svg-url?)) (rx/filter (comp not svg-url?))
(rx/map prepare) (rx/mapcat upload)
(rx/mapcat #(rp/cmd! :create-file-media-object-from-url %))
(rx/do on-image)) (rx/do on-image))
(->> (rx/from uris) (->> (rx/from uris)
@ -142,7 +145,7 @@
[:local? :boolean] [:local? :boolean]
[:name {:optional true} :string] [:name {:optional true} :string]
[:data {:optional true} :any] ; FIXME [:data {:optional true} :any] ; FIXME
[:uris {:optional true} [:vector :string]] [:uris {:optional true} [:sequential :string]]
[:mtype {:optional true} :string]]) [:mtype {:optional true} :string]])
(defn- process-media-objects (defn- process-media-objects
@ -213,8 +216,6 @@
:on-image #(st/emit! (dwl/add-media %)))] :on-image #(st/emit! (dwl/add-media %)))]
(process-media-objects params))) (process-media-objects params)))
;; TODO: it is really need handle SVG here, looks like it already
;; handled separately
(defn upload-media-workspace (defn upload-media-workspace
[{:keys [position file-id] :as params}] [{:keys [position file-id] :as params}]
(let [params (assoc params (let [params (assoc params

View file

@ -451,8 +451,8 @@
(dnd/has-type? event "text/uri-list") (dnd/has-type? event "text/uri-list")
(let [data (dnd/get-data event "text/uri-list") (let [data (dnd/get-data event "text/uri-list")
lines (str/lines data) lines (str/lines data)
uris (->> lines (filter #(str/starts-with? % "http"))) uris (filterv #(str/starts-with? % "http") lines)
data (->> lines (filter #(str/starts-with? % "data:image/"))) data (filterv #(str/starts-with? % "data:image/") lines)
params {:file-id (:id file) params {:file-id (:id file)
:position viewport-coord} :position viewport-coord}
params (if (seq uris) params (if (seq uris)