Resource Virtual Filename getFilename() is missing the path?

(7 beta6)

I have images stored in a database byte{] field and can display them with

StreamResource stream = new StreamResource(source,person.getId() + ".jpg", app);
String filename = stream.getFilename();
Embedded theimage = new Embedded(filename, stream);

where source is returned by a class that implements the StreamSource Interface with the getStream method:


...
InputStream getStream() {
		return new ByteArrayInputStream(image);
}

This works fine so far, but what i want to do later is generate a generic html output where i use the html img src tag instead of the embedded component.

For this i need the complete virtual filename. With the above embedded, the image in the browser has an URL like this for example:

http://localhost:8080/TestApplication/APP/11/31.jpg

that is, a virtual filename generated by vaadin with a changing path to make it unique.

BUT when i call the stream.getFilename(), it only returns "
31.jpg
".without the vaadin-generated path. Ths is of course not enough for my “img src” html tag that should be
in this case something like [code]

[/code]

My question is, how can i get the virtual filename including its virtual (and for each image changing) path for each of the StreamResource instances?

There is unfortunately no way of doing that right now when using StreamResource. You could however instead use a RequestHandler for serving images from the database as that gives you full control over the paths.

See
this disucssion
for more information.

Thank you very much, will try that. Is there a ticket for the path issue, or will it stay like this?

Seems like there hasn’t been any ticket about this until right now when I created
ticket #10115
.

I see :slight_smile: Anyway, having read through
the above discussion
,

I understand that using the streamresource without connecting it to a component will cause memory leaks for not being garbage collected.

But then how to serve these byte database pics from the request handler without making them a streamresource?

One would have to store them from the database as temp files xyz.jpg so that img src can find them?

There is no StreamResource involved when using a RequestHandler - the handler just gets a request with parameters and can write to the outputStream of the response if it wants to, or just ignore the request if that is more appropriate.

This means that you can generate URLs like /imageFromDatabase?id=123 and then make the RequestHandler handle requests for /imageFromDatabase (based on VaadinRequest.getRequestPathInfo()) by getting the id parameter, fetching the image from the database and writing it directly to the output stream of the response (VaadinResponse.getOutputStream()) while ignoring requests that are to other paths.

Got it. Thanks a lot.