Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding of the file name in multi part uploads #617

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/clj_http/multipart.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@

(defmethod make-multipart-body File
;; Create a FileBody object from the given map, requiring at least :content
[{:keys [^String name ^String mime-type ^File content ^String encoding]}]
[{:keys [^String name ^String mime-type ^File content ^String encoding ^String file-name]}]
(cond
(and name mime-type content encoding)
(FileBody. content (ContentType/create mime-type encoding) name)
(FileBody. content (ContentType/create mime-type encoding) (or file-name name))

(and mime-type content encoding)
(FileBody. content (ContentType/create mime-type encoding))
(FileBody. content (ContentType/create mime-type encoding) file-name)

(and name mime-type content)
(FileBody. content (ContentType/create mime-type) name)
(FileBody. content (ContentType/create mime-type) (or file-name name))

(and mime-type content)
(FileBody. content (ContentType/create mime-type))
(FileBody. content (ContentType/create mime-type) file-name)

content
(FileBody. content)
(FileBody. content nil file-name)

:else
(throw (Exception. "Multipart file body must contain at least :content"))))
Expand Down
11 changes: 10 additions & 1 deletion test/clj_http/test/multipart_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,16 @@
(is (= "file-body" (body-mime-type body)))
(is (= (Charset/forName "ascii") (body-charset body)))
(is (= test-file (.getFile body) ))
(is (= "testname" (.getFilename body)))))))
(is (= "testname" (.getFilename body)))))

(testing "the :file-name key overrides the :name key"
(let [test-file (File. "testfile")
body (make-multipart-body {:content test-file
:name "testname"
:file-name "foo.txt"})]
(is (instance? FileBody body))
(is (= test-file (.getFile body) ))
(is (= "foo.txt" (.getFilename body)))))))

(deftest test-multipart-content-charset
(testing "charset is nil if no multipart-charset is supplied"
Expand Down