Antek
Antek

Reputation: 650

Akka HTTP - How to delete file after download?

When using directive getFromFile (documented here: https://doc.akka.io/docs/akka-http/current/routing-dsl/directives/file-and-resource-directives/getFromFile.html#description)

How to handle finished download and delete file after download? Eventually if it's not possible when using this directive - how to serve file in other way and delete it after download?

Upvotes: 2

Views: 582

Answers (1)

Bill Duncan
Bill Duncan

Reputation: 430

As far as I'm aware there is nothing out of the Akka-Http box to do this. However, if you stream the file then it can deleted once the stream has finished.

            val source = FileIO.fromPath(file.toPath)
              .watchTermination() { case (_, result) =>
                result.onComplete(_ => file.delete())
              }
            complete(HttpEntity(ContentTypes.`application/octet-stream`, source))

Upvotes: 1

Related Questions