I am sending a file in play application response. When the client downloads the file, i am cleaning the file from the local server. I am achieving this using below code:
val fileToServe = TemporaryFile(new File(fileName))
Ok.sendFile(fileToServe.file, onClose = () => { fileToServe.clean })
But when client disconnects the connection, the temporary file remains in the local server. But i want to handle this disconnection and clean up the temporary file. I heard about onDoneEnumerating() but couldn't use it.
Can anyone point me out the easiest way to handle the disconnection and clean up the temporary file from local server?
TemporaryFile is for when Play receives a data stream that has to be kept in a temporary file location, rather than you sending a file out. It removes on finalization (pre 2.6) or on phantom file reference (2.6.x).
The easiest way to catch the disconnection is to call Files.deleteIfExists
in the onClose block. If that doesn't seem to be working for some reason, you can use the temporary file reaper:
https://www.playframework.com/documentation/2.6.x/ScalaFileUpload#Cleaning-up-temporary-files
that will clean out the temp files directory every so often.