From 12ac01a9cba97973d05b1c4c17c26d9aea8f7a50 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 11 Apr 2023 12:40:59 +0200 Subject: [PATCH 1/2] :sparkles: Improve audit props for upload-file-media-objects rpc method --- backend/src/app/rpc/commands/media.clj | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/backend/src/app/rpc/commands/media.clj b/backend/src/app/rpc/commands/media.clj index 168a78538..d17e443aa 100644 --- a/backend/src/app/rpc/commands/media.clj +++ b/backend/src/app/rpc/commands/media.clj @@ -14,6 +14,7 @@ [app.config :as cf] [app.db :as db] [app.http.client :as http] + [app.loggers.audit :as-alias audit] [app.media :as media] [app.rpc :as-alias rpc] [app.rpc.climit :as climit] @@ -72,8 +73,15 @@ (files/check-edition-permissions! pool profile-id file-id) (media/validate-media-type! content) (validate-content-size! content) - - (create-file-media-object cfg params))) + (->> (create-file-media-object cfg params) + (p/fmap (fn [object] + (with-meta object + {::audit/replace-props + {:name (:name params) + :file-id file-id + :is-local (:is-local params) + :size (:size content) + :mtype (:mtype content)}})))))) (defn- big-enough-for-thumbnail? "Checks if the provided image info is big enough for From 7ef07385c6fe666e35fdf55cad37b8004eed415f Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 12 Apr 2023 13:15:15 +0200 Subject: [PATCH 2/2] :bug: Fix inconsistencies on drop datauri images (mainly for firefox browser) --- .../main/ui/workspace/viewport/actions.cljs | 24 ++++++++++++------- frontend/src/app/util/webapi.cljs | 17 +++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/frontend/src/app/main/ui/workspace/viewport/actions.cljs b/frontend/src/app/main/ui/workspace/viewport/actions.cljs index a18d24bd3..eef5890df 100644 --- a/frontend/src/app/main/ui/workspace/viewport/actions.cljs +++ b/frontend/src/app/main/ui/workspace/viewport/actions.cljs @@ -26,6 +26,7 @@ [app.util.keyboard :as kbd] [app.util.object :as obj] [app.util.timers :as timers] + [app.util.webapi :as wapi] [beicon.core :as rx] [cuerdas.core :as str] [rumext.v2 :as mf])) @@ -434,7 +435,9 @@ (:id component) (gpt/point final-x final-y)))) - ;; Will trigger when the user drags an image from a browser to the viewport (firefox and chrome do it a bit different depending on the origin) + ;; Will trigger when the user drags an image from a browser + ;; to the viewport (firefox and chrome do it a bit different + ;; depending on the origin) (dnd/has-type? event "Files") (let [files (dnd/get-files event) params {:file-id (:id file) @@ -442,16 +445,19 @@ :blobs (seq files)}] (st/emit! (dwm/upload-media-workspace params))) - ;; Will trigger when the user drags an image from a browser to the viewport (firefox and chrome do it a bit different depending on the origin) + ;; Will trigger when the user drags an image (usually rendered as datauri) from a + ;; browser to the viewport (mainly on firefox, all depending on the origin of the + ;; drag event). (dnd/has-type? event "text/uri-list") - (let [data (dnd/get-data event "text/uri-list") - lines (str/lines data) - uris (filter #(and (not (str/blank? %)) - (not (str/starts-with? % "#"))) - lines) + (let [data (dnd/get-data event "text/uri-list") + lines (str/lines data) + uris (->> lines (filter #(str/starts-with? % "http"))) + data (->> lines (filter #(str/starts-with? % "data:image/"))) params {:file-id (:id file) - :position viewport-coord - :uris uris}] + :position viewport-coord} + params (if (seq uris) + (assoc params :uris uris) + (assoc params :blobs (map wapi/data-uri->blob data)))] (st/emit! (dwm/upload-media-workspace params))) ;; Will trigger when the user drags an SVG asset from the assets panel diff --git a/frontend/src/app/util/webapi.cljs b/frontend/src/app/util/webapi.cljs index 1f3a9397d..49f46f0bc 100644 --- a/frontend/src/app/util/webapi.cljs +++ b/frontend/src/app/util/webapi.cljs @@ -60,6 +60,23 @@ (assert (blob? b) "invalid arguments") (js/URL.createObjectURL b)) +(defn data-uri? + [s] + (str/starts-with? s "data:")) + +(defn data-uri->blob + [data-uri] + (let [[mtype b64-data] (str/split data-uri ";base64,") + mtype (subs mtype (inc (str/index-of mtype ":"))) + decoded (.atob js/window b64-data) + size (.-length ^js decoded) + content (js/Uint8Array. size)] + + (doseq [i (range 0 size)] + (aset content i (.charCodeAt ^js decoded i))) + + (create-blob content mtype))) + (defn write-to-clipboard [data] (assert (string? data) "`data` should be string")