From ad2d8c8ee0154fb6e70b17625cd8140c1bdcd5bf Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 13 Jan 2021 11:40:33 +0100 Subject: [PATCH] :sparkles: More improvements on media uploading. --- backend/src/app/main.clj | 3 ++- backend/src/app/media.clj | 5 +++-- backend/src/app/rpc/mutations/media.clj | 27 ++++++++++++++++++++----- vendor/svgclean/main.js | 5 +++-- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/backend/src/app/main.clj b/backend/src/app/main.clj index 919796fe3..ef17baac3 100644 --- a/backend/src/app/main.clj +++ b/backend/src/app/main.clj @@ -99,7 +99,8 @@ :tokens (ig/ref :app.tokens/tokens) :metrics (ig/ref :app.metrics/metrics) :storage (ig/ref :app.storage/storage) - :redis (ig/ref :app.redis/redis)} + :redis (ig/ref :app.redis/redis) + :svgc (ig/ref :app.svgparse/svgc)} :app.notifications/handler {:redis (ig/ref :app.redis/redis) diff --git a/backend/src/app/media.clj b/backend/src/app/media.clj index a431757c1..fa279f652 100644 --- a/backend/src/app/media.clj +++ b/backend/src/app/media.clj @@ -120,10 +120,11 @@ (ex/raise :type :validation :code :unable-to-parse-svg :hint "uploaded svg has invalid content")) - (reduce (fn [_ f] + (reduce (fn [default f] (if-let [res (f attrs)] (reduced res) - nil)) + default)) + {:width 100 :height 100} [(fn parse-width-and-height [{:keys [width height]}] (when (and (string? width) diff --git a/backend/src/app/rpc/mutations/media.clj b/backend/src/app/rpc/mutations/media.clj index 343a52375..147146491 100644 --- a/backend/src/app/rpc/mutations/media.clj +++ b/backend/src/app/rpc/mutations/media.clj @@ -56,8 +56,19 @@ (-> (assoc cfg :conn conn) (create-file-media-object params))))) +(defn- big-enough-for-thumbnail? + "Checks if the provided image info is big enough for + create a separate thumbnail storage object." + [info] + (or (> (:width info) (:width thumbnail-options)) + (> (:height info) (:height thumbnail-options)))) + +(defn- svg-image? + [info] + (= (:mtype info) "image/svg+xml")) + (defn create-file-media-object - [{:keys [conn storage] :as cfg} {:keys [id file-id is-local name content] :as params}] + [{:keys [conn storage svgc] :as cfg} {:keys [id file-id is-local name content] :as params}] (media/validate-media-type (:content-type content)) (let [storage (assoc storage :conn conn) source-path (fs/path (:tempfile content)) @@ -65,13 +76,19 @@ source-info (media/run {:cmd :info :input {:path source-path :mtype source-mtype}}) - thumb (when (not= (:mtype source-info) "image/svg+xml") + thumb (when (and (not (svg-image? source-info)) + (big-enough-for-thumbnail? source-info)) (media/run (assoc thumbnail-options :cmd :generic-thumbnail - :input {:mtype (:mtype source-info) :path source-path}))) + :input {:mtype (:mtype source-info) + :path source-path}))) - image (sto/put-object storage {:content (sto/content source-path) - :content-type (:mtype source-info)}) + image (if (= (:mtype source-info) "image/svg+xml") + (let [data (svgc (slurp source-path))] + (sto/put-object storage {:content (sto/content data) + :content-type (:mtype source-info)})) + (sto/put-object storage {:content (sto/content source-path) + :content-type (:mtype source-info)})) thumb (when thumb (sto/put-object storage {:content (sto/content (:data thumb) (:size thumb)) diff --git a/vendor/svgclean/main.js b/vendor/svgclean/main.js index 041ecd09d..2fbcc8edc 100644 --- a/vendor/svgclean/main.js +++ b/vendor/svgclean/main.js @@ -1,6 +1,6 @@ const plugins = [ {removeDimensions: true}, - {removeXMLNS: true}, + // {removeXMLNS: false}, {removeScriptElement: true}, {removeViewBox: false}, {moveElemsAttrsToGroup: false}, @@ -14,9 +14,10 @@ const plugins = [ ]; const svgc = require("./src/svgclean.js"); -const inst = svgc.configure({plugins, multipass: undefined}); +const inst = svgc.configure({plugins}); exports.optimize = function(data) { return svgc.optimize(inst, data) .then((result) => result.data); }; +