twitter/finagle

Receive HTTP file uploads as a stream

Open

#499 aperta il 26 apr 2016

Vedi su GitHub
 (2 commenti) (0 reazioni) (0 assegnatari)Scala (1435 fork)batch import
help wanted

Metriche repository

Star
 (8864 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

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

Guida contributor