Skip to content

Commit 4c18803

Browse files
committed
Clean up ring.middleware.multipart-params
1 parent 1f5d1a0 commit 4c18803

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

ring-core/src/ring/middleware/multipart_params.clj

+21-35
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,33 @@
1010
(:require [ring.util.codec :refer [assoc-conj]]
1111
[ring.util.request :as req]
1212
[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]
1819
[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]
2222
(reify ProgressListener
23-
(update [this bytes-read content-length item-count]
23+
(update [_ bytes-read content-length item-count]
2424
(progress-fn request bytes-read content-length item-count))))
2525

26-
(defn- multipart-form?
27-
"Does a request have a multipart form?"
28-
[request]
26+
(defn- multipart-form? [request]
2927
(= (req/content-type request) "multipart/form-data"))
3028

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]
3530
(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]
4538
(lazy-seq
46-
(if (.hasNext it)
39+
(when (.hasNext it)
4740
(cons (.next it) (file-item-iterator-seq it)))))
4841

4942
(defn- file-item-seq [^FileUpload upload context]
@@ -66,10 +59,7 @@
6659
fallback-encoding)))
6760
v)])))
6861

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]
7363
[(.getFieldName item)
7464
(if (.isFormField item)
7565
{:bytes (IOUtils/toByteArray (.openStream item))
@@ -86,10 +76,7 @@
8676
(.setProgressListener upload (progress-listener request progress-fn)))
8777
upload))
8878

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]
9380
(require (symbol (namespace sym)))
9481
(find-var sym))
9582

@@ -100,7 +87,6 @@
10087
(func))))
10188

10289
(defn- parse-multipart-params
103-
"Parse a map of multipart parameters from the request."
10490
[request {:keys [encoding fallback-encoding store] :as options}]
10591
(let [store (or store @default-store)
10692
fallback-encoding (or encoding

0 commit comments

Comments
 (0)