Reputation: 2260
I have an application where some images will be dynamically created based on user input and I need to send those images back to the user's browser. Once the user leaves the page the images are on, the images won't be needed anymore, so I just need them around long enough to serve them.
I understand how to use Ok(sendFile(...))
or Ok(stream(...))
within a controller to send the image data, but what I can't wrap my head around is how I'd specify the path for the src
in the img
tag. More explicitly, I presume I will have something like
<img [email protected](...) />
somewhere in my template.
Ideally, I wouldn't have to create temp files to hold the images since that's an awful lot of input/output that might not need to happen, although it does seem to be the easiest way to get a unique identifier that I could pass to the serveImage
controller.
If I try to serve the images from memory, how would I provide a unique id for the image, make sure that it's available in memory when the web browser asks for it, and get rid of it when it's no longer needed?
Thanks! Todd
Upvotes: 3
Views: 834
Reputation: 18446
Generating unique ids is quite simple.
The "serve the images from memory, [...] get rid of it when it's no longer needed" part sounds a lot like caching to me. Play provides a Caching API for that.
Upvotes: 3