diff --git a/ring-core/src/ring/middleware/multipart_params.clj b/ring-core/src/ring/middleware/multipart_params.clj index 54aefc1b..0ae6f0e6 100644 --- a/ring-core/src/ring/middleware/multipart_params.clj +++ b/ring-core/src/ring/middleware/multipart_params.clj @@ -10,40 +10,33 @@ (:require [ring.util.codec :refer [assoc-conj]] [ring.util.request :as req] [ring.util.parsing :as parsing]) - (:import [org.apache.commons.fileupload UploadContext - FileItemIterator - FileItemStream - FileUpload - ProgressListener] + (:import [org.apache.commons.fileupload + UploadContext + FileItemIterator + FileItemStream + FileUpload + ProgressListener] [org.apache.commons.io IOUtils])) -(defn- progress-listener - "Create a progress listener that calls the supplied function." - [request progress-fn] + +(defn- progress-listener [request progress-fn] (reify ProgressListener - (update [this bytes-read content-length item-count] + (update [_ bytes-read content-length item-count] (progress-fn request bytes-read content-length item-count)))) -(defn- multipart-form? - "Does a request have a multipart form?" - [request] +(defn- multipart-form? [request] (= (req/content-type request) "multipart/form-data")) -(defn- request-context - "Create an UploadContext object from a request map." - {:tag UploadContext} - [request encoding] +(defn- request-context ^UploadContext [request encoding] (reify UploadContext - (getContentType [this] (get-in request [:headers "content-type"])) - (getContentLength [this] (or (req/content-length request) -1)) - (contentLength [this] (or (req/content-length request) -1)) - (getCharacterEncoding [this] encoding) - (getInputStream [this] (:body request)))) - -(defn- file-item-iterator-seq - "Create a lazy seq from a FileItemIterator instance." - [^FileItemIterator it] + (getContentType [_] (get-in request [:headers "content-type"])) + (getContentLength [_] (or (req/content-length request) -1)) + (contentLength [_] (or (req/content-length request) -1)) + (getCharacterEncoding [_] encoding) + (getInputStream [_] (:body request)))) + +(defn- file-item-iterator-seq [^FileItemIterator it] (lazy-seq - (if (.hasNext it) + (when (.hasNext it) (cons (.next it) (file-item-iterator-seq it))))) (defn- file-item-seq [^FileUpload upload context] @@ -66,10 +59,7 @@ fallback-encoding))) v)]))) -(defn- parse-file-item - "Parse a FileItemStream into a key-value pair. If the request is a file the - supplied store function is used to save it." - [^FileItemStream item store] +(defn- parse-file-item [^FileItemStream item store] [(.getFieldName item) (if (.isFormField item) {:bytes (IOUtils/toByteArray (.openStream item)) @@ -86,10 +76,7 @@ (.setProgressListener upload (progress-listener request progress-fn))) upload)) -(defn- load-var - "Returns the var named by the supplied symbol, or nil if not found. Attempts - to load the var namespace on the fly if not already loaded." - [sym] +(defn- load-var [sym] (require (symbol (namespace sym))) (find-var sym)) @@ -100,7 +87,6 @@ (func)))) (defn- parse-multipart-params - "Parse a map of multipart parameters from the request." [request {:keys [encoding fallback-encoding store] :as options}] (let [store (or store @default-store) fallback-encoding (or encoding