Alfresco: Calculate folder size using Java based WebScript
I was assigned to a training task to write a web script for calculating the size of a folder or a file. But you need to go through all the nodes recursively. If you don’t calculate it recursively in folders you won’t get accurate folder size.
Requirements:
- Java Development Kit 1.7 or later
- Text Editor or IDE (Eclipse/Sublime Text/Atom)
- Apache Maven 3 or later
- Web Browser (Chrome/Firefox/Safari)
For this project, I generated the Alfresco 5 All-in-One maven project. You really don’t want Alfresco Share module in this project. But I included it because you may need to find a NodeRefId. It would be easier with Share. The source code of this project is available at GitHub.
size.get.html.ftl
size.get.desc.xml
FileSizeWebScript.java
service-context.xml
Create above files in below locations of your maven project.
- size.get.desc.xml -
repo-amp/src/main/amp/config/alfresco/extension/templates/webscripts/org/dedunu/alfresco/size.get.desc.xml
- size.get.html.ftl -
repo-amp/src/main/amp/config/alfresco/extension/templates/webscripts/org/dedunu/alfresco/size.get.html.ftl
- FileSizeWebScript.java -
repo-amp/src/main/java/org/dedunu/alfresco/FileSizeWebScript.java
- service-context.xml -
repo-amp/src/main/amp/config/alfresco/module/repo-amp/context/service-context.xml
How to test the web script?
Take a terminal. Navigate to project folder. And type below command.
$ mvn clean install -Prun -Dmaven.test.skip
It may take a while to start the Alfresco Repository and Share server. Wait till it finishes completely.
Then open a web browser and go to http://localhost:8080/share. Then login. Go to Document library.
Find a folder and click on “View Details”. Then copy NodeRef from browser as shown below.
Open a new tab and type below URL. (Replace
[http://localhost:8080/alfresco/s/size?nodeRef=
If you have followed the instruction properly, you will get a page like below.
If you have any questions regarding these examples, please comment!!! Enjoy Alfresco!