Uploading an image file directly from FTP server

Hi there,

I am still new to CUBA. Could you help me out with an idea…
I am trying to download an Image directly from FTP server using apache commons net library. I don’t want to save image to temp folder or database. How can I make userImage (image) field load the picture from a stream?

	protected void postInit() {
		super.postInit();

		FTPClient ftpClient = new FTPClient();
		try{
			ftpClient.connect(FTP_SERVER);
			ftpClient.login(FTP_USERNAME, FTP_PASSWORD);

//			File file = new File("/modules/web/themes/halo/test.jpeg"); //used for testing

			try(FileOutputStream fos = new FileOutputStream(file)){
				ftpClient.retrieveFile(FTP_IMAGE, fos);

//				userImage.setSource(StreamResource.class).getStreamSupplier(?????);
			}

		} catch (IOException io){
			io.printStackTrace();
		}
);

The other option I thought about is using CUBA functionality and load the files from temp folder. The problem there is that temp folder is in “deploy/tomcat/temp/app/” and when I try to deploy the application to a dev server I cannot specify a folder there. On localhost it is working, but deployed somewhere else - not.

Thank you in advance!

Just an idea for 2nd option, In production, there is a “work” folder you can access using path

../work

You can save the file to a byte array instead of a file, something like this:

try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
    ftpClient.retrieveFile(FTP_IMAGE, bos);
    byte[] bytes = bos.toByteArray();

    userImage.setSource(StreamResource.class)
        .setStreamSupplier(() -> new ByteArrayInputStream(bytes));
}