|
112 | 112 | func (load-var store)]
|
113 | 113 | (func))))
|
114 | 114 |
|
| 115 | +(defn default-invalid-filename-handler [request e] |
| 116 | + (response/bad-request (.getMessage e))) |
| 117 | + |
115 | 118 | (defn multipart-params-request
|
116 | 119 | "Adds :multipart-params and :params keys to request.
|
117 | 120 | See: wrap-multipart-params."
|
118 | 121 | {:added "1.2"}
|
119 | 122 | ([request]
|
120 | 123 | (multipart-params-request request {}))
|
121 | 124 | ([request options]
|
122 |
| - (let [store (or (:store options) @default-store) |
123 |
| - forced-encoding (:encoding options) |
124 |
| - req-encoding (or forced-encoding |
125 |
| - (:fallback-encoding options) |
126 |
| - (req/character-encoding request) |
127 |
| - "UTF-8") |
128 |
| - progress (:progress-fn options) |
129 |
| - params (if (multipart-form? request) |
130 |
| - (parse-multipart-params request |
131 |
| - req-encoding |
132 |
| - forced-encoding |
133 |
| - store |
134 |
| - progress) |
135 |
| - {})] |
136 |
| - (merge-with merge request |
137 |
| - {:multipart-params params} |
138 |
| - {:params params})))) |
139 |
| - |
140 |
| -(defn default-invalid-filename-handler [request e] |
141 |
| - (response/bad-request (.getMessage e))) |
| 125 | + (try |
| 126 | + (let [store (or (:store options) @default-store) |
| 127 | + forced-encoding (:encoding options) |
| 128 | + req-encoding (or forced-encoding |
| 129 | + (:fallback-encoding options) |
| 130 | + (req/character-encoding request) |
| 131 | + "UTF-8") |
| 132 | + progress (:progress-fn options) |
| 133 | + params (if (multipart-form? request) |
| 134 | + (parse-multipart-params request |
| 135 | + req-encoding |
| 136 | + forced-encoding |
| 137 | + store |
| 138 | + progress) |
| 139 | + {})] |
| 140 | + (merge-with merge request |
| 141 | + {:multipart-params params} |
| 142 | + {:params params})) |
| 143 | + (catch InvalidFileNameException e |
| 144 | + (let [invalid-filename-handler |
| 145 | + (:invalid-filename-handler options default-invalid-filename-handler)] |
| 146 | + (with-meta (invalid-filename-handler request e) {::error? true})))))) |
142 | 147 |
|
143 | 148 | (defn wrap-multipart-params
|
144 | 149 | "Middleware to parse multipart parameters from a request. Adds the
|
|
149 | 154 |
|
150 | 155 | The following options are accepted
|
151 | 156 |
|
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." |
| 157 | + :encoding |
| 158 | + - Character encoding to use for multipart parsing. |
| 159 | + Overrides the encoding specified in the request. If not |
| 160 | + specified, uses the encoding specified in a part named |
| 161 | + \"_charset_\", or the content type for each part, or |
| 162 | + request character encoding if the part has no encoding, |
| 163 | + or \"UTF-8\" if no request character encoding is set. |
| 164 | +
|
| 165 | + :fallback-encoding |
| 166 | + - Specifies the character encoding used in parsing if a |
| 167 | + part of the request does not specify encoding in its |
| 168 | + content type or no part named \"_charset_\" is present. |
| 169 | + Has no effect if :encoding is also set. |
| 170 | +
|
| 171 | + :store |
| 172 | + - A function that stores a file upload. The function |
| 173 | + should expect a map with :filename, :content-type and |
| 174 | + :stream keys, and its return value will be used as the |
| 175 | + value for the parameter in the multipart parameter map. |
| 176 | + The default storage function is the temp-file-store. |
| 177 | +
|
| 178 | + :progress-fn |
| 179 | + - A function that gets called during uploads. The |
| 180 | + function should expect four parameters: request, |
| 181 | + bytes-read, content-length, and item-count. |
| 182 | +
|
| 183 | + :invalid-filename-handler |
| 184 | + - A function that gets called when the file being uploaded |
| 185 | + has an invalid name. The function should expect two |
| 186 | + parameters: request and an exception of type |
| 187 | + InvalidFileNameException." |
178 | 188 | ([handler]
|
179 | 189 | (wrap-multipart-params handler {}))
|
180 | 190 | ([handler options]
|
181 | 191 | (fn
|
182 | 192 | ([request]
|
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))))) |
| 193 | + (let [multipart-params-request (multipart-params-request request options)] |
| 194 | + (if (contains? (meta multipart-params-request) ::error?) |
| 195 | + multipart-params-request |
| 196 | + (handler multipart-params-request)))) |
189 | 197 | ([request respond 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))))))))) |
| 198 | + (let [multipart-params-request (multipart-params-request request options)] |
| 199 | + (if (contains? (meta multipart-params-request) ::error?) |
| 200 | + (handler multipart-params-request respond raise) |
| 201 | + (respond multipart-params-request))))))) |
0 commit comments