if (!StorageUtils.docIsAnImage(file)) throw new DocumentIsNotAnImageException("The file " + id + " is not an image");
//are the dimensions allowed?
//the check is delegated to the caller
String sizePattern= dimensions.toString();
try{
FileDao dao=FileDao.getInstance();
byte[] resizedImage = dao.getStoredResizedPicture( file, sizePattern);
if (resizedImage!=null) return resizedImage;
ByteArrayOutputStream fileContent = StorageUtils.extractFileFromDoc(file);
if (fileContent.toByteArray().length==0) return new byte[]{};
String contentType = getContentType(file);
String ext = contentType.substring(contentType.indexOf("/")+1);
WritebleImageFormat format;
try{
format = WritebleImageFormat.valueOf(ext);
}catch (Exception e){
format= WritebleImageFormat.png;
}
resizedImage=StorageUtils.resizeImage(fileContent.toByteArray(), format, dimensions);
//save the resized image for future requests
dao.storeResizedPicture(file, sizePattern, resizedImage);
return resizedImage;
}catch ( InvalidModelException e) {
throw new RuntimeException("A very strange error occurred! ",e);
}catch (OutOfMemoryError e){
throw new FileTooBigException();