Yet more (...) changed functionality from CUBA --> Jmix: .setSource(FDR.class)

So in CUBA, the way image previewing/etc was done (from Loading and Displaying Images - CUBA Platform. Developer’s Manual - down near the bottom), used image.setSource(FileDescriptorResource.class).setFileDescriptor(fileDescriptor) - however… in Jmix, there is no FileDescriptorResource and like so many other things, there’s no mention of this in the migration docs.

So…how’s it done now? We made heavy use of this…

We have not yet started our migration of the Cuba - Jmix application and I do not have at hand a sample project c addon migration to check…
But I planned for us use load fileDescriptor → byte[] and then:

image.setSource(StreamResource.class)
                .setStreamSupplier(() -> new ByteArrayInputStream(...))

I’ll have a look at how to do that. At least it’s a start.

I’m puzzled as to why FileDescriptorResource was removed though. Going from a FileDescriptor seems pretty natural…

Hmmm, @andrey_vb - I’m not seeing a way to convert a FileDescriptor to a byte[]! The only interesting method on FileDescriptor is getName() and that only returns the bare filename. There is no getBytes() or similar. O_o

In the migration addon I see the CubaFileStorage bean.
So something like this should work:

@Autowired
protected CubaFileStorage fileStorage;
....
byte[] fileBytes = fileStorage.loadFile(fileDescriptor);

The Javadoc for the migration addon has been published and you can see a lot of useful stuff there.

I ended up with,

        image.setSource(StreamResource.class)
                .setStreamSupplier(() -> {
                    try {
                        return localFileStorage.openStream(imageFile);
                    } catch (FileStorageException e) {
                        e.printStackTrace();
                    }
                    return null;
                });

Where localFileStorage is FileStorageAPI.

Another way is to convert FileDescriptor to FileRef using CubaFileStorage method:

@Autowired
private CubaFileStorage cubaFileStorage;

public void setImageSource(FileDescriptor fileDescriptor) {
    FileRef fileRef = cubaFileStorage.toFileRef(fileDescriptor);
    image.setSource(FileStorageResource.class).setFileReference(fileRef);
}
2 Likes