finagle icon indicating copy to clipboard operation
finagle copied to clipboard

Receive HTTP file uploads as a stream

Open tindzk opened this issue 8 years ago • 2 comments

I am currently porting an akka-http project to Finagle. One feature I liked about akka-http is that it exposes file uploads as a stream. In Finagle, large files may get written to disk. Furthermore, the interface for accessing the content of file uploads is slightly inconvenient, requiring me to use pattern matching several times:

val r = req.multipart.flatMap(
  _.files.collectFirst {
    case (k, v) if k == "file" => v
  }.flatMap(_.headOption))

val file = r.map match {
  case d: http.exp.Multipart.OnDiskFileUpload   => d.content
  case m: http.exp.Multipart.InMemoryFileUpload =>
    m.content match {
      case Buf.ByteArray.Owned(bytes, _, _) =>
        val f = File.createTempFile("upload", ".tmp")
        val fw = new FileOutputStream(f)
        fw.write(bytes)
        fw.close()
        f
    }
}

tindzk avatar Apr 26 '16 10:04 tindzk

Hello @tindzk! It's really great to hear you're migrating to Finagle.

I'm actually the person who is fully responsible for that multipart API we have today. So let me give some insights on why it works that way. First of all, sorry if that API was churn to use, but the trade off was made intentionally.

This is the second iteration of the multipart API and as you can see it's still under exp package, which indicates that we're not sure it's good enough to be widely adopted. Originally this API was introduced when we're migrating internally from Netty types to Finagle types to unblock ourselves for Netty 4 migration. At that time, Finagle didn't support multipart requests so people used Netty API directly and we decided to wrap it with out own thing to 1) migrate people away 2) make sure we didn't break anything by providing almost 1-to-1 mapping between API and keeping the behaviour unchanged.

We have an internal ticket to track the progress on redesigning this API and making that more useful and safe (and support streaming?), but it's not in the top of our backlog right now (Netty 4 migration is).

Hope that helps. Welcome to the Finagle community!

vkostyukov avatar Apr 26 '16 16:04 vkostyukov

@tindzk Also, take a look at Finatra or Finch since you're moving from akka-http. They are both built on top of Finagle and Twitter Server.

taylorleese avatar Apr 27 '16 06:04 taylorleese