|
137 | 137 | {:multipart-params params}
|
138 | 138 | {:params params}))))
|
139 | 139 |
|
| 140 | +(defn default-invalid-filename-handler [request e] |
| 141 | + (response/bad-request (.getMessage e))) |
| 142 | + |
140 | 143 | (defn wrap-multipart-params
|
141 | 144 | "Middleware to parse multipart parameters from a request. Adds the
|
142 | 145 | following keys to the request map:
|
|
146 | 149 |
|
147 | 150 | The following options are accepted
|
148 | 151 |
|
149 |
| - :encoding - character encoding to use for multipart parsing. |
150 |
| - Overrides the encoding specified in the request. If not |
151 |
| - specified, uses the encoding specified in a part named |
152 |
| - \"_charset_\", or the content type for each part, or |
153 |
| - request character encoding if the part has no encoding, |
154 |
| - or \"UTF-8\" if no request character encoding is set. |
155 |
| -
|
156 |
| - :fallback-encoding - specifies the character encoding used in parsing if a |
157 |
| - part of the request does not specify encoding in its |
158 |
| - content type or no part named \"_charset_\" is present. |
159 |
| - Has no effect if :encoding is also set. |
160 |
| -
|
161 |
| - :store - a function that stores a file upload. The function |
162 |
| - should expect a map with :filename, :content-type and |
163 |
| - :stream keys, and its return value will be used as the |
164 |
| - value for the parameter in the multipart parameter map. |
165 |
| - The default storage function is the temp-file-store. |
166 |
| -
|
167 |
| - :progress-fn - a function that gets called during uploads. The |
168 |
| - function should expect four parameters: request, |
169 |
| - bytes-read, content-length, and item-count." |
| 152 | + :encoding - character encoding to use for multipart parsing. |
| 153 | + Overrides the encoding specified in the request. If not |
| 154 | + specified, uses the encoding specified in a part named |
| 155 | + \"_charset_\", or the content type for each part, or |
| 156 | + request character encoding if the part has no encoding, |
| 157 | + or \"UTF-8\" if no request character encoding is set. |
| 158 | +
|
| 159 | + :fallback-encoding - specifies the character encoding used in parsing if a |
| 160 | + part of the request does not specify encoding in its |
| 161 | + content type or no part named \"_charset_\" is present. |
| 162 | + Has no effect if :encoding is also set. |
| 163 | +
|
| 164 | + :store - a function that stores a file upload. The function |
| 165 | + should expect a map with :filename, :content-type and |
| 166 | + :stream keys, and its return value will be used as the |
| 167 | + value for the parameter in the multipart parameter map. |
| 168 | + The default storage function is the temp-file-store. |
| 169 | +
|
| 170 | + :progress-fn - a function that gets called during uploads. The |
| 171 | + function should expect four parameters: request, |
| 172 | + bytes-read, content-length, and item-count. |
| 173 | +
|
| 174 | + :invalid-filename-handler - a function that gets called when the file being uploaded |
| 175 | + has an invalid name. The function should expect two |
| 176 | + parameters: request and an exception of type |
| 177 | + InvalidFileNameException." |
170 | 178 | ([handler]
|
171 | 179 | (wrap-multipart-params handler {}))
|
172 | 180 | ([handler options]
|
173 | 181 | (fn
|
174 | 182 | ([request]
|
175 |
| - (try |
176 |
| - (handler (multipart-params-request request options)) |
177 |
| - (catch InvalidFileNameException e |
178 |
| - (response/bad-request (.getMessage e))))) |
| 183 | + (let [invalid-file-name-handler |
| 184 | + (:invalid-filename-handler options default-invalid-filename-handler)] |
| 185 | + (try |
| 186 | + (handler (multipart-params-request request options)) |
| 187 | + (catch InvalidFileNameException e |
| 188 | + (invalid-file-name-handler request e))))) |
179 | 189 | ([request respond raise]
|
180 |
| - (try |
181 |
| - (handler (multipart-params-request request options) respond raise) |
182 |
| - (catch InvalidFileNameException e |
183 |
| - (handler request |
184 |
| - (fn [_] (respond (response/bad-request (.getMessage e)))) |
185 |
| - raise))))))) |
| 190 | + (let [invalid-file-name-handler |
| 191 | + (:invalid-filename-handler options default-invalid-filename-handler)] |
| 192 | + (try |
| 193 | + (handler (multipart-params-request request options) respond raise) |
| 194 | + (catch InvalidFileNameException e |
| 195 | + (respond (invalid-file-name-handler request e))))))))) |
0 commit comments