mirror of
https://github.com/penpot/penpot.git
synced 2025-05-13 17:07:12 +02:00
🐛 Fix svg assets uploading.
This commit is contained in:
parent
f06264ea0a
commit
678fe3d63e
4 changed files with 57 additions and 16 deletions
|
@ -91,4 +91,4 @@
|
||||||
[{:keys [pool] :as storage} request]
|
[{:keys [pool] :as storage} request]
|
||||||
(let [id (get-in request [:path-params :id])
|
(let [id (get-in request [:path-params :id])
|
||||||
mobj (get-file-media-object pool id)]
|
mobj (get-file-media-object pool id)]
|
||||||
(generic-handler storage request (:thumbnail-id mobj))))
|
(generic-handler storage request (or (:thumbnail-id mobj) (:media-id mobj)))))
|
||||||
|
|
|
@ -12,12 +12,15 @@
|
||||||
(:require
|
(:require
|
||||||
[app.common.exceptions :as ex]
|
[app.common.exceptions :as ex]
|
||||||
[app.common.media :as cm]
|
[app.common.media :as cm]
|
||||||
|
[app.common.data :as d]
|
||||||
[app.common.spec :as us]
|
[app.common.spec :as us]
|
||||||
[app.config :as cfg]
|
[app.config :as cfg]
|
||||||
[app.util.http :as http]
|
[app.util.http :as http]
|
||||||
|
[app.svgparse :as svg]
|
||||||
[clojure.core.async :as a]
|
[clojure.core.async :as a]
|
||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io]
|
||||||
[clojure.spec.alpha :as s]
|
[clojure.spec.alpha :as s]
|
||||||
|
[cuerdas.core :as str]
|
||||||
[datoteka.core :as fs])
|
[datoteka.core :as fs])
|
||||||
(:import
|
(:import
|
||||||
java.io.ByteArrayInputStream
|
java.io.ByteArrayInputStream
|
||||||
|
@ -111,24 +114,57 @@
|
||||||
(.addImage))]
|
(.addImage))]
|
||||||
(generic-process (assoc params :operation op))))
|
(generic-process (assoc params :operation op))))
|
||||||
|
|
||||||
|
(defn get-basic-info-from-svg
|
||||||
|
[{:keys [tag attrs] :as data}]
|
||||||
|
(when (not= tag :svg)
|
||||||
|
(ex/raise :type :validation
|
||||||
|
:code :unable-to-parse-svg
|
||||||
|
:hint "uploaded svg has invalid content"))
|
||||||
|
(reduce (fn [_ f]
|
||||||
|
(if-let [res (f attrs)]
|
||||||
|
(reduced res)
|
||||||
|
nil))
|
||||||
|
[(fn parse-width-and-height
|
||||||
|
[{:keys [width height]}]
|
||||||
|
(when (and (string? width)
|
||||||
|
(string? height))
|
||||||
|
(let [width (d/parse-double width)
|
||||||
|
height (d/parse-double height)]
|
||||||
|
(when (and width height)
|
||||||
|
{:width (int width)
|
||||||
|
:height (int height)}))))
|
||||||
|
(fn parse-viewbox
|
||||||
|
[{:keys [viewBox]}]
|
||||||
|
(let [[x y width height] (->> (str/split viewBox #"\s+" 4)
|
||||||
|
(map d/parse-double))]
|
||||||
|
(when (and x y width height)
|
||||||
|
{:width (int width)
|
||||||
|
:height (int height)})))]))
|
||||||
|
|
||||||
(defmethod process :info
|
(defmethod process :info
|
||||||
[{:keys [input] :as params}]
|
[{:keys [input] :as params}]
|
||||||
(us/assert ::input input)
|
(us/assert ::input input)
|
||||||
(let [{:keys [path mtype]} input]
|
(let [{:keys [path mtype]} input]
|
||||||
(let [instance (Info. (str path))
|
(if (= mtype "image/svg+xml")
|
||||||
|
(let [data (svg/parse (slurp path))
|
||||||
|
info (get-basic-info-from-svg data)]
|
||||||
|
(when-not info
|
||||||
|
(ex/raise :type :validation
|
||||||
|
:code :unable-to-retrieve-dimensions
|
||||||
|
:hint "uploaded svg does not provides dimensions"))
|
||||||
|
(assoc info :mtype mtype))
|
||||||
|
|
||||||
;; SVG files are converted to PNG to extract their properties
|
(let [instance (Info. (str path))
|
||||||
mtype (if (= mtype "image/svg+xml") "image/png" mtype)
|
mtype' (.getProperty instance "Mime type")]
|
||||||
mtype' (.getProperty instance "Mime type")]
|
(when (and (string? mtype)
|
||||||
(when (and (string? mtype)
|
(not= mtype mtype'))
|
||||||
(not= mtype mtype'))
|
(ex/raise :type :validation
|
||||||
(ex/raise :type :validation
|
:code :media-type-mismatch
|
||||||
:code :media-type-mismatch
|
:hint (str "Seems like you are uploading a file whose content does not match the extension."
|
||||||
:hint (str "Seems like you are uploading a file whose content does not match the extension."
|
"Expected: " mtype ". Got: " mtype')))
|
||||||
"Expected: " mtype ". Got: " mtype')))
|
{:width (.getImageWidth instance)
|
||||||
{:width (.getImageWidth instance)
|
:height (.getImageHeight instance)
|
||||||
:height (.getImageHeight instance)
|
:mtype mtype}))))
|
||||||
:mtype mtype'})))
|
|
||||||
|
|
||||||
(defmethod process :default
|
(defmethod process :default
|
||||||
[{:keys [cmd] :as params}]
|
[{:keys [cmd] :as params}]
|
||||||
|
|
|
@ -64,6 +64,7 @@
|
||||||
source-mtype (:content-type content)
|
source-mtype (:content-type content)
|
||||||
|
|
||||||
source-info (media/run {:cmd :info :input {:path source-path :mtype source-mtype}})
|
source-info (media/run {:cmd :info :input {:path source-path :mtype source-mtype}})
|
||||||
|
|
||||||
thumb (when (not= (:mtype source-info) "image/svg+xml")
|
thumb (when (not= (:mtype source-info) "image/svg+xml")
|
||||||
(media/run (assoc thumbnail-options
|
(media/run (assoc thumbnail-options
|
||||||
:cmd :generic-thumbnail
|
:cmd :generic-thumbnail
|
||||||
|
|
|
@ -116,9 +116,13 @@
|
||||||
{:status 200
|
{:status 200
|
||||||
:body (process-request cfg body)})
|
:body (process-request cfg body)})
|
||||||
|
|
||||||
|
(defn parse
|
||||||
|
[data]
|
||||||
|
(with-open [istream (IOUtils/toInputStream data "UTF-8")]
|
||||||
|
(xml/parse istream)))
|
||||||
|
|
||||||
(defn process-request
|
(defn process-request
|
||||||
[{:keys [svgc] :as cfg} body]
|
[{:keys [svgc] :as cfg} body]
|
||||||
(let [data (slurp body)
|
(let [data (slurp body)
|
||||||
data (svgc data)]
|
data (svgc data)]
|
||||||
(with-open [istream (IOUtils/toInputStream data "UTF-8")]
|
(parse data)))
|
||||||
(xml/parse istream))))
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue