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

Multiple files upload #157

Merged
merged 4 commits into from
May 6, 2017
Merged
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
10 changes: 9 additions & 1 deletion fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Request.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Request : Fuel.RequestConvertible {
return this
}

fun source(source: (Request, URL) -> File): Request {
fun sources(source: (Request, URL) -> Iterable<File>): Request {
val uploadTaskRequest = taskRequest as? UploadTaskRequest ?: throw IllegalStateException("source is only used with RequestType.UPLOAD")

uploadTaskRequest.apply {
Expand All @@ -140,6 +140,14 @@ class Request : Fuel.RequestConvertible {
return this
}

fun source(source: (Request, URL) -> File): Request {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we implement source call sources internally so we can save some duplications?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. source call sources and add single file into List.

sources { request, url ->
listOf(source.invoke(request, request.url))
}

return this
}

fun name(name: () -> String): Request {
this.name = name()
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,43 @@ class UploadTaskRequest(request: Request) : TaskRequest(request) {
val boundary = request.httpHeaders["Content-Type"]?.split("=", limit = 2)?.get(1) ?: System.currentTimeMillis().toHexString()

var progressCallback: ((Long, Long) -> Unit)? = null
lateinit var sourceCallback: ((Request, URL) -> File)
lateinit var sourceCallback: (Request, URL) -> Iterable<File>

var dataStream: ByteArrayOutputStream? = null
var fileInputStream: FileInputStream? = null

override fun call(): Response {
try {
val file = sourceCallback.invoke(request, request.url)
//file input
fileInputStream = FileInputStream(file)
dataStream = ByteArrayOutputStream().apply {
write("--" + boundary + CRLF)
write("Content-Disposition: form-data; name=\"" + request.name + "\"; filename=\"" + file.name + "\"")
write(CRLF)
write("Content-Type: " + URLConnection.guessContentTypeFromName(file.name))
write(CRLF)
write(CRLF)
val files = sourceCallback.invoke(request, request.url)

//input file data
fileInputStream!!.copyTo(this, BUFFER_SIZE) { writtenBytes ->
progressCallback?.invoke(writtenBytes, file.length())
}
files.forEachIndexed { i, file ->
val postFix = if (files.count() == 1) "" else "${i + 1}"

write(CRLF)
write("--$boundary")
write(CRLF)
write("Content-Disposition: form-data; name=\"" + request.name + "$postFix\"; filename=\"${file.name}\"")
write(CRLF)
write("Content-Type: " + URLConnection.guessContentTypeFromName(file.name))
write(CRLF)
write(CRLF)

//input file data
FileInputStream(file).use {
it.copyTo(this, BUFFER_SIZE) { writtenBytes ->
progressCallback?.invoke(writtenBytes, file.length())
}
}

write(CRLF)
}

request.parameters.forEach {
write("--$boundary" + CRLF)
write("Content-Disposition: form-data; name=\"" + it.first + "\"" + CRLF)
write("Content-Type: text/plain" + CRLF)
write("--$boundary")
write(CRLF)
write("Content-Disposition: form-data; name=\"${it.first}\"")
write(CRLF)
write("Content-Type: text/plain")
write(CRLF)
write(CRLF)
write(it.second.toString())
write(CRLF)
Expand All @@ -62,7 +70,6 @@ class UploadTaskRequest(request: Request) : TaskRequest(request) {
return super.call()
} finally {
dataStream?.close()
fileInputStream?.close()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,38 @@ class RequestUploadTest : BaseTestCase() {
assertThat(response?.httpStatusCode, isEqualTo(statusCode))
}

@Test
fun httpUploadWithMultipleFiles() {
var request: Request? = null
var response: Response? = null
var data: Any? = null
var error: FuelError? = null

manager.upload("/post", param = listOf("foo" to "bar"))
.sources { request, url ->
listOf(File(currentDir, "lorem_ipsum_short.tmp"),
File(currentDir, "lorem_ipsum_long.tmp"))
}
.name { "file-name" }
.responseString { req, res, result ->
request = req
response = res
val (d, err) = result
data = d
error = err
print(d)
}

assertThat(request, notNullValue())
assertThat(response, notNullValue())
assertThat(error, nullValue())
assertThat(data, notNullValue())

val string = data as String
assertThat(string, containsString("file-name1"))
assertThat(string, containsString("file-name2"))

val statusCode = HttpURLConnection.HTTP_OK
assertThat(response?.httpStatusCode, isEqualTo(statusCode))
}
}