Skip to content

Commit

Permalink
Changes based on review 4
Browse files Browse the repository at this point in the history
- Refactor to avoid using metadata.
- Expand the docstring slightly to give some indication of the expected
  return value when invalid-filename-handler is overridden.
  • Loading branch information
expez committed Sep 25, 2018
1 parent cd881bd commit 65c03a0
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions ring-core/src/ring/middleware/multipart_params.clj
Original file line number Diff line number Diff line change
Expand Up @@ -112,38 +112,33 @@
func (load-var store)]
(func))))

(defn default-invalid-filename-handler [request e]
(response/bad-request (.getMessage e)))

(defn multipart-params-request
"Adds :multipart-params and :params keys to request.
See: wrap-multipart-params."
{:added "1.2"}
([request]
(multipart-params-request request {}))
([request options]
(try
(let [store (or (:store options) @default-store)
forced-encoding (:encoding options)
req-encoding (or forced-encoding
(:fallback-encoding options)
(req/character-encoding request)
"UTF-8")
progress (:progress-fn options)
params (if (multipart-form? request)
(parse-multipart-params request
req-encoding
forced-encoding
store
progress)
{})]
(merge-with merge request
{:multipart-params params}
{:params params}))
(catch InvalidFileNameException e
(let [invalid-filename-handler
(:invalid-filename-handler options default-invalid-filename-handler)]
(with-meta (invalid-filename-handler request e) {::error? true}))))))
(let [store (or (:store options) @default-store)
forced-encoding (:encoding options)
req-encoding (or forced-encoding
(:fallback-encoding options)
(req/character-encoding request)
"UTF-8")
progress (:progress-fn options)
params (if (multipart-form? request)
(parse-multipart-params request
req-encoding
forced-encoding
store
progress)
{})]
(merge-with merge request
{:multipart-params params}
{:params params}))))

(defn default-invalid-filename-handler [request e]
(response/bad-request (.getMessage e)))

(defn wrap-multipart-params
"Middleware to parse multipart parameters from a request. Adds the
Expand Down Expand Up @@ -184,18 +179,26 @@
- A function that gets called when the file being uploaded
has an invalid name. The function should expect two
parameters: request and an exception of type
InvalidFileNameException."
InvalidFileNameException. It should return a ring response."
([handler]
(wrap-multipart-params handler {}))
([handler options]
(fn
([request]
(let [multipart-params-request (multipart-params-request request options)]
(if (contains? (meta multipart-params-request) ::error?)
multipart-params-request
(handler multipart-params-request))))
(let [req-or-ex (try
(multipart-params-request request options)
(catch Exception ex ex))
invalid-filename-handler
(:invalid-filename-handler options default-invalid-filename-handler)]
(if (instance? Throwable req-or-ex)
(invalid-filename-handler request req-or-ex)
(handler req-or-ex))))
([request respond raise]
(let [multipart-params-request (multipart-params-request request options)]
(if (contains? (meta multipart-params-request) ::error?)
(handler multipart-params-request respond raise)
(respond multipart-params-request)))))))
(let [req-or-ex (try
(multipart-params-request request options)
(catch Exception ex ex))
invalid-filename-handler
(:invalid-filename-handler options default-invalid-filename-handler)]
(if (instance? Throwable req-or-ex)
(respond (invalid-filename-handler request req-or-ex))
(handler req-or-ex respond raise)))))))

0 comments on commit 65c03a0

Please sign in to comment.