Simplify media mime types structs

This commit is contained in:
Andrey Antukh 2025-05-24 11:28:25 +02:00
parent d9d2cc7b4e
commit 8f774a3611
6 changed files with 25 additions and 10 deletions

View file

@ -55,7 +55,7 @@
(sm/check-fn schema:input))
(defn validate-media-type!
([upload] (validate-media-type! upload cm/valid-image-types))
([upload] (validate-media-type! upload cm/image-types))
([upload allowed]
(when-not (contains? allowed (:mtype upload))
(ex/raise :type :validation

View file

@ -9,11 +9,18 @@
(:require
[cuerdas.core :as str]))
;; We have added ".ttf" as string to solve a problem with chrome input selector
(def valid-font-types #{"font/ttf" ".ttf" "font/woff", "application/font-woff" "woff" "font/otf" ".otf" "font/opentype"})
(def valid-image-types #{"image/jpeg", "image/png", "image/webp", "image/gif", "image/svg+xml"})
(def str-image-types (str/join "," valid-image-types))
(def str-font-types (str/join "," valid-font-types))
(def font-types
#{"font/ttf"
"font/woff"
"font/otf"
"font/opentype"})
(def image-types
#{"image/jpeg"
"image/png"
"image/webp"
"image/gif"
"image/svg+xml"})
(defn format->extension
[format]

View file

@ -15,6 +15,9 @@
[cljs.spec.alpha :as s]
[cuerdas.core :as str]))
(def accept-image-types
(str/join "," cm/image-types))
;; --- Predicates
(defn file?
@ -38,7 +41,7 @@
(defn validate-file
"Check that a file obtained with the file javascript API is valid."
[file]
(when-not (contains? cm/valid-image-types (.-type file))
(when-not (contains? cm/image-types (.-type file))
(ex/raise :type :validation
:code :media-type-not-allowed
:hint (str/ffmt "media type % is not supported" (.-type file))))

View file

@ -29,6 +29,11 @@
[okulary.core :as l]
[rumext.v2 :as mf]))
(def ^:private accept-font-types
(str (str/join "," font-types)
;; A workaround to solve a problem with chrome input selector
",.ttf,application/font-woff,woff,.otf"))
(defn- use-page-title
[team section]
(mf/with-effect [team]
@ -180,7 +185,7 @@
:tab-index "0"}
[:span (tr "labels.add-custom-font")]
[:& file-uploader {:input-id "font-upload"
:accept cm/str-font-types
:accept accept-font-types
:multi true
:ref input-ref
:on-selected on-selected}]]

View file

@ -525,7 +525,7 @@
:aria-label (tr "workspace.assets.components.add-component")
:on-click add-component
:icon "add"}
[:& file-uploader {:accept cm/str-image-types
[:& file-uploader {:accept dwm/accept-image-types
:multi true
:ref input-ref
:on-selected on-file-selected}]])]

View file

@ -64,7 +64,7 @@
i/img
[:& file-uploader
{:input-id "image-upload"
:accept cm/str-image-types
:accept dwm/accept-image-types
:multi true
:ref ref
:on-selected on-selected}]]]))