Skip to content

Commit

Permalink
[MNT-24127] Added rest endpoint implementation to calculating folder …
Browse files Browse the repository at this point in the history
…size
  • Loading branch information
mohit-singh4 committed Mar 12, 2024
1 parent 2d8231e commit b0d64de
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
28 changes: 16 additions & 12 deletions remote-api/src/main/java/org/alfresco/rest/api/impl/NodesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3556,38 +3556,42 @@ public void validateProperties(Map<String, Object> properties, List<String> excl
});
}
}

/**
*
* @param folderNodeId nodeId value.
* @param nodeId nodeId value.
*/
@Override
public Map<String, Object> getFolderSize(String folderNodeId){

NodeRef nodeRef = this.validateNode(folderNodeId);
long size=this.getNodeSize(nodeRef);
public Map<String, Object> getFolderSize(String nodeId)
{
NodeRef nodeRef = validateNode(nodeId);
long size = getNodeSize(nodeRef);
int k = 1024;
String[] sizes = {"Bytes", "KB", "MB", "GB", "TB", "PB"};
int i = (int) Math.floor(Math.log(size) / Math.log(k));
float finalSize = Float.parseFloat(String.valueOf(size / Math.pow(k, i)));
DecimalFormat form = new DecimalFormat("0.00");
Map<String, Object> response = new HashMap<>();
response.put("id", folderNodeId);
response.put("id", nodeId);
response.put("size", String.valueOf(form.format(finalSize) + " " + sizes[i]));
return response;
}

protected long getNodeSize(NodeRef nodeRef){

private long getNodeSize(NodeRef nodeRef)
{
long size;
// Collecting current node size.
//Collecting current node size.
ContentData contentData = (ContentData) nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
try {
try
{
size = contentData.getSize();
} catch (Exception e) {
} catch (Exception e)
{
size = 0;
}
List<ChildAssociationRef> chilAssocsList = nodeService.getChildAssocs(nodeRef);
for (ChildAssociationRef childAssociationRef : chilAssocsList) {
for (ChildAssociationRef childAssociationRef : chilAssocsList)
{
NodeRef childNodeRef = childAssociationRef.getChildRef();
size = size + getNodeSize(childNodeRef);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void afterPropertiesSet()
*/
@Override
@WebApiDescription(title = "Size of folder",description = "Return a size of folder")
public Map<String, Object> readAll(String nodeId)
public Map<String, Object> readById(String nodeId)
{
return nodes.getFolderSize(nodeId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,6 @@ interface FolderSize<E> extends ResourceAction
*
* @param nodeId Entity resource context for this relationship.
*/
E readAll(String nodeId);
E readById(String nodeId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ else if (RelationshipResourceAction.FolderSize.class.isAssignableFrom(resource.g
{
throw new DeletedResourceException("(GET by id) "+resource.getMetaData().getUniqueId());
}
RelationshipResourceAction.FolderSize<Map<String, Object>> relationGetter = (RelationshipResourceAction.FolderSize<Map<String, Object>>) resource.getResource();
Object result = relationGetter.readAll(params.getEntityId());
RelationshipResourceAction.FolderSize<?> relationGetter = (RelationshipResourceAction.FolderSize<?>) resource.getResource();
Object result = relationGetter.readById(params.getEntityId());
return result;
}
else
Expand Down

0 comments on commit b0d64de

Please sign in to comment.