I want to implement a Liferay Portlet that downloads a ~1GB file from a separate server, and serves it to the website visitor who clicked the link.
The file must be streamed in a memory-efficient way (so no loading everything into memory), and the user should see the download progress shortly after clicking (so no storing everything onto the local disk).
I must use WebClient because it seems to be the standard for making web requests within Liferay 7 (RestTemplate will be deprecated).
I started writing something like this, inspired by an example from the javadoc:
Mono<DataBuffer> bodyMono = client.get()
.uri("https://theotherserver.com/file94875.pdf")
.retrieve()
.bodyToMono(DataBuffer.class);
... which I would feed into the portlet's MVCResourceCommand.serveResource()
via PortletResponseUtil.sendFile
, which expects a java.io.InputStream
.
Unfortunately WebClient gives me a Mono<DataBuffer>
(or Flux<DataBuffer>
), and another answer claims that reconstructing the InputStream defeats the purpose of using WebClient in the first place.
What would be the most efficient and WebClient-savvy way to implement this?