|
10 | 10 | (:require [ring.util.codec :refer [assoc-conj]]
|
11 | 11 | [ring.util.request :as req]
|
12 | 12 | [ring.util.parsing :as parsing])
|
13 |
| - (:import [org.apache.commons.fileupload UploadContext |
14 |
| - FileItemIterator |
15 |
| - FileItemStream |
16 |
| - FileUpload |
17 |
| - ProgressListener] |
| 13 | + (:import [org.apache.commons.fileupload |
| 14 | + UploadContext |
| 15 | + FileItemIterator |
| 16 | + FileItemStream |
| 17 | + FileUpload |
| 18 | + ProgressListener] |
18 | 19 | [org.apache.commons.io IOUtils]))
|
19 |
| -(defn- progress-listener |
20 |
| - "Create a progress listener that calls the supplied function." |
21 |
| - [request progress-fn] |
| 20 | + |
| 21 | +(defn- progress-listener [request progress-fn] |
22 | 22 | (reify ProgressListener
|
23 |
| - (update [this bytes-read content-length item-count] |
| 23 | + (update [_ bytes-read content-length item-count] |
24 | 24 | (progress-fn request bytes-read content-length item-count))))
|
25 | 25 |
|
26 |
| -(defn- multipart-form? |
27 |
| - "Does a request have a multipart form?" |
28 |
| - [request] |
| 26 | +(defn- multipart-form? [request] |
29 | 27 | (= (req/content-type request) "multipart/form-data"))
|
30 | 28 |
|
31 |
| -(defn- request-context |
32 |
| - "Create an UploadContext object from a request map." |
33 |
| - {:tag UploadContext} |
34 |
| - [request encoding] |
| 29 | +(defn- request-context ^UploadContext [request encoding] |
35 | 30 | (reify UploadContext
|
36 |
| - (getContentType [this] (get-in request [:headers "content-type"])) |
37 |
| - (getContentLength [this] (or (req/content-length request) -1)) |
38 |
| - (contentLength [this] (or (req/content-length request) -1)) |
39 |
| - (getCharacterEncoding [this] encoding) |
40 |
| - (getInputStream [this] (:body request)))) |
41 |
| - |
42 |
| -(defn- file-item-iterator-seq |
43 |
| - "Create a lazy seq from a FileItemIterator instance." |
44 |
| - [^FileItemIterator it] |
| 31 | + (getContentType [_] (get-in request [:headers "content-type"])) |
| 32 | + (getContentLength [_] (or (req/content-length request) -1)) |
| 33 | + (contentLength [_] (or (req/content-length request) -1)) |
| 34 | + (getCharacterEncoding [_] encoding) |
| 35 | + (getInputStream [_] (:body request)))) |
| 36 | + |
| 37 | +(defn- file-item-iterator-seq [^FileItemIterator it] |
45 | 38 | (lazy-seq
|
46 |
| - (if (.hasNext it) |
| 39 | + (when (.hasNext it) |
47 | 40 | (cons (.next it) (file-item-iterator-seq it)))))
|
48 | 41 |
|
49 | 42 | (defn- file-item-seq [^FileUpload upload context]
|
|
66 | 59 | fallback-encoding)))
|
67 | 60 | v)])))
|
68 | 61 |
|
69 |
| -(defn- parse-file-item |
70 |
| - "Parse a FileItemStream into a key-value pair. If the request is a file the |
71 |
| - supplied store function is used to save it." |
72 |
| - [^FileItemStream item store] |
| 62 | +(defn- parse-file-item [^FileItemStream item store] |
73 | 63 | [(.getFieldName item)
|
74 | 64 | (if (.isFormField item)
|
75 | 65 | {:bytes (IOUtils/toByteArray (.openStream item))
|
|
86 | 76 | (.setProgressListener upload (progress-listener request progress-fn)))
|
87 | 77 | upload))
|
88 | 78 |
|
89 |
| -(defn- load-var |
90 |
| - "Returns the var named by the supplied symbol, or nil if not found. Attempts |
91 |
| - to load the var namespace on the fly if not already loaded." |
92 |
| - [sym] |
| 79 | +(defn- load-var [sym] |
93 | 80 | (require (symbol (namespace sym)))
|
94 | 81 | (find-var sym))
|
95 | 82 |
|
|
100 | 87 | (func))))
|
101 | 88 |
|
102 | 89 | (defn- parse-multipart-params
|
103 |
| - "Parse a map of multipart parameters from the request." |
104 | 90 | [request {:keys [encoding fallback-encoding store] :as options}]
|
105 | 91 | (let [store (or store @default-store)
|
106 | 92 | fallback-encoding (or encoding
|
|
0 commit comments