mirror of
https://github.com/penpot/penpot.git
synced 2025-05-16 01:06:38 +02:00
✨ Add some type hints and remove legacy code.
This commit is contained in:
parent
2e084cc2a6
commit
784a4f8ecd
11 changed files with 18 additions and 181 deletions
|
@ -27,6 +27,7 @@
|
||||||
com.zaxxer.hikari.HikariConfig
|
com.zaxxer.hikari.HikariConfig
|
||||||
com.zaxxer.hikari.HikariDataSource
|
com.zaxxer.hikari.HikariDataSource
|
||||||
com.zaxxer.hikari.metrics.prometheus.PrometheusMetricsTrackerFactory
|
com.zaxxer.hikari.metrics.prometheus.PrometheusMetricsTrackerFactory
|
||||||
|
java.lang.AutoCloseable
|
||||||
java.sql.Connection
|
java.sql.Connection
|
||||||
java.sql.Savepoint
|
java.sql.Savepoint
|
||||||
org.postgresql.PGConnection
|
org.postgresql.PGConnection
|
||||||
|
@ -59,7 +60,7 @@
|
||||||
(log/debugf "initialize connection pool %s with uri %s" (:name cfg) (:uri cfg))
|
(log/debugf "initialize connection pool %s with uri %s" (:name cfg) (:uri cfg))
|
||||||
(let [pool (create-pool cfg)]
|
(let [pool (create-pool cfg)]
|
||||||
(when (seq migrations)
|
(when (seq migrations)
|
||||||
(with-open [conn (open pool)]
|
(with-open [conn ^AutoCloseable (open pool)]
|
||||||
(mg/setup! conn)
|
(mg/setup! conn)
|
||||||
(doseq [[mname steps] migrations]
|
(doseq [[mname steps] migrations]
|
||||||
(mg/migrate! conn {:name (name mname) :steps steps}))))
|
(mg/migrate! conn {:name (name mname) :steps steps}))))
|
||||||
|
@ -164,7 +165,7 @@
|
||||||
[& args]
|
[& args]
|
||||||
`(jdbc/with-transaction ~@args))
|
`(jdbc/with-transaction ~@args))
|
||||||
|
|
||||||
(defn open
|
(defn ^Connection open
|
||||||
[pool]
|
[pool]
|
||||||
(jdbc/get-connection pool))
|
(jdbc/get-connection pool))
|
||||||
|
|
||||||
|
@ -288,7 +289,7 @@
|
||||||
(pginterval data)
|
(pginterval data)
|
||||||
|
|
||||||
(dt/duration? data)
|
(dt/duration? data)
|
||||||
(->> (/ (.toMillis data) 1000.0)
|
(->> (/ (.toMillis ^java.time.Duration data) 1000.0)
|
||||||
(format "%s seconds")
|
(format "%s seconds")
|
||||||
(pginterval))
|
(pginterval))
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@
|
||||||
(assoc params
|
(assoc params
|
||||||
:format format
|
:format format
|
||||||
:mtype (cm/format->mtype format)
|
:mtype (cm/format->mtype format)
|
||||||
:size (alength thumbnail-data)
|
:size (alength ^bytes thumbnail-data)
|
||||||
:data (ByteArrayInputStream. thumbnail-data)))))
|
:data (ByteArrayInputStream. thumbnail-data)))))
|
||||||
|
|
||||||
(defmulti process :cmd)
|
(defmulti process :cmd)
|
||||||
|
@ -89,7 +89,7 @@
|
||||||
(.addImage)
|
(.addImage)
|
||||||
(.autoOrient)
|
(.autoOrient)
|
||||||
(.strip)
|
(.strip)
|
||||||
(.thumbnail (int width) (int height) ">")
|
(.thumbnail ^Integer (int width) ^Integer (int height) ">")
|
||||||
(.quality (double quality))
|
(.quality (double quality))
|
||||||
(.addImage))]
|
(.addImage))]
|
||||||
(generic-process (assoc params :operation op))))
|
(generic-process (assoc params :operation op))))
|
||||||
|
@ -101,7 +101,7 @@
|
||||||
(.addImage)
|
(.addImage)
|
||||||
(.autoOrient)
|
(.autoOrient)
|
||||||
(.strip)
|
(.strip)
|
||||||
(.thumbnail (int width) (int height) "^")
|
(.thumbnail ^Integer (int width) ^Integer (int height) "^")
|
||||||
(.gravity "center")
|
(.gravity "center")
|
||||||
(.extent (int width) (int height))
|
(.extent (int width) (int height))
|
||||||
(.quality (double quality))
|
(.quality (double quality))
|
||||||
|
|
|
@ -22,7 +22,9 @@
|
||||||
[ring.adapter.jetty9 :as jetty]
|
[ring.adapter.jetty9 :as jetty]
|
||||||
[ring.middleware.cookies :refer [wrap-cookies]]
|
[ring.middleware.cookies :refer [wrap-cookies]]
|
||||||
[ring.middleware.keyword-params :refer [wrap-keyword-params]]
|
[ring.middleware.keyword-params :refer [wrap-keyword-params]]
|
||||||
[ring.middleware.params :refer [wrap-params]]))
|
[ring.middleware.params :refer [wrap-params]])
|
||||||
|
(:import
|
||||||
|
org.eclipse.jetty.websocket.api.WebSocketAdapter))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; Http Handler
|
;; Http Handler
|
||||||
|
@ -187,7 +189,7 @@
|
||||||
(aa/<? (handle-message ws {:type :disconnect}))
|
(aa/<? (handle-message ws {:type :disconnect}))
|
||||||
(catch Throwable err
|
(catch Throwable err
|
||||||
(log/errorf err "Unexpected exception on websocket handler.")
|
(log/errorf err "Unexpected exception on websocket handler.")
|
||||||
(let [session (.getSession conn)]
|
(let [session (.getSession ^WebSocketAdapter conn)]
|
||||||
(when session
|
(when session
|
||||||
(.disconnect session)))))))
|
(.disconnect session)))))))
|
||||||
|
|
||||||
|
|
|
@ -256,12 +256,11 @@
|
||||||
;; --- Helpers
|
;; --- Helpers
|
||||||
|
|
||||||
(defn decode-row
|
(defn decode-row
|
||||||
[{:keys [pages data changes] :as row}]
|
[{:keys [data changes] :as row}]
|
||||||
(when row
|
(when row
|
||||||
(cond-> row
|
(cond-> row
|
||||||
changes (assoc :changes (blob/decode changes))
|
changes (assoc :changes (blob/decode changes))
|
||||||
data (assoc :data (blob/decode data))
|
data (assoc :data (blob/decode data)))))
|
||||||
pages (assoc :pages (vec (.getArray pages))))))
|
|
||||||
|
|
||||||
(def decode-row-xf
|
(def decode-row-xf
|
||||||
(comp (map decode-row)
|
(comp (map decode-row)
|
||||||
|
|
|
@ -41,5 +41,3 @@
|
||||||
(teams/check-read-permissions! conn profile-id team-id)
|
(teams/check-read-permissions! conn profile-id team-id)
|
||||||
(let [files (db/exec! conn [sql:recent-files team-id])]
|
(let [files (db/exec! conn [sql:recent-files team-id])]
|
||||||
(into [] decode-row-xf files))))
|
(into [] decode-row-xf files))))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -308,7 +308,7 @@
|
||||||
(if-let [[groups total] (retrieve-deleted-objects conn)]
|
(if-let [[groups total] (retrieve-deleted-objects conn)]
|
||||||
(do
|
(do
|
||||||
(run! (partial delete-in-bulk conn) groups)
|
(run! (partial delete-in-bulk conn) groups)
|
||||||
(recur (+ n total)))
|
(recur (+ n ^long total)))
|
||||||
(do
|
(do
|
||||||
(log/infof "gc-deleted: processed %s items" n)
|
(log/infof "gc-deleted: processed %s items" n)
|
||||||
{:deleted n})))))))
|
{:deleted n})))))))
|
||||||
|
|
|
@ -165,7 +165,7 @@
|
||||||
(string->content data)
|
(string->content data)
|
||||||
|
|
||||||
(bytes? data)
|
(bytes? data)
|
||||||
(input-stream->content (ByteArrayInputStream. ^bytes data) (alength data))
|
(input-stream->content (ByteArrayInputStream. ^bytes data) (alength ^bytes data))
|
||||||
|
|
||||||
(instance? InputStream data)
|
(instance? InputStream data)
|
||||||
(do
|
(do
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
(defn encode
|
(defn encode
|
||||||
([data] (encode data nil))
|
([data] (encode data nil))
|
||||||
([data {:keys [version] :or {version default-version}}]
|
([data {:keys [version] :or {version default-version}}]
|
||||||
(case version
|
(case (long version)
|
||||||
1 (encode-v1 data)
|
1 (encode-v1 data)
|
||||||
2 (encode-v2 data)
|
2 (encode-v2 data)
|
||||||
(throw (ex-info "unsupported version" {:version version})))))
|
(throw (ex-info "unsupported version" {:version version})))))
|
||||||
|
@ -81,7 +81,7 @@
|
||||||
(defn- encode-v2
|
(defn- encode-v2
|
||||||
[data]
|
[data]
|
||||||
(let [data (n/fast-freeze data)
|
(let [data (n/fast-freeze data)
|
||||||
dlen (alength data)
|
dlen (alength ^bytes data)
|
||||||
mlen (Zstd/compressBound dlen)
|
mlen (Zstd/compressBound dlen)
|
||||||
cdata (byte-array mlen)
|
cdata (byte-array mlen)
|
||||||
clen (Zstd/compressByteArray ^bytes cdata 0 mlen
|
clen (Zstd/compressByteArray ^bytes cdata 0 mlen
|
||||||
|
|
|
@ -161,7 +161,7 @@
|
||||||
(.setDebug session debug)
|
(.setDebug session debug)
|
||||||
session))
|
session))
|
||||||
|
|
||||||
(defn smtp-message
|
(defn ^MimeMessage smtp-message
|
||||||
[cfg message]
|
[cfg message]
|
||||||
(let [^Session session (smtp-session cfg)]
|
(let [^Session session (smtp-session cfg)]
|
||||||
(build-message cfg session message)))
|
(build-message cfg session message)))
|
||||||
|
|
|
@ -1,101 +0,0 @@
|
||||||
;; 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.util.svg
|
|
||||||
"Icons SVG parsing helpers."
|
|
||||||
(:require
|
|
||||||
[app.common.exceptions :as ex]
|
|
||||||
[app.common.spec :as us]
|
|
||||||
[clojure.spec.alpha :as s]
|
|
||||||
[cuerdas.core :as str])
|
|
||||||
(:import
|
|
||||||
org.jsoup.Jsoup
|
|
||||||
org.jsoup.nodes.Attribute
|
|
||||||
org.jsoup.nodes.Element
|
|
||||||
org.jsoup.nodes.Document))
|
|
||||||
|
|
||||||
(s/def ::content string?)
|
|
||||||
(s/def ::width ::us/number)
|
|
||||||
(s/def ::height ::us/number)
|
|
||||||
(s/def ::name string?)
|
|
||||||
(s/def ::view-box (s/coll-of ::us/number :min-count 4 :max-count 4))
|
|
||||||
|
|
||||||
(s/def ::svg-entity
|
|
||||||
(s/keys :req-un [::content ::width ::height ::view-box]
|
|
||||||
:opt-un [::name]))
|
|
||||||
|
|
||||||
;; --- Implementation
|
|
||||||
|
|
||||||
(defn- parse-double
|
|
||||||
[data]
|
|
||||||
(s/assert ::us/string data)
|
|
||||||
(Double/parseDouble data))
|
|
||||||
|
|
||||||
(defn- parse-viewbox
|
|
||||||
[data]
|
|
||||||
(s/assert ::us/string data)
|
|
||||||
(mapv parse-double (str/split data #"\s+")))
|
|
||||||
|
|
||||||
(defn- parse-attrs
|
|
||||||
[^Element element]
|
|
||||||
(persistent!
|
|
||||||
(reduce (fn [acc ^Attribute attr]
|
|
||||||
(let [key (.getKey attr)
|
|
||||||
val (.getValue attr)]
|
|
||||||
(case key
|
|
||||||
"width" (assoc! acc :width (parse-double val))
|
|
||||||
"height" (assoc! acc :height (parse-double val))
|
|
||||||
"viewbox" (assoc! acc :view-box (parse-viewbox val))
|
|
||||||
"sodipodi:docname" (assoc! acc :name val)
|
|
||||||
acc)))
|
|
||||||
(transient {})
|
|
||||||
(.attributes element))))
|
|
||||||
|
|
||||||
(defn- impl-parse
|
|
||||||
[data]
|
|
||||||
(try
|
|
||||||
(let [document (Jsoup/parse ^String data)
|
|
||||||
element (some-> (.body ^Document document)
|
|
||||||
(.getElementsByTag "svg")
|
|
||||||
(first))
|
|
||||||
content (.html element)
|
|
||||||
attrs (parse-attrs element)]
|
|
||||||
(assoc attrs :content content))
|
|
||||||
(catch java.lang.IllegalArgumentException _e
|
|
||||||
(ex/raise :type :validation
|
|
||||||
:code ::invalid-input
|
|
||||||
:message "Input does not seems to be a valid svg."))
|
|
||||||
(catch java.lang.NullPointerException _e
|
|
||||||
(ex/raise :type :validation
|
|
||||||
:code ::invalid-input
|
|
||||||
:message "Input does not seems to be a valid svg."))
|
|
||||||
(catch org.jsoup.UncheckedIOException _e
|
|
||||||
(ex/raise :type :validation
|
|
||||||
:code ::invalid-input
|
|
||||||
:message "Input does not seems to be a valid svg."))
|
|
||||||
(catch Exception _e
|
|
||||||
(ex/raise :type :internal
|
|
||||||
:code ::unexpected))))
|
|
||||||
|
|
||||||
;; --- Public Api
|
|
||||||
|
|
||||||
(defn parse-string
|
|
||||||
"Parse SVG from a string."
|
|
||||||
[data]
|
|
||||||
(s/assert ::us/string data)
|
|
||||||
(let [result (impl-parse data)]
|
|
||||||
(if (s/valid? ::svg-entity result)
|
|
||||||
result
|
|
||||||
(ex/raise :type :validation
|
|
||||||
:code ::invalid-result
|
|
||||||
:message "The result does not conform valid svg entity."))))
|
|
||||||
|
|
||||||
(defn parse
|
|
||||||
[data]
|
|
||||||
(parse-string (slurp data)))
|
|
|
@ -1,62 +0,0 @@
|
||||||
;; 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.tests.test-util-svg
|
|
||||||
(:require
|
|
||||||
[clojure.test :as t]
|
|
||||||
[clojure.java.io :as io]
|
|
||||||
[app.http :as http]
|
|
||||||
[app.util.svg :as svg]
|
|
||||||
[app.tests.helpers :as th]))
|
|
||||||
|
|
||||||
(t/deftest parse-svg-1
|
|
||||||
(let [result (-> (io/resource "app/tests/_files/sample1.svg")
|
|
||||||
(svg/parse))]
|
|
||||||
(t/is (contains? result :width))
|
|
||||||
(t/is (contains? result :height))
|
|
||||||
(t/is (contains? result :view-box))
|
|
||||||
(t/is (contains? result :name))
|
|
||||||
(t/is (contains? result :content))
|
|
||||||
(t/is (= 500.0 (:width result)))
|
|
||||||
(t/is (= 500.0 (:height result)))
|
|
||||||
(t/is (= [0.0 0.0 500.00001 500.00001] (:view-box result)))
|
|
||||||
(t/is (= "lock.svg" (:name result)))))
|
|
||||||
|
|
||||||
(t/deftest parse-svg-2
|
|
||||||
(let [result (-> (io/resource "app/tests/_files/sample2.svg")
|
|
||||||
(svg/parse))]
|
|
||||||
(t/is (contains? result :width))
|
|
||||||
(t/is (contains? result :height))
|
|
||||||
(t/is (contains? result :view-box))
|
|
||||||
(t/is (contains? result :name))
|
|
||||||
(t/is (contains? result :content))
|
|
||||||
(t/is (= 500.0 (:width result)))
|
|
||||||
(t/is (= 500.0 (:height result)))
|
|
||||||
(t/is (= [0.0 0.0 500.0 500.00001] (:view-box result)))
|
|
||||||
(t/is (= "play.svg" (:name result)))))
|
|
||||||
|
|
||||||
(t/deftest parse-invalid-svg-1
|
|
||||||
(let [image (io/resource "app/tests/_files/sample.jpg")
|
|
||||||
out (th/try! (svg/parse image))]
|
|
||||||
|
|
||||||
(let [error (:error out)]
|
|
||||||
(t/is (th/ex-info? error))
|
|
||||||
(t/is (th/ex-of-code? error ::svg/invalid-input)))))
|
|
||||||
|
|
||||||
(t/deftest parse-invalid-svg-2
|
|
||||||
(let [out (th/try! (svg/parse-string ""))]
|
|
||||||
(let [error (:error out)]
|
|
||||||
(t/is (th/ex-info? error))
|
|
||||||
(t/is (th/ex-of-code? error ::svg/invalid-input)))))
|
|
||||||
|
|
||||||
(t/deftest parse-invalid-svg-3
|
|
||||||
(let [out (th/try! (svg/parse-string "<svg></svg>"))]
|
|
||||||
(let [error (:error out)]
|
|
||||||
(t/is (th/ex-info? error))
|
|
||||||
(t/is (th/ex-of-code? error ::svg/invalid-result)))))
|
|
Loading…
Add table
Add a link
Reference in a new issue