restDataStore and fileRef

Hi,

On the client side (a jmix app), how to get an image with the fileRef that we get with restDataStore ?
I want to show the image in the interface.

Best regards
Alexandre

Hi Alexandre,

Currently you can load an image from a remote FileStorage as follows.

Suppose you have the following image component in a view:

<image id="image" height="10em" width="10em"/>

Then you should set a StreamResource for it in the controller:

@ViewComponent
private JmixImage<Object> image;

@Subscribe
public void onInit(final InitEvent event) {
    image.setSrc(getImageStreamResource());
}

private StreamResource getImageStreamResource() {
    return new StreamResource("image", () -> {
        FileRef fileRef = getEditedEntity().getPicture();
        // Get RestClient of your REST DataStore
        RestClient restClient = restDataStoreUtils.getRestClient("service-app");
        // Download file
        Resource resource = restClient.get()
                .uri("/rest/files?fileRef={fileRef}", fileRef.toString())
                .retrieve()
                .body(Resource.class);
        try {
            return resource.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
}

Note that this code will load the whole file into the client’s memory, so don’t use it for large files.

We’ll try to provide a simpler and more reliable solution in the future, see Simplify access to FileStorage through generic REST · Issue #4119 · jmix-framework/jmix

Regards,
Konstantin

Thank you for you reply, it help me