♻️ Remove duplicated code

This commit is contained in:
Andrés Moya 2020-08-10 10:01:51 +02:00 committed by Andrey Antukh
parent d349e46cd8
commit 93bde62581
16 changed files with 188 additions and 306 deletions

View file

@ -57,6 +57,9 @@
;; Related info on how thumbnails generation
;; 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
@ -196,6 +199,13 @@
(us/assert (s/coll-of vector?) pairs)
(reduce #(resolve-uri mst/media-storage %1 (nth %2 0) (nth %2 1)) row pairs))
(defn validate-media-type
[media-type]
(when-not (valid-media-types media-type)
(ex/raise :type :validation
:code :media-type-not-allowed
:hint "Seems like you are uploading an invalid media object")))
(defn download-media-object
[url]
(let [result (http/get! url {:as :byte-array})

View file

@ -45,12 +45,9 @@
(declare persist-media-object-on-fs)
(declare persist-media-thumbnail-on-fs)
(def valid-media-object-types?
#{"image/jpeg", "image/png", "image/webp", "image/svg+xml"})
(s/def :uxbox$upload/filename ::us/string)
(s/def :uxbox$upload/size ::us/integer)
(s/def :uxbox$upload/content-type valid-media-object-types?)
(s/def :uxbox$upload/content-type media/valid-media-types)
(s/def :uxbox$upload/tempfile any?)
(s/def ::upload
@ -64,11 +61,11 @@
(s/def ::is-local ::us/boolean)
(s/def ::add-media-object-from-url
(s/keys :req-un [::profile-id ::file-id ::url ::is-local]
(s/keys :req-un [::profile-id ::file-id ::is-local ::url]
:opt-un [::id]))
(s/def ::upload-media-object
(s/keys :req-un [::profile-id ::file-id ::name ::content ::is-local]
(s/keys :req-un [::profile-id ::file-id ::is-local ::name ::content]
:opt-un [::id]))
(sm/defmutation ::add-media-object-from-url
@ -89,12 +86,8 @@
(create-media-object conn params))))
(defn create-media-object
[conn {:keys [id content file-id name is-local]}]
(when-not (valid-media-object-types? (:content-type content))
(ex/raise :type :validation
:code :media-type-not-allowed
:hint "Seems like you are uploading an invalid media object."))
[conn {:keys [id file-id is-local name content]}]
(media/validate-media-type (:content-type content))
(let [info (media/run {:cmd :info :input {:path (:tempfile content)
:mtype (:content-type content)}})
path (persist-media-object-on-fs content)

View file

@ -272,15 +272,11 @@
(sm/defmutation ::update-profile-photo
[{:keys [profile-id file] :as params}]
(when-not (media-mutations/valid-media-object-types? (:content-type file))
(ex/raise :type :validation
:code :media-type-not-allowed
:hint "Seems like you are uploading an invalid media object"))
(media/validate-media-type (:content-type file))
(db/with-atomic [conn db/pool]
(let [profile (profile/retrieve-profile conn profile-id)
_ (media/run {:cmd :info :input {:path (:tempfile file)
:mtype (:content-type file)}})
:mtype (:content-type file)}})
photo (upload-photo conn params)]
;; Schedule deletion of old photo