diff --git a/backend/src/app/http.clj b/backend/src/app/http.clj index 595508a45b..cb26d0210e 100644 --- a/backend/src/app/http.clj +++ b/backend/src/app/http.clj @@ -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}] diff --git a/backend/src/app/http/handlers.clj b/backend/src/app/http/handlers.clj index 1265a97e10..b99f118a28 100644 --- a/backend/src/app/http/handlers.clj +++ b/backend/src/app/http/handlers.clj @@ -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)}) diff --git a/backend/src/app/services/svgparse.clj b/backend/src/app/services/svgparse.clj new file mode 100644 index 0000000000..733b070d07 --- /dev/null +++ b/backend/src/app/services/svgparse.clj @@ -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))))