♻️ Move some more duplicated code to common module

This commit is contained in:
Andrés Moya 2020-08-11 11:33:38 +02:00 committed by Andrey Antukh
parent 62e2c1cc14
commit 91899be423
8 changed files with 91 additions and 70 deletions

View file

@ -12,6 +12,7 @@
[potok.core :as ptk]
[uxbox.common.spec :as us]
[uxbox.common.data :as d]
[uxbox.common.media :as cm]
[uxbox.main.data.messages :as dm]
[uxbox.main.store :as st]
[uxbox.main.repo :as rp]
@ -24,40 +25,17 @@
;; --- 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-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
(defn validate-file
;; Check that a file obtained with the file javascript API is valid.
[file]
(when (> (.-size file) max-file-size)
(when (> (.-size file) cm/max-file-size)
(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") {})))
file)

View file

@ -13,6 +13,7 @@
[cljs.spec.alpha :as s]
[potok.core :as ptk]
[uxbox.common.data :as d]
[uxbox.common.media :as cm]
[uxbox.common.geom.point :as gpt]
[uxbox.common.pages :as cp]
[uxbox.common.spec :as us]
@ -409,7 +410,7 @@
[file-id is-local media-object]
(us/verify ::us/uuid file-id)
(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/UpdateEvent
(update [_ state]

View file

@ -11,8 +11,8 @@
(ns uxbox.main.ui.workspace.left-toolbar
(:require
[rumext.alpha :as mf]
[uxbox.common.media :as cm]
[uxbox.main.refs :as refs]
[uxbox.main.data.media :as di]
[uxbox.main.data.workspace :as dw]
[uxbox.main.store :as st]
[uxbox.main.ui.components.file-uploader :refer [file-uploader]]
@ -74,7 +74,7 @@
:on-click on-image}
[:*
i/image
[:& file-uploader {:accept di/str-media-types
[:& file-uploader {:accept cm/str-media-types
:multi true
:input-ref file-input
:on-selected on-files-selected}]]]

View file

@ -13,11 +13,11 @@
[cuerdas.core :as str]
[rumext.alpha :as mf]
[uxbox.common.data :as d]
[uxbox.common.media :as cm]
[uxbox.common.pages :as cp]
[uxbox.common.geom.shapes :as geom]
[uxbox.common.geom.point :as gpt]
[uxbox.main.ui.icons :as i]
[uxbox.main.data.media :as di]
[uxbox.main.data.workspace :as dw]
[uxbox.main.data.colors :as dcol]
[uxbox.main.refs :as refs]
@ -114,7 +114,7 @@
[:span (str "\u00A0(") (count media-objects) ")"] ;; Unicode 00A0 is non-breaking space
[:div.group-button {:on-click add-graphic}
i/plus
[:& file-uploader {:accept di/str-media-types
[:& file-uploader {:accept cm/str-media-types
:multi true
:input-ref file-input
:on-selected on-files-selected}]]]