Play framework - Stream file response

1.5k Views Asked by At

I have an action which intends to return a file describing records persisted in some database (Cassandra). Since many records might be described I rather not store the entire representations on memory before creating the response. My current solution is creating a file on the file system written using FileWriter and once the file computation is finished returning it as:

val responseFile = new File(records_file_path)
val fileContent = Enumerator.fromFile(responseFile)

Ok.sendFile(
  content = responseFile,
  fileName = _ => "records_descriptions.csv"
)

Is there a nicer way to do it without the need to persist a file on the disk ?
I was thinking about something like ChunkedResult maybe ?
I'm just not sure how to create the output stream to write the DB records into without the entire thing to be loaded on the heap at the same time

1

There are 1 best solutions below

0
On

The solution was to create an Enumerator instead of a stream.
I created the Enumerator using the map method on the Seq returned from the Databse and on the Controller side used Ok.chunked(resultsEnumerator)