0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-04-09 13:31:23 -05:00

🎉 Add basic code for svg parsing to clj data structure.

Usage example:

curl -X POST http://localhost:6060/api/svg -H "content-type: image/svg+xml" -d "@example2.svg" |jq
This commit is contained in:
Andrey Antukh 2020-12-12 16:06:25 +01:00 committed by Alonso Torres
parent f84d0f34e6
commit 4f6f4eea4c
3 changed files with 53 additions and 7 deletions
backend/src/app

View file

@ -37,6 +37,8 @@
[middleware/keyword-params]
[middleware/cookies]]}
["/svg" {:post handlers/parse-svg}]
["/oauth"
["/google" {:post google/auth}]
["/google/callback" {:get google/callback}]
@ -46,12 +48,9 @@
["/echo" {:get handlers/echo-handler
:post handlers/echo-handler}]
["/login" {:handler auth/login-handler
:method :post}]
["/logout" {:handler auth/logout-handler
:method :post}]
["/login-ldap" {:handler ldap/auth
:method :post}]
["/login" {:post auth/login-handler}]
["/logout" {:post auth/logout-handler}]
["/login-ldap" {:post ldap/auth}]
["/w" {:middleware [session/middleware]}
["/query/:type" {:get handlers/query-handler}]

View file

@ -15,7 +15,8 @@
[app.http.session :as session]
[app.services.init]
[app.services.mutations :as sm]
[app.services.queries :as sq]))
[app.services.queries :as sq]
[app.services.svgparse :as svgp]))
(def unauthorized-services
#{:create-demo-profile
@ -74,3 +75,12 @@
:cookies (:cookies req)
:headers (:headers req)}})
(defn parse-svg
[{:keys [headers body] :as request}]
(when (not= "image/svg+xml" (get headers "content-type"))
(ex/raise :type :validation
:code :unsupported-mime-type
:mime (get headers "content-type")))
{:status 200
:body (svgp/parse body)})

View file

@ -0,0 +1,37 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; This Source Code Form is "Incompatible With Secondary Licenses", as
;; defined by the Mozilla Public License, v. 2.0.
;;
;; Copyright (c) 2020 UXBOX Labs SL
(ns app.services.svgparse
(:require
[app.common.exceptions :as ex]
[clojure.xml :as xml]
[clojure.java.shell :as shell]
[clojure.java.io :as io])
(:import
java.io.InputStream
org.apache.commons.io.IOUtils))
(defn- string->input-stream
[^String data]
(IOUtils/toInputStream data "UTF-8"))
(defn- clean-svg
[^InputStream input]
(let [result (shell/sh "svgcleaner" "-c" "-" :in input :out-enc :bytes)]
(when (not= 0 (:exit result))
(ex/raise :type :validation
:code :unable-to-optimize
:hint (:err result)))
(io/input-stream (:out result))))
(defn parse
[^InputStream input]
(with-open [istream (io/input-stream input)]
(-> (clean-svg istream)
(xml/parse))))