Byte Array to FileDescriptor

I am probably just missing somethings here but and help would be appreciated.

I’m getting back from a a third party API call a response that includes a Base64 encoded string. I’ve decoded the data string to a byte array. But now I’m not sure how to save this byte array as a file descriptor. I know I can write that byte array to a file but then I’m not sure how to actually bundle that into a FileDescriptor to save to external storage.

Help?

Hi,

The com.haulmont.cuba.core.app.FileStorageService#saveFile method can be used to save byte array to file storage.

@Inject
private FileStorageService fileStorageService;
@Inject
private Metadata metadata;
@Inject
private Notifications notifications;
@Inject
private TimeSource timeSource;
@Inject
private DataManager dataManager;

@Subscribe("btn")
public void onBtnClick(Button.ClickEvent event) {
    // Create a file descriptor assosiated with a file in a file storage
    FileDescriptor fileDescriptor = metadata.create(FileDescriptor.class);
    fileDescriptor.setName("test.txt");
    fileDescriptor.setExtension("txt");
    fileDescriptor.setCreateDate(timeSource.currentTimestamp());

    byte[] data = "test text".getBytes();

    try {
        fileStorageService.saveFile(fileDescriptor, data);
        dataManager.commit(fileDescriptor);
    } catch (FileStorageException e) {
        notifications.create()
                .withCaption(e.getMessage())
                .show();
    }
}

Gleb

2 Likes

What would be the maximum size allowed?

The size limitation would not be on CUBA’s side. It would be on S3’s side. The only thing being captured on CUBA’s side is the UUID of the FileDescriptor entity, the file type, and the date created. With those 3 things it can upload/retrieve pretty much any file to/from S3.

Additionally, the cuba.maxUploadSizeMb property can be used to specify the maximum file size (in megabytes) that can be uploaded using the FileUploadField and FileMultiUploadField components.

Gleb

1 Like

i was wondering if there are any defined set of configurations that could place restrictions on max file size, but @gorelov recent response just made it clear.
Thanks