twitter/finagle

Receive HTTP file uploads as a stream

Open

#499 aberto em 26 de abr. de 2016

Ver no GitHub
 (2 comments) (0 reactions) (0 assignees)Scala (1.435 forks)batch import
help wanted

Métricas do repositório

Stars
 (8.864 stars)
Métricas de merge de PR
 (Nenhuma PRs mesclada em 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
    }
}

Guia do colaborador