mirror of
https://github.com/penpot/penpot.git
synced 2025-05-11 12:46:39 +02:00
♻️ Move some more duplicated code to common module
This commit is contained in:
parent
62e2c1cc14
commit
91899be423
8 changed files with 91 additions and 70 deletions
|
@ -17,6 +17,7 @@
|
||||||
[mount.core :refer [defstate]]
|
[mount.core :refer [defstate]]
|
||||||
[uxbox.config :as cfg]
|
[uxbox.config :as cfg]
|
||||||
[uxbox.common.data :as d]
|
[uxbox.common.data :as d]
|
||||||
|
[uxbox.common.media :as cm]
|
||||||
[uxbox.common.exceptions :as ex]
|
[uxbox.common.exceptions :as ex]
|
||||||
[uxbox.common.spec :as us]
|
[uxbox.common.spec :as us]
|
||||||
[uxbox.media-storage :as mst]
|
[uxbox.media-storage :as mst]
|
||||||
|
@ -40,15 +41,14 @@
|
||||||
(s/def ::path (s/or :path fs/path?
|
(s/def ::path (s/or :path fs/path?
|
||||||
:string string?
|
:string string?
|
||||||
:file fs/file?))
|
:file fs/file?))
|
||||||
(s/def ::mtype string?)
|
|
||||||
|
|
||||||
(s/def ::input
|
(s/def ::input
|
||||||
(s/keys :req-un [::path]
|
(s/keys :req-un [::path]
|
||||||
:opt-un [::mtype]))
|
:opt-un [::cm/mtype]))
|
||||||
|
|
||||||
(s/def ::width integer?)
|
(s/def ::width integer?)
|
||||||
(s/def ::height integer?)
|
(s/def ::height integer?)
|
||||||
(s/def ::format #{:jpeg :webp :png :svg})
|
(s/def ::format #{:jpeg :webp :png})
|
||||||
(s/def ::quality #(< 0 % 101))
|
(s/def ::quality #(< 0 % 101))
|
||||||
|
|
||||||
(s/def ::thumbnail-params
|
(s/def ::thumbnail-params
|
||||||
|
@ -57,39 +57,11 @@
|
||||||
;; Related info on how thumbnails generation
|
;; Related info on how thumbnails generation
|
||||||
;; http://www.imagemagick.org/Usage/thumbnails/
|
;; http://www.imagemagick.org/Usage/thumbnails/
|
||||||
|
|
||||||
(def valid-media-types
|
|
||||||
#{"image/jpeg", "image/png", "image/webp", "image/svg+xml"})
|
|
||||||
|
|
||||||
(defn format->extension
|
|
||||||
[format]
|
|
||||||
(case format
|
|
||||||
:png ".png"
|
|
||||||
:jpeg ".jpg"
|
|
||||||
:webp ".webp"
|
|
||||||
:svg ".svg"))
|
|
||||||
|
|
||||||
(defn format->mtype
|
|
||||||
[format]
|
|
||||||
(case format
|
|
||||||
:png "image/png"
|
|
||||||
:jpeg "image/jpeg"
|
|
||||||
:webp "image/webp"
|
|
||||||
:svg "image/svg+xml"))
|
|
||||||
|
|
||||||
(defn mtype->format
|
|
||||||
[mtype]
|
|
||||||
(case mtype
|
|
||||||
"image/png" :png
|
|
||||||
"image/jpeg" :jpeg
|
|
||||||
"image/webp" :webp
|
|
||||||
"image/svg+xml" :svg
|
|
||||||
nil))
|
|
||||||
|
|
||||||
(defn- generic-process
|
(defn- generic-process
|
||||||
[{:keys [input format quality operation] :as params}]
|
[{:keys [input format quality operation] :as params}]
|
||||||
(let [{:keys [path mtype]} input
|
(let [{:keys [path mtype]} input
|
||||||
format (or (mtype->format mtype) format)
|
format (or (cm/mtype->format mtype) format)
|
||||||
ext (format->extension format)
|
ext (cm/format->extension format)
|
||||||
tmp (fs/create-tempfile :suffix ext)]
|
tmp (fs/create-tempfile :suffix ext)]
|
||||||
|
|
||||||
(doto (ConvertCmd.)
|
(doto (ConvertCmd.)
|
||||||
|
@ -99,7 +71,7 @@
|
||||||
(fs/delete tmp)
|
(fs/delete tmp)
|
||||||
(assoc params
|
(assoc params
|
||||||
:format format
|
:format format
|
||||||
:mtype (format->mtype format)
|
:mtype (cm/format->mtype format)
|
||||||
:data (ByteArrayInputStream. thumbnail-data)))))
|
:data (ByteArrayInputStream. thumbnail-data)))))
|
||||||
|
|
||||||
(defmulti process :cmd)
|
(defmulti process :cmd)
|
||||||
|
@ -170,6 +142,9 @@
|
||||||
(finally
|
(finally
|
||||||
(.release semaphore))))
|
(.release semaphore))))
|
||||||
|
|
||||||
|
|
||||||
|
;; --- Utility functions
|
||||||
|
|
||||||
(defn resolve-urls
|
(defn resolve-urls
|
||||||
[row src dst]
|
[row src dst]
|
||||||
(s/assert map? row)
|
(s/assert map? row)
|
||||||
|
@ -201,7 +176,7 @@
|
||||||
|
|
||||||
(defn validate-media-type
|
(defn validate-media-type
|
||||||
[media-type]
|
[media-type]
|
||||||
(when-not (valid-media-types media-type)
|
(when-not (cm/valid-media-types media-type)
|
||||||
(ex/raise :type :validation
|
(ex/raise :type :validation
|
||||||
:code :media-type-not-allowed
|
:code :media-type-not-allowed
|
||||||
:hint "Seems like you are uploading an invalid media object")))
|
:hint "Seems like you are uploading an invalid media object")))
|
||||||
|
@ -211,14 +186,14 @@
|
||||||
(let [result (http/get! url {:as :byte-array})
|
(let [result (http/get! url {:as :byte-array})
|
||||||
data (:body result)
|
data (:body result)
|
||||||
content-type (get (:headers result) "content-type")
|
content-type (get (:headers result) "content-type")
|
||||||
format (mtype->format content-type)]
|
format (cm/mtype->format content-type)]
|
||||||
(if (nil? format)
|
(if (nil? format)
|
||||||
(ex/raise :type :validation
|
(ex/raise :type :validation
|
||||||
:code :media-type-not-allowed
|
:code :media-type-not-allowed
|
||||||
:hint "Seems like the url points to an invalid media object.")
|
:hint "Seems like the url points to an invalid media object.")
|
||||||
(let [tempfile (fs/create-tempfile)
|
(let [tempfile (fs/create-tempfile)
|
||||||
base-filename (first (fs/split-ext (fs/name tempfile)))
|
base-filename (first (fs/split-ext (fs/name tempfile)))
|
||||||
filename (str base-filename (format->extension format))]
|
filename (str base-filename (cm/format->extension format))]
|
||||||
(with-open [ostream (io/output-stream tempfile)]
|
(with-open [ostream (io/output-stream tempfile)]
|
||||||
(.write ostream data))
|
(.write ostream data))
|
||||||
{:filename filename
|
{:filename filename
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
(:require
|
(:require
|
||||||
[clojure.spec.alpha :as s]
|
[clojure.spec.alpha :as s]
|
||||||
[datoteka.core :as fs]
|
[datoteka.core :as fs]
|
||||||
|
[uxbox.common.media :as cm]
|
||||||
[uxbox.common.exceptions :as ex]
|
[uxbox.common.exceptions :as ex]
|
||||||
[uxbox.common.spec :as us]
|
[uxbox.common.spec :as us]
|
||||||
[uxbox.common.uuid :as uuid]
|
[uxbox.common.uuid :as uuid]
|
||||||
|
@ -47,7 +48,7 @@
|
||||||
|
|
||||||
(s/def :uxbox$upload/filename ::us/string)
|
(s/def :uxbox$upload/filename ::us/string)
|
||||||
(s/def :uxbox$upload/size ::us/integer)
|
(s/def :uxbox$upload/size ::us/integer)
|
||||||
(s/def :uxbox$upload/content-type media/valid-media-types)
|
(s/def :uxbox$upload/content-type cm/valid-media-types)
|
||||||
(s/def :uxbox$upload/tempfile any?)
|
(s/def :uxbox$upload/tempfile any?)
|
||||||
|
|
||||||
(s/def ::upload
|
(s/def ::upload
|
||||||
|
@ -154,7 +155,7 @@
|
||||||
|
|
||||||
name (str "thumbnail-"
|
name (str "thumbnail-"
|
||||||
(first (fs/split-ext (fs/name (:path input))))
|
(first (fs/split-ext (fs/name (:path input))))
|
||||||
(media/format->extension (:format thumb)))
|
(cm/format->extension (:format thumb)))
|
||||||
path (ust/save! mst/media-storage name (:data thumb))]
|
path (ust/save! mst/media-storage name (:data thumb))]
|
||||||
|
|
||||||
(-> thumb
|
(-> thumb
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
[uxbox.common.exceptions :as ex]
|
[uxbox.common.exceptions :as ex]
|
||||||
[uxbox.common.spec :as us]
|
[uxbox.common.spec :as us]
|
||||||
[uxbox.common.uuid :as uuid]
|
[uxbox.common.uuid :as uuid]
|
||||||
|
[uxbox.common.media :as cm]
|
||||||
[uxbox.config :as cfg]
|
[uxbox.config :as cfg]
|
||||||
[uxbox.db :as db]
|
[uxbox.db :as db]
|
||||||
[uxbox.emails :as emails]
|
[uxbox.emails :as emails]
|
||||||
|
@ -299,7 +300,7 @@
|
||||||
:height 256
|
:height 256
|
||||||
:input {:path (fs/path (:tempfile file))
|
:input {:path (fs/path (:tempfile file))
|
||||||
:mtype (:content-type file)}})
|
:mtype (:content-type file)}})
|
||||||
name (str prefix (media/format->extension (:format thumb)))]
|
name (str prefix (cm/format->extension (:format thumb)))]
|
||||||
(ust/save! mst/media-storage name (:data thumb))))
|
(ust/save! mst/media-storage name (:data thumb))))
|
||||||
|
|
||||||
(defn- update-profile-photo
|
(defn- update-profile-photo
|
||||||
|
|
65
common/uxbox/common/media.cljc
Normal file
65
common/uxbox/common/media.cljc
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
;; 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 uxbox.common.media
|
||||||
|
(:require
|
||||||
|
[clojure.spec.alpha :as s]
|
||||||
|
[cuerdas.core :as str]))
|
||||||
|
|
||||||
|
(def valid-media-types
|
||||||
|
#{"image/jpeg", "image/png", "image/webp", "image/svg+xml"})
|
||||||
|
|
||||||
|
(def str-media-types (str/join "," valid-media-types))
|
||||||
|
|
||||||
|
(defn format->extension
|
||||||
|
[format]
|
||||||
|
(case format
|
||||||
|
:png ".png"
|
||||||
|
:jpeg ".jpg"
|
||||||
|
:webp ".webp"
|
||||||
|
:svg ".svg"))
|
||||||
|
|
||||||
|
(defn format->mtype
|
||||||
|
[format]
|
||||||
|
(case format
|
||||||
|
:png "image/png"
|
||||||
|
:jpeg "image/jpeg"
|
||||||
|
:webp "image/webp"
|
||||||
|
:svg "image/svg+xml"))
|
||||||
|
|
||||||
|
(defn mtype->format
|
||||||
|
[mtype]
|
||||||
|
(case mtype
|
||||||
|
"image/png" :png
|
||||||
|
"image/jpeg" :jpeg
|
||||||
|
"image/webp" :webp
|
||||||
|
"image/svg+xml" :svg
|
||||||
|
nil))
|
||||||
|
|
||||||
|
(def max-file-size (* 5 1024 1024))
|
||||||
|
|
||||||
|
(s/def ::id uuid?)
|
||||||
|
(s/def ::name string?)
|
||||||
|
(s/def ::width number?)
|
||||||
|
(s/def ::height number?)
|
||||||
|
(s/def ::created-at inst?)
|
||||||
|
(s/def ::modified-at inst?)
|
||||||
|
(s/def ::mtype string?)
|
||||||
|
(s/def ::uri string?)
|
||||||
|
|
||||||
|
(s/def ::media-object
|
||||||
|
(s/keys :req-un [::id
|
||||||
|
::name
|
||||||
|
::width
|
||||||
|
::height
|
||||||
|
::mtype
|
||||||
|
::created-at
|
||||||
|
::modified-at
|
||||||
|
::uri]))
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
[potok.core :as ptk]
|
[potok.core :as ptk]
|
||||||
[uxbox.common.spec :as us]
|
[uxbox.common.spec :as us]
|
||||||
[uxbox.common.data :as d]
|
[uxbox.common.data :as d]
|
||||||
|
[uxbox.common.media :as cm]
|
||||||
[uxbox.main.data.messages :as dm]
|
[uxbox.main.data.messages :as dm]
|
||||||
[uxbox.main.store :as st]
|
[uxbox.main.store :as st]
|
||||||
[uxbox.main.repo :as rp]
|
[uxbox.main.repo :as rp]
|
||||||
|
@ -24,40 +25,17 @@
|
||||||
|
|
||||||
;; --- Specs
|
;; --- Specs
|
||||||
|
|
||||||
(s/def ::id uuid?)
|
|
||||||
(s/def ::name string?)
|
|
||||||
(s/def ::width number?)
|
|
||||||
(s/def ::height number?)
|
|
||||||
(s/def ::created-at inst?)
|
|
||||||
(s/def ::modified-at inst?)
|
|
||||||
(s/def ::mtype string?)
|
|
||||||
(s/def ::uri string?)
|
|
||||||
|
|
||||||
(s/def ::media-object
|
|
||||||
(s/keys :req-un [::id
|
|
||||||
::name
|
|
||||||
::width
|
|
||||||
::height
|
|
||||||
::mtype
|
|
||||||
::created-at
|
|
||||||
::modified-at
|
|
||||||
::uri]))
|
|
||||||
|
|
||||||
(s/def ::js-file #(instance? js/Blob %))
|
(s/def ::js-file #(instance? js/Blob %))
|
||||||
(s/def ::js-files (s/coll-of ::js-file))
|
(s/def ::js-files (s/coll-of ::js-file))
|
||||||
|
|
||||||
(def allowed-media-types #{"image/jpeg" "image/png" "image/webp" "image/svg+xml"})
|
|
||||||
(def str-media-types (str/join "," allowed-media-types))
|
|
||||||
(def max-file-size (* 5 1024 1024))
|
|
||||||
|
|
||||||
;; --- Utility functions
|
;; --- Utility functions
|
||||||
|
|
||||||
(defn validate-file
|
(defn validate-file
|
||||||
;; Check that a file obtained with the file javascript API is valid.
|
;; Check that a file obtained with the file javascript API is valid.
|
||||||
[file]
|
[file]
|
||||||
(when (> (.-size file) max-file-size)
|
(when (> (.-size file) cm/max-file-size)
|
||||||
(throw (ex-info (tr "errors.media-too-large") {})))
|
(throw (ex-info (tr "errors.media-too-large") {})))
|
||||||
(when-not (contains? allowed-media-types (.-type file))
|
(when-not (contains? cm/valid-media-types (.-type file))
|
||||||
(throw (ex-info (tr "errors.media-format-unsupported") {})))
|
(throw (ex-info (tr "errors.media-format-unsupported") {})))
|
||||||
file)
|
file)
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
[cljs.spec.alpha :as s]
|
[cljs.spec.alpha :as s]
|
||||||
[potok.core :as ptk]
|
[potok.core :as ptk]
|
||||||
[uxbox.common.data :as d]
|
[uxbox.common.data :as d]
|
||||||
|
[uxbox.common.media :as cm]
|
||||||
[uxbox.common.geom.point :as gpt]
|
[uxbox.common.geom.point :as gpt]
|
||||||
[uxbox.common.pages :as cp]
|
[uxbox.common.pages :as cp]
|
||||||
[uxbox.common.spec :as us]
|
[uxbox.common.spec :as us]
|
||||||
|
@ -409,7 +410,7 @@
|
||||||
[file-id is-local media-object]
|
[file-id is-local media-object]
|
||||||
(us/verify ::us/uuid file-id)
|
(us/verify ::us/uuid file-id)
|
||||||
(us/verify ::us/boolean is-local)
|
(us/verify ::us/boolean is-local)
|
||||||
(us/verify ::di/media-object media-object)
|
(us/verify ::cm/media-object media-object)
|
||||||
(ptk/reify ::upload-media-objects-result
|
(ptk/reify ::upload-media-objects-result
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
|
|
|
@ -11,8 +11,8 @@
|
||||||
(ns uxbox.main.ui.workspace.left-toolbar
|
(ns uxbox.main.ui.workspace.left-toolbar
|
||||||
(:require
|
(:require
|
||||||
[rumext.alpha :as mf]
|
[rumext.alpha :as mf]
|
||||||
|
[uxbox.common.media :as cm]
|
||||||
[uxbox.main.refs :as refs]
|
[uxbox.main.refs :as refs]
|
||||||
[uxbox.main.data.media :as di]
|
|
||||||
[uxbox.main.data.workspace :as dw]
|
[uxbox.main.data.workspace :as dw]
|
||||||
[uxbox.main.store :as st]
|
[uxbox.main.store :as st]
|
||||||
[uxbox.main.ui.components.file-uploader :refer [file-uploader]]
|
[uxbox.main.ui.components.file-uploader :refer [file-uploader]]
|
||||||
|
@ -74,7 +74,7 @@
|
||||||
:on-click on-image}
|
:on-click on-image}
|
||||||
[:*
|
[:*
|
||||||
i/image
|
i/image
|
||||||
[:& file-uploader {:accept di/str-media-types
|
[:& file-uploader {:accept cm/str-media-types
|
||||||
:multi true
|
:multi true
|
||||||
:input-ref file-input
|
:input-ref file-input
|
||||||
:on-selected on-files-selected}]]]
|
:on-selected on-files-selected}]]]
|
||||||
|
|
|
@ -13,11 +13,11 @@
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]
|
||||||
[rumext.alpha :as mf]
|
[rumext.alpha :as mf]
|
||||||
[uxbox.common.data :as d]
|
[uxbox.common.data :as d]
|
||||||
|
[uxbox.common.media :as cm]
|
||||||
[uxbox.common.pages :as cp]
|
[uxbox.common.pages :as cp]
|
||||||
[uxbox.common.geom.shapes :as geom]
|
[uxbox.common.geom.shapes :as geom]
|
||||||
[uxbox.common.geom.point :as gpt]
|
[uxbox.common.geom.point :as gpt]
|
||||||
[uxbox.main.ui.icons :as i]
|
[uxbox.main.ui.icons :as i]
|
||||||
[uxbox.main.data.media :as di]
|
|
||||||
[uxbox.main.data.workspace :as dw]
|
[uxbox.main.data.workspace :as dw]
|
||||||
[uxbox.main.data.colors :as dcol]
|
[uxbox.main.data.colors :as dcol]
|
||||||
[uxbox.main.refs :as refs]
|
[uxbox.main.refs :as refs]
|
||||||
|
@ -114,7 +114,7 @@
|
||||||
[:span (str "\u00A0(") (count media-objects) ")"] ;; Unicode 00A0 is non-breaking space
|
[:span (str "\u00A0(") (count media-objects) ")"] ;; Unicode 00A0 is non-breaking space
|
||||||
[:div.group-button {:on-click add-graphic}
|
[:div.group-button {:on-click add-graphic}
|
||||||
i/plus
|
i/plus
|
||||||
[:& file-uploader {:accept di/str-media-types
|
[:& file-uploader {:accept cm/str-media-types
|
||||||
:multi true
|
:multi true
|
||||||
:input-ref file-input
|
:input-ref file-input
|
||||||
:on-selected on-files-selected}]]]
|
:on-selected on-files-selected}]]]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue