|
46 | 46 | (if (.hasNext it)
|
47 | 47 | (cons (.next it) (file-item-iterator-seq it)))))
|
48 | 48 |
|
49 |
| -(defn- file-item-seq |
50 |
| - "Create a seq of FileItem instances from a request context." |
51 |
| - [request progress-fn context] |
52 |
| - (let [upload (if progress-fn |
53 |
| - (doto (FileUpload.) |
54 |
| - (.setProgressListener (progress-listener request progress-fn))) |
55 |
| - (FileUpload.))] |
56 |
| - (file-item-iterator-seq |
57 |
| - (.getItemIterator ^FileUpload upload context)))) |
| 49 | +(defn- file-item-seq [^FileUpload upload context] |
| 50 | + (file-item-iterator-seq (.getItemIterator upload context))) |
58 | 51 |
|
59 | 52 | (defn- parse-content-type-charset [^FileItemStream item]
|
60 | 53 | (some->> (.getContentType item) parsing/find-content-type-charset))
|
|
86 | 79 | :stream (.openStream item)}))
|
87 | 80 | (.isFormField item)])
|
88 | 81 |
|
89 |
| -(defn- parse-multipart-params |
90 |
| - "Parse a map of multipart parameters from the request." |
91 |
| - [request fallback-encoding forced-encoding store progress-fn] |
92 |
| - (->> (request-context request fallback-encoding) |
93 |
| - (file-item-seq request progress-fn) |
94 |
| - (map #(parse-file-item % store)) |
95 |
| - (decode-string-values fallback-encoding forced-encoding) |
96 |
| - (reduce (fn [m [k v]] (assoc-conj m k v)) {}))) |
| 82 | +(defn- make-file-upload [request {:keys [progress-fn max-file-size]}] |
| 83 | + (let [upload (FileUpload.)] |
| 84 | + (.setFileSizeMax upload (or max-file-size -1)) |
| 85 | + (when progress-fn |
| 86 | + (.setProgressListener upload (progress-listener request progress-fn))) |
| 87 | + upload)) |
97 | 88 |
|
98 | 89 | (defn- load-var
|
99 | 90 | "Returns the var named by the supplied symbol, or nil if not found. Attempts
|
|
108 | 99 | func (load-var store)]
|
109 | 100 | (func))))
|
110 | 101 |
|
| 102 | +(defn- parse-multipart-params |
| 103 | + "Parse a map of multipart parameters from the request." |
| 104 | + [request {:keys [encoding fallback-encoding store] :as options}] |
| 105 | + (let [store (or store @default-store) |
| 106 | + fallback-encoding (or encoding |
| 107 | + fallback-encoding |
| 108 | + (req/character-encoding request) |
| 109 | + "UTF-8")] |
| 110 | + (->> (request-context request fallback-encoding) |
| 111 | + (file-item-seq (make-file-upload request options)) |
| 112 | + (map #(parse-file-item % store)) |
| 113 | + (decode-string-values fallback-encoding encoding) |
| 114 | + (reduce (fn [m [k v]] (assoc-conj m k v)) {})))) |
| 115 | + |
111 | 116 | (defn multipart-params-request
|
112 | 117 | "Adds :multipart-params and :params keys to request.
|
113 | 118 | See: wrap-multipart-params."
|
114 | 119 | {:added "1.2"}
|
115 | 120 | ([request]
|
116 | 121 | (multipart-params-request request {}))
|
117 | 122 | ([request options]
|
118 |
| - (let [store (or (:store options) @default-store) |
119 |
| - forced-encoding (:encoding options) |
120 |
| - req-encoding (or forced-encoding |
121 |
| - (:fallback-encoding options) |
122 |
| - (req/character-encoding request) |
123 |
| - "UTF-8") |
124 |
| - progress (:progress-fn options) |
125 |
| - params (if (multipart-form? request) |
126 |
| - (parse-multipart-params request |
127 |
| - req-encoding |
128 |
| - forced-encoding |
129 |
| - store |
130 |
| - progress) |
131 |
| - {})] |
| 123 | + (let [params (if (multipart-form? request) |
| 124 | + (parse-multipart-params request options) |
| 125 | + {})] |
132 | 126 | (merge-with merge request
|
133 | 127 | {:multipart-params params}
|
134 | 128 | {:params params}))))
|
|
162 | 156 |
|
163 | 157 | :progress-fn - a function that gets called during uploads. The
|
164 | 158 | function should expect four parameters: request,
|
165 |
| - bytes-read, content-length, and item-count." |
| 159 | + bytes-read, content-length, and item-count. |
| 160 | +
|
| 161 | + :max-file-size - the maximum size allowed size of a file in bytes. If |
| 162 | + nil or omitted, there is no limit." |
166 | 163 | ([handler]
|
167 | 164 | (wrap-multipart-params handler {}))
|
168 | 165 | ([handler options]
|
|
0 commit comments