Powered By Blogger

Sunday, December 5, 2010

Code to get the file size in Alfresco

public NodePropertyResolver resolverSize = new NodePropertyResolver() {
private static final long serialVersionUID = 1273541660444385276L;

public Object get(Node node) {
ContentData content = (ContentData)node.getProperties().get(ContentModel.PROP_CONTENT);
return (content != null ? new Long(content.getSize()) : 0L);
}
};


SO get the content data from the node. From the content Data extract the size of the file.

3 comments:

  1. Do you know a way to find the version history size of a node?

    ReplyDelete
  2. I finally figured it out with a mix of this post and your other post about version history...

    --------------------------
    The resulting code :
    --------------------------

    protected void generateNodeVersionHistorySize(ScriptNode node)
    {
    Context cx = Context.enter();
    Scriptable scope = cx.initStandardObjects();
    if(node.getIsContainer()){
    this.historyCheckedSpace++;
    List childRefs = this.services.getNodeService().getChildAssocs(node.getNodeRef());
    for (int i = 0; i < childRefs.size(); i++)
    {
    // create our Node representation from the NodeRef
    ScriptNode child = new ScriptNode(childRefs.get(i).getChildRef(), this.services, scope);

    generateNodeVersionHistorySize(child);
    }
    }else if(node.getIsDocument()){
    this.historyCheckedDoc++;
    if(node.getNodeRef() != null){
    VersionHistory versionHistory = versionService.getVersionHistory(node.getNodeRef());
    int versionQty = versionHistory.getAllVersions().size();
    historyTotalHistory += versionQty;
    historyTotalHistoryWhitoutCurrent += (versionQty - 1);

    ContentData contentRef = (ContentData )nodeService.getProperty(node.getNodeRef(), ContentModel.PROP_CONTENT);
    long nodeSize = (contentRef != null ? new Long(contentRef.getSize()) : 0L);
    long versionSize = 0;

    for(Version version : versionHistory.getAllVersions())
    {
    contentRef = (ContentData )nodeService.getProperty(version.getVersionedNodeRef(), ContentModel.PROP_CONTENT);
    versionSize += (contentRef != null ? new Long(contentRef.getSize()) : 0L);
    }

    historyTotalHistorySize += versionSize;
    historyTotalHistorySizeWhitoutCurrent += (versionSize - nodeSize);
    }
    }else{
    this.historyCheckedUnknown++;
    if (logger.isDebugEnabled()){
    logger.debug("getNodeVersionHistorySize > UnknownNode : " + node.getName() + "(" + node.getId() + ")");
    }
    }

    }

    ReplyDelete
  3. hmmmm Good Charles, Thanks for sharing the code

    ReplyDelete