Upon a GET request, my Scotty webapp will run some computation and store its result in a temporary file, which it sends as the response using file.
Now I would like to run some cleanup (i.e. delete the temporary file) after the file has been sent. Scotty does not seem to include a way for doing so.
Is there is any functionality in WAI for achieving this?
wai gives us a function
responseStreamthat constructs a
Responseout of aStreamingBody, which is actually a functionthat, given a "write" action, and a "flush" action, finds some bytes somewhere and performs all the writing and flushing.
wai also provides us with a ready-made
responseFilefunction:but it doesn't delete the file at the end. Could we modify it in some way? It seems that we can, with the help of the responseToStream auxiliary function
that "opens up" an already constructed
Response, allowing us to tweak things.Like this:
(Note: the type of
streameris a bit mind-twisting because of all the higher-orderness.)This solution has the disadvantage that requests have to wait until the file is deleted in order to complete. Another option could be to send filepaths to some kind of concurrent queue that performed the deletions in another thread.