March 04, 2015

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 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.  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 <NodeRef> with the NodeRef you copied from Alfresco Share interface.)


If you have followed instruction properly, you will get a page like below.


If you have any questions regarding this examples, please comment!!! Enjoy Alfresco!

9 comments:

  1. Sweet!
    There seems to be no way to perform this in pure CMIS, unfortunately... so this is a great tool for Alfresco.
    I wish Alfresco would provide this feature by default within the Share interface, useful before downloading as ZIP or before syncing with CmisSync.
    Repositories tend to get bigger and bigger, and it is not easy to find what is taking a lot of space.
    Next, how about creating a tool like Ubuntu's Baobab, which shows the size of all Sites/folders of the entire repository, as a nice pie chart? :-) That would be very useful to admins who are struggling with disk space.

    ReplyDelete
    Replies
    1. Yes, I agree with you. I'll try to contribute to Alfresco Community edition and make it work. Thank you for the idea!!!

      Delete
  2. Nice Post Bro :) Thanks for sharing

    ReplyDelete
  3. Hi,

    Can anyone suggest how to get rid of below FTL error:-


    Message: 06120006 Wrapped Exception (with status template): 06120013 Error during processing of the template 'Expression fileName is undefined on line 11, column 34 in com/trafigura/environment/size.get.html.ftl.'. Please contact your system administrator.

    Exception: freemarker.core.InvalidReferenceException - Expression fileName is undefined on line 11, column 34 in com/trafigura/environment/size.get.html.ftl.
    freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:125)
    freemarker.core.Expression.getStringValue(Expression.java:118)
    freemarker.core.Expression.getStringValue(Expression.java:93)


    Thanks in advance..!!

    ReplyDelete
    Replies
    1. I think you have done a spelling mistake "fileName" in Java file. Please check whether it has correct case as well.

      Delete
  4. Hi,

    I followed what you have mentioned, but i am getting blank response to FTL file.
    below is the response i am getting from FTL:-

    Check file size

    File Name : fileName

    File Size : size

    Looks like FTL is not capturing the data from controller. Can you please help me with it.

    ReplyDelete
    Replies
    1. There is a small problem with this webpage rendering. Can you please refer raw FTL file https://gist.githubusercontent.com/dedunumax/20a17d08c6efe1f2314f/raw/1688dc6f6dd39e158d578d88139aacc44769b451/size.get.html

      Delete
  5. Hi,

    I too thought to use raw FTL for it. but after using raw FTL , it gives below error:-


    01220003 Wrapped Exception (with status template): 01220017 Error during processing of the template 'Expression fileName is undefined on line 12, column 34 in com/trafigura/environment/size.get.html.ftl.'. Please contact your system administrator.

    Can we try using another format for view like ftl,json? Anyone else got this issue as well?

    ReplyDelete