-
Notifications
You must be signed in to change notification settings - Fork 250
/
multipart.clj
73 lines (65 loc) · 2.78 KB
/
multipart.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(ns reitit.ring.middleware.multipart
(:refer-clojure :exclude [compile])
(:require [clojure.spec.alpha :as s]
[reitit.coercion :as coercion]
[ring.middleware.multipart-params :as multipart-params]
[spec-tools.core :as st])
(:import (java.io File)))
(s/def ::filename string?)
(s/def ::content-type string?)
(s/def ::tempfile (partial instance? File))
(s/def ::bytes bytes?)
(s/def ::size int?)
(def temp-file-part
"Spec for file param created by ring.middleware.multipart-params.temp-file store."
(st/spec
{:spec (s/keys :req-un [::filename ::content-type ::tempfile ::size])
:swagger/type "file"}))
(def bytes-part
"Spec for file param created by ring.middleware.multipart-params.byte-array store."
(st/spec
{:spec (s/keys :req-un [::filename ::content-type ::bytes])
:swagger/type "file"}))
(defn- coerced-request [request coercers]
(if-let [coerced (if coercers (coercion/coerce-request coercers request))]
(update request :parameters merge coerced)
request))
(defn- compile [options]
(fn [{:keys [parameters coercion]} opts]
(if-let [multipart (:multipart parameters)]
(let [parameter-coercion {:multipart (coercion/->ParameterCoercion
:multipart-params :string true true)}
opts (assoc opts ::coercion/parameter-coercion parameter-coercion)
coercers (if multipart (coercion/request-coercers coercion parameters opts))]
{:data {:swagger {:consumes ^:replace #{"multipart/form-data"}}}
:wrap (fn [handler]
(fn
([request]
(-> request
(multipart-params/multipart-params-request options)
(coerced-request coercers)
(handler)))
([request respond raise]
(-> request
(multipart-params/multipart-params-request options)
(coerced-request coercers)
(handler respond raise)))))}))))
;;
;; public api
;;
(defn create-multipart-middleware
"Creates a Middleware to handle the multipart params, based on
ring.middleware.multipart-params, taking same options. Mounts only
if endpoint has `[:parameters :multipart]` defined. Publishes coerced
parameters into `[:parameters :multipart]` under request."
([]
(create-multipart-middleware nil))
([options]
{:name ::multipart
:compile (compile options)}))
(def multipart-middleware
"Middleware to handle the multipart params, based on
ring.middleware.multipart-params, taking same options. Mounts only
if endpoint has `[:parameters :multipart]` defined. Publishes coerced
parameters into `[:parameters :multipart]` under request."
(create-multipart-middleware))