I need to send temporary file content with webflux...
# spring
r
I need to send temporary file content with webflux and coroutines, but I want do delete the file after that. Where can I run
zipFile.delete()
? Currently I have this:
Copy code
suspend fun download(request: ServerRequest): ServerResponse {
	val zipFile = // get the temporary <http://java.io|java.io>.File from a different service
	ServerResponse.ok().contentLength(zipFile.length())
	.contentType(MediaType.parseMediaType("application/zip"))
        .header("Content-Disposition", "attachment; filename=\"file.zip\"")
        .bodyValueAndAwait(FileSystemResource(zipFile))
}
Is there any callback I can use? I don't want to read the file into temporary
ByteArray
.
I've tried this and I'm not quite sure why it's not working (I'm getting
java.nio.file.NoSuchFileException
) :
Copy code
suspend fun download(request: ServerRequest): ServerResponse {
	val zipFile = // get the temporary <http://java.io|java.io>.File from a different service
	val reponse = ServerResponse.ok().contentLength(zipFile.length())
		.contentType(MediaType.parseMediaType("application/zip"))
        .header("Content-Disposition", "attachment; filename=\"file.zip\"")
        .bodyValueAndAwait(FileSystemResource(zipFile))
    zipFile.delete()
    response
}
d
Have you tried
.bodyValueAndAwait(FileSystemResource(zipFile)).also { zipFile.delete() }
r
it doesn't work (the same NoSuchFileException)
Found this https://stackoverflow.com/a/37326148 but it's not exactly the solution I would expect ...
And it doesn't work - the file is not deleted.
This is my horrible but working solution so far 😉
Copy code
.bodyValueAndAwait(FileSystemResource(zipFile)).also {
    GlobalScope.launch {
         delay(5000)
         zipFile.delete()
    }
}
d
Hmm nah ... this gonna be flaky
r
Since its on Linux, it seems to work fine, because file can be deleted on Linux during read process. It seems to be processed on a very low level - I'm not sure if it is "zero-copy".