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

Multiple files upload #157

merged 4 commits into from
May 6, 2017

Conversation

babedev
Copy link
Collaborator

@babedev babedev commented May 5, 2017

What's in this PR?

Implementation of multiple files upload as mentioned in #147 . I created new method call sources so we can pass multiple files into sourceCallback.

Example

sources { request, url ->
   arrayOf(
      File(currentDir, "lorem_ipsum_short.tmp"),
      File(currentDir, "lorem_ipsum_long.tmp")
   )
}

@babedev babedev added the 🦄 enhancement Changing current behaviour, enhancing what's already there label May 5, 2017
@codecov
Copy link

codecov bot commented May 5, 2017

Codecov Report

Merging #157 into master will increase coverage by 0.58%.
The diff coverage is 87.5%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #157      +/-   ##
============================================
+ Coverage     60.72%   61.31%   +0.58%     
- Complexity      145      147       +2     
============================================
  Files            29       29              
  Lines           802      809       +7     
  Branches        143      143              
============================================
+ Hits            487      496       +9     
+ Misses          229      228       -1     
+ Partials         86       85       -1
Impacted Files Coverage Δ Complexity Δ
...n/kotlin/com/github/kittinunf/fuel/core/Request.kt 63.46% <100%> (+1.08%) 43 <1> (+1) ⬆️
.../kittinunf/fuel/core/requests/UploadTaskRequest.kt 80% <85.71%> (+7.77%) 8 <1> (+1) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b4a4dde...1383e7c. Read the comment docs.

@@ -140,6 +140,15 @@ 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.

@@ -131,7 +131,7 @@ class Request : Fuel.RequestConvertible {
return this
}

fun source(source: (Request, URL) -> File): Request {
fun sources(source: (Request, URL) -> Array<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.

What do you think about return this as Iterable<File>? This might make our API more flexible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Interesting.

write("--$boundary" + CRLF)
write("Content-Disposition: form-data; name=\"" + it.first + "\"" + CRLF)
write("Content-Type: text/plain" + CRLF)
write("--$boundary$CRLF")
Copy link
Owner

Choose a reason for hiding this comment

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

👏

}
files.forEachIndexed { i, file ->
val postFix = if (files.size == 1) "" else "${i + 1}"
fileInputStream = FileInputStream(file)
Copy link
Owner

@kittinunf kittinunf May 6, 2017

Choose a reason for hiding this comment

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

You might consider move this to L43 so it gets closer to the place that we actually use.

Also as a protip, Kotlin use function is very handy to work with anything implements closeable interface.

FileInputStream(f).use {
  //do something with inputstream
}
//automatically close the stream for us

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

wow, that's cool. I will try it.

@kittinunf
Copy link
Owner

I have left some comments. Thanks so much for helping us implementing this feature 👏

@@ -131,7 +131,7 @@ class Request : Fuel.RequestConvertible {
return this
}

fun source(source: (Request, URL) -> File): Request {
fun sources(source: (Request, URL) -> Collection<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.

Iterable is better 🙏

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, you review so fast. I will change it :).


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

Choose a reason for hiding this comment

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

Should we also make this as String interpolation?

@kittinunf
Copy link
Owner

This is awesome! Welcome @babedev for your first PR 💯 . 🚢 🇮🇹 from me.

@babedev babedev merged commit 73a9975 into kittinunf:master May 6, 2017
@babedev babedev deleted the bd/upload-files branch May 6, 2017 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🦄 enhancement Changing current behaviour, enhancing what's already there
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants