twitter/finagle

Receive HTTP file uploads as a stream

Open

#499 opened on Apr 26, 2016

View on GitHub
 (2 comments) (0 reactions) (0 assignees)Scala (1,435 forks)batch import
help wanted

Repository metrics

Stars
 (8,864 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

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
    }
}

Contributor guide