|
11 | 11 | [ring.util.codec :refer [assoc-conj]] |
12 | 12 | [ring.util.request :as req] |
13 | 13 | [ring.util.parsing :as parsing]) |
14 | | - (:import [org.apache.commons.fileupload |
15 | | - UploadContext |
16 | | - FileItemStream |
17 | | - FileUpload |
18 | | - FileUploadBase$FileUploadIOException |
| 14 | + (:import [org.apache.commons.fileupload2.core |
| 15 | + AbstractFileUpload |
| 16 | + RequestContext |
| 17 | + FileItemInput |
| 18 | + FileUploadException |
19 | 19 | ProgressListener] |
20 | 20 | [org.apache.commons.io IOUtils])) |
21 | 21 |
|
|
24 | 24 | (update [_ bytes-read content-length item-count] |
25 | 25 | (progress-fn request bytes-read content-length item-count)))) |
26 | 26 |
|
27 | | -(defn- set-progress-listener [^FileUpload upload request progress-fn] |
| 27 | +(defn- set-progress-listener [^AbstractFileUpload upload request progress-fn] |
28 | 28 | (when progress-fn |
29 | 29 | (.setProgressListener upload (progress-listener request progress-fn)))) |
30 | 30 |
|
31 | 31 | (defn- file-upload [request {:keys [progress-fn max-file-size]}] |
32 | | - (doto (FileUpload.) |
33 | | - (.setFileSizeMax (or max-file-size -1)) |
| 32 | + (doto (proxy [AbstractFileUpload] []) |
| 33 | + ;; There seems to be an off-by-one bug in FileUpload 2.0.0-M1 that requires |
| 34 | + ;; us to increment the max-file-size option to get it to work correctly. |
| 35 | + (.setFileSizeMax (if max-file-size (inc max-file-size) -1)) |
34 | 36 | (set-progress-listener request progress-fn))) |
35 | 37 |
|
36 | 38 | (defn- multipart-form? [request] |
37 | 39 | (= (req/content-type request) "multipart/form-data")) |
38 | 40 |
|
39 | | -(defn- request-context ^UploadContext [request encoding] |
40 | | - (reify UploadContext |
| 41 | +(defn- request-context ^RequestContext [request encoding] |
| 42 | + (reify RequestContext |
41 | 43 | (getContentType [_] (get-in request [:headers "content-type"])) |
42 | 44 | (getContentLength [_] (or (req/content-length request) -1)) |
43 | | - (contentLength [_] (or (req/content-length request) -1)) |
44 | 45 | (getCharacterEncoding [_] encoding) |
45 | 46 | (getInputStream [_] (:body request)))) |
46 | 47 |
|
47 | | -(defn- file-item-iterable [^FileUpload upload context] |
| 48 | +(defn- file-item-iterable [^AbstractFileUpload upload ^RequestContext context] |
48 | 49 | (reify Iterable |
49 | 50 | (iterator [_] |
50 | 51 | (let [it (.getItemIterator upload context)] |
51 | 52 | (reify java.util.Iterator |
52 | 53 | (hasNext [_] (.hasNext it)) |
53 | 54 | (next [_] (.next it))))))) |
54 | 55 |
|
55 | | -(defn- parse-content-type-charset [^FileItemStream item] |
| 56 | +(defn- parse-content-type-charset [^FileItemInput item] |
56 | 57 | (some->> (.getContentType item) parsing/find-content-type-charset)) |
57 | 58 |
|
58 | | -(defn- parse-file-item [^FileItemStream item store] |
| 59 | +(defn- parse-file-item [^FileItemInput item store] |
59 | 60 | {:field? (.isFormField item) |
60 | 61 | :name (.getFieldName item) |
61 | 62 | :value (if (.isFormField item) |
62 | | - {:bytes (IOUtils/toByteArray (.openStream item)) |
| 63 | + {:bytes (IOUtils/toByteArray (.getInputStream item)) |
63 | 64 | :encoding (parse-content-type-charset item)} |
64 | 65 | (store {:filename (.getName item) |
65 | 66 | :content-type (.getContentType item) |
66 | | - :stream (.openStream item)}))}) |
| 67 | + :stream (.getInputStream item)}))}) |
67 | 68 |
|
68 | 69 | (defn- find-param [params name] |
69 | 70 | (first (filter #(= name (:name %)) params))) |
|
133 | 134 | ((try |
134 | 135 | (let [request (requestf)] |
135 | 136 | #(handlef request)) |
136 | | - (catch FileUploadBase$FileUploadIOException _ |
| 137 | + (catch FileUploadException _ |
137 | 138 | errorf) |
138 | 139 | (catch clojure.lang.ExceptionInfo _ |
139 | 140 | errorf)))) |
|
0 commit comments