Skip to content

Instantly share code, notes, and snippets.

@RobertoUa
Created October 23, 2017 11:19
Show Gist options
  • Select an option

  • Save RobertoUa/34f360a9e9a1e4c2bab0abce361775fe to your computer and use it in GitHub Desktop.

Select an option

Save RobertoUa/34f360a9e9a1e4c2bab0abce361775fe to your computer and use it in GitHub Desktop.
def uploadedFile(fieldName: String): Directive1[(FileInfo, File)] =
extractRequestContext.flatMap { ctx ⇒
import ctx.executionContext
import ctx.materializer
fileUpload(fieldName).flatMap {
case (fileInfo, bytes) ⇒
val destination = File.createTempFile("akka-http-upload", ".tmp")
val uploadedF: Future[IOResult] = bytes.runWith(FileIO.toPath(destination.toPath))
onComplete(uploadedF)
.flatMap(_.flatMap(_.status) match {
case Success(_) ⇒
provide((fileInfo, destination))
case Failure(ex) ⇒
destination.delete()
failWith(ex)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment