Getting immages from Meta-inf/resources directory

Hello, I recently encountered an issue that, based on my testing, seems to be related to permissions.

In my code, I am attempting different methods to access images, but I am experiencing problems when trying to load images dynamically.

Image avatarImage = new Image();

FileStorage fileStorage = fileStorageLocator.getDefault();
String path = sysFile.substring(0, 11);
String nomeFile = sysFile.substring(12);
FileRef fileRef = new FileRef("META-INF/resources/sales/", path, nomeFile);
String nomeFileDir = path + nomeFile;
SRC_PATH = "/META-INF/resources/sales/" + path + nomeFile;

// Attempt to load image dynamically
StreamResource streamResource = new StreamResource(
    nomeFile.substring(0, nomeFile.length() - 4),
    () -> getClass().getResourceAsStream(SRC_PATH)
);

// Attempt to load the same image with a static path
StreamResource streamResource1 = new StreamResource(
    "0c14c0ad-3b79-5519-fa5e-9a4cad49284a",
    () -> getClass().getResourceAsStream("/META-INF/resources/icons/0c14c0ad-3b79-5519-fa5e-9a4cad49284a.jpg")
);

avatarImage.setSrc(streamResource);
avatarImage.setSrc(streamResource1);

return avatarImage;

Issue Description

The first StreamResource (using SRC_PATH) does not work, returning a null stream, leading to the following error:

java.lang.NullPointerException: Cannot invoke "java.io.InputStream.read(byte[])" because "source" is null.

However, the second StreamResource, which uses a static path (/META-INF/resources/icons/), works perfectly.

Additionally, I have also tried accessing the file using File Storage & FileRef, but it does not work either:

FileRef fileRef = new FileRef(fileStorage.getStorageName(), path, nomeFile);

avatarImage.setSrc(new StreamResource(
    fileRef.getFileName(),
    () -> fileStorageLocator.getByName(fileRef.getStorageName()).openStream(fileRef)
));

This attempt also results in an error:

[nio-8080-exec-2] io.jmix.localfs.LocalFileStorage         : File C:\Users\utente\IdeaProjects\jmixprogettostudio\.jmix\work\filestorage\2024\09\26 not found
ERROR 2784 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[springServlet] : Servlet.service() for servlet [springServlet] threw exception
java.lang.IllegalStateException: UI instance is not available. 
It means that you are calling this method out of a normal workflow where it's always implicitly set. 
That may happen if you call the method from the custom thread without 'UI::access' or from test

Hi

The exceptions provided don’t look like access exceptions. Are you sure that the files are actually accessible at the generated path?

The only exceptions with access can be when trying to use files from the META-INF directory without prior user authentication. This is a feature of spring security. In this case, only resources from the frontend/themes directory are available.

In any case, you can refer to the examples in our online demo application. It show the recommended ways to use the image component.

Dmitriy

Thanks . I already solved the problem.