Search code examples
scalaakkaakka-streamakka-http

How to send a file as a response using akka http?


I am a little new to akka world, so my domain of knowledge is a bit small. I am creating an https server and handling it using akka streams and http, for a specific url, i need to send a file back to client. How can i achieve that using akka streams and avoiding the akka routes.

def handleCall(request:HttpRequest):HttpResponse = {
  logger.info("Request is {}",request)
  val uri:String = request.getUri().path()
  if(uri == "/download"){
    val f = new File("/1000.txt")
    logger.info("file download")
    return HttpEntity(
    //What should i put here if i want to return a text file.
    )
}

Solution

  • If the file is likely to be large then you don't want to consume the entire contents into memory before sending it to the client. This is solved via a purely stream based solution:

    import scala.io
    import akka.stream.scaladsl.Source
    import akka.http.scaladsl.model.HttpEntity.{Chunked, ChunkStreamPart}
    import akka.http.scaladsl.model.{HttpResponse, ContentTypes}
    
    val fileContentsSource : (String, String) => Source[ChunkStreamPart, _] =
      (fileName, enc) =>
        Source
          .fromIterator( io.Source.fromFile(fileName, enc).getLines )
          .map(ChunkStreamPart.apply)
    
    
    val fileEntityResponse : (String, String) => HttpResponse =
      (fileName, enc) => 
        HttpResponse(entity = Chunked(ContentTypes.`text/plain(UTF-8)`,
                                      fileContentsSource(fileName, enc)))
    

    Now you can create and send an HttpResponse without having the server keep the entire contents:

    val httpResp : HttpResponse = fileEntityResponse("/foo/log.txt", "UTF8")