|
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))) |
| 140 | +(defn default-invalid-filename-handler |
| 141 | + ([request] |
| 142 | + (-> request ::invalid-filename-exception .getMessage response/bad-request)) |
| 143 | + ([request respond raise] |
| 144 | + (respond (default-invalid-filename-handler request)))) |
142 | 145 |
|
143 | 146 | (defn wrap-multipart-params |
144 | 147 | "Middleware to parse multipart parameters from a request. Adds the following |
|
172 | 175 | parameters: request, bytes-read, content-length, and item-count. |
173 | 176 |
|
174 | 177 | :invalid-filename-handler |
175 | | - - A function that gets called when the file being uploaded has an invalid name. |
176 | | - The function should expect two parameters: request and an exception of type |
177 | | - InvalidFileNameException. It should return a ring response." |
| 178 | + - A handler that gets called when the file being uploaded has an invalid name. |
| 179 | + When this handler receives the request it can expect one additional key, |
| 180 | + ::invalid-filename-exception, which contains an InvalidFileNameException." |
178 | 181 | ([handler] |
179 | 182 | (wrap-multipart-params handler {})) |
180 | 183 | ([handler options] |
|
183 | 186 | (fn ([request] |
184 | 187 | (let [req-or-ex (try |
185 | 188 | (multipart-params-request request options) |
186 | | - (catch Exception ex ex))] |
187 | | - (if (instance? Throwable req-or-ex) |
188 | | - (invalid-filename-handler request req-or-ex) |
| 189 | + (catch InvalidFileNameException ex ex))] |
| 190 | + (if (instance? InvalidFileNameException req-or-ex) |
| 191 | + (invalid-filename-handler |
| 192 | + (assoc request ::invalid-filename-exception req-or-ex)) |
189 | 193 | (handler req-or-ex)))) |
190 | 194 | ([request respond raise] |
191 | 195 | (let [req-or-ex (try |
192 | 196 | (multipart-params-request request options) |
193 | | - (catch Exception ex ex))] |
194 | | - (if (instance? Throwable req-or-ex) |
195 | | - (respond (invalid-filename-handler request req-or-ex)) |
| 197 | + (catch InvalidFileNameException ex ex))] |
| 198 | + (if (instance? InvalidFileNameException req-or-ex) |
| 199 | + (invalid-filename-handler |
| 200 | + (assoc request ::invalid-filename-exception req-or-ex) respond raise) |
196 | 201 | (handler req-or-ex respond raise)))))))) |
0 commit comments