From 3f988b47e60bdf419242b2032b8860ad3e20532a Mon Sep 17 00:00:00 2001 From: Lars Andersen Date: Tue, 25 Sep 2018 11:32:35 +0200 Subject: [PATCH] Tweaks after review 5 - Wrap docstring at column 80. - Expand let to prevent per-request lookup of invalid-filename-handler. --- .../src/ring/middleware/multipart_params.clj | 78 +++++++++---------- 1 file changed, 35 insertions(+), 43 deletions(-) diff --git a/ring-core/src/ring/middleware/multipart_params.clj b/ring-core/src/ring/middleware/multipart_params.clj index 1eb755ef..d0ad0c2c 100644 --- a/ring-core/src/ring/middleware/multipart_params.clj +++ b/ring-core/src/ring/middleware/multipart_params.clj @@ -141,64 +141,56 @@ (response/bad-request (.getMessage e))) (defn wrap-multipart-params - "Middleware to parse multipart parameters from a request. Adds the - following keys to the request map: + "Middleware to parse multipart parameters from a request. Adds the following + keys to the request map: :multipart-params - a map of multipart parameters :params - a merged map of all types of parameter - The following options are accepted + The following options are accepted: :encoding - - Character encoding to use for multipart parsing. - Overrides the encoding specified in the request. If not - specified, uses the encoding specified in a part named - \"_charset_\", or the content type for each part, or - request character encoding if the part has no encoding, - or \"UTF-8\" if no request character encoding is set. + - Character encoding to use for multipart parsing. Overrides the encoding + specified in the request. If not specified,uses the encoding specified in a + part named \"_charset_\", or the content type for each part, or request + character encoding if the part has no encoding, or \"UTF-8\" if no request + character encoding is set. :fallback-encoding - - Specifies the character encoding used in parsing if a - part of the request does not specify encoding in its - content type or no part named \"_charset_\" is present. - Has no effect if :encoding is also set. + - Specifies the character encoding used in parsing if a part of the request + does not specify encoding in its content type or no part named \"_charset_\" + is present. Has no effect if :encoding is also set. :store - - A function that stores a file upload. The function - should expect a map with :filename, :content-type and - :stream keys, and its return value will be used as the - value for the parameter in the multipart parameter map. - The default storage function is the temp-file-store. + - A function that stores a file upload. The function should expect a map with + :filename, :content-type and :stream keys, and its return value will be used + as the value for the parameter in the multipart parameter map. The default + storage function is the temp-file-store. :progress-fn - - A function that gets called during uploads. The - function should expect four parameters: request, - bytes-read, content-length, and item-count. + - A function that gets called during uploads. The function should expect four + parameters: request, bytes-read, content-length, and item-count. :invalid-filename-handler - - 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 + - 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. It should return a ring response." ([handler] (wrap-multipart-params handler {})) ([handler options] - (fn - ([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 [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))))))) + (let [invalid-filename-handler + (:invalid-filename-handler options default-invalid-filename-handler)] + (fn ([request] + (let [req-or-ex (try + (multipart-params-request request options) + (catch Exception ex ex))] + (if (instance? Throwable req-or-ex) + (invalid-filename-handler request req-or-ex) + (handler req-or-ex)))) + ([request respond raise] + (let [req-or-ex (try + (multipart-params-request request options) + (catch Exception ex ex))] + (if (instance? Throwable req-or-ex) + (respond (invalid-filename-handler request req-or-ex)) + (handler req-or-ex respond raise))))))))