Get the size of an image file loaded into an image component in cq5?

I am working on a cq5 project and I need to limit the size of the image file uploaded via the image component to around 150kb. I created a custom image component called " customimage

" by copying, pasting and renaming the default image component.

The " fileReference

" property returns the relative path to the file (for instance "/content/dam/myProject/emea/logo.jpg")

. There is no property for an absolute path. Right now I have some code written to check the file size in the customimage.jsp file. I have some code that looks like

String path = (String)properties.get("fileReference","");

File file = new File(path);

double file_size = file.length();

      

The problem is that there is not any image that I pass to the component, file_size returns 0.0. The project is in html 4, so I cannot take advantage of the excellent javascript I / O file that is added html5

, so it needs to be executed via java. What am I doing wrong? Is there any other way I should be approaching this problem in cq5?

+3


source to share


1 answer


The fileReference attribute will give you the path to the image in the jcr repository, presumably the dam, not the file system path, meaning the file constructor you created won't work.

Untested, but something like the following may work.



Resource r = resourceResolver.getResource(path);
Asset a = r.adaptTo(Asset.class);
Rendition rnd = a.getOriginal();
long size = rnd.getSize();

      

+3


source







All Articles