I want to resize uploaded images to a small version and thumbnail version. Therefore I am using the great module S3. This is working fine and all images are uploaded to s3.
But after a few uploads I realised that the resized images are stored on the disk as well and are not deleted.
My code:
public static Picture save(File file, Recipe recipe){
File picture = new File(UUID.randomUUID().toString());
//Resize main picture
Images.resize(file, picture, 600, 600, true);
File thumbNail = new File(UUID.randomUUID().toString());
//Resize thumbnail
Images.resize(file, thumbNail, 260, 190, true);
Picture pic = new Picture();
pic.pictureFileName = file.getName();
pic.picture = new S3Blob();
pic.thumbPictureFileName = file.getName();
pic.thumbPicture = new S3Blob();
pic.recipe = recipe;
try{
pic.picture.set(new FileInputStream(picture), MimeTypes.getContentType(file.getName()));
pic.thumbPicture.set(new FileInputStream(thumbNail), MimeTypes.getContentType(file.getName()));
pic.save();
}catch(FileNotFoundException e){
Logger.error(e, "FileNotFound");
}
return pic;
}
How can I delete the files after uploading? Normaly I would wait for the stream when its finished but I dont know when its finished and so I cant delete the file.
As Samuel commented I was expecting an async behaviour which is not correct.
After resizing and uploading the pictures and just delete them with file.delete() and thats it!