Skip to content

Commit

Permalink
[APPS-2564]: Added endpoint to get Folder Size.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-singh4 committed Mar 1, 2024
1 parent 2da78a9 commit c0344ff
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 19 deletions.
16 changes: 8 additions & 8 deletions remote-api/src/main/java/org/alfresco/rest/api/Nodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@
import java.util.Map;
import java.util.Set;

import org.alfresco.rest.api.model.AssocChild;
import org.alfresco.rest.api.model.AssocTarget;
import org.alfresco.rest.api.model.Document;
import org.alfresco.rest.api.model.Folder;
import org.alfresco.rest.api.model.LockInfo;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.PathInfo;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.api.model.*;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.resource.content.BasicContentInfo;
Expand Down Expand Up @@ -419,6 +412,13 @@ default DirectAccessUrl requestContentDirectUrl(NodeRef nodeRef, boolean attachm

void validateProperties(Map<String, Object> properties, List<String> excludedNS, List<QName> excludedProperties);

/**
* Get the size of Folder.
*
* @param nodeId String
* @return Map Object.
*/
Map<String, Object> getFolderSize(String nodeId);

/**
* API Constants - query parameters, etc
Expand Down
49 changes: 38 additions & 11 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 @@ -47,6 +47,10 @@
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.alfresco.model.ApplicationModel;
import org.alfresco.model.ContentModel;
import org.alfresco.model.QuickShareModel;
Expand Down Expand Up @@ -83,18 +87,8 @@
import org.alfresco.rest.api.ClassDefinitionMapper;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.QuickShareLinks;
import org.alfresco.rest.api.model.AssocChild;
import org.alfresco.rest.api.model.AssocTarget;
import org.alfresco.rest.api.model.ClassDefinition;
import org.alfresco.rest.api.model.Document;
import org.alfresco.rest.api.model.Folder;
import org.alfresco.rest.api.model.LockInfo;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.NodePermissions;
import org.alfresco.rest.api.model.PathInfo;
import org.alfresco.rest.api.model.*;
import org.alfresco.rest.api.model.PathInfo.ElementInfo;
import org.alfresco.rest.api.model.QuickShareLink;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException;
import org.alfresco.rest.framework.core.exceptions.DisabledServiceException;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
Expand Down Expand Up @@ -169,6 +163,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.extensions.surf.util.Content;
import org.springframework.extensions.webscripts.servlet.FormData;
Expand Down Expand Up @@ -3555,6 +3550,38 @@ public void validateProperties(Map<String, Object> properties, List<String> excl
});
}
}
public Map<String, Object> getFolderSize(String FolderNodeId){

NodeRef nodeRef = this.validateNode(FolderNodeId);
long size=this.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))));
Map<String, Object> response = new HashMap<>();
response.put("id",FolderNodeId);
response.put("size",String.valueOf(finalSize + " " + sizes[i]));
return response;
}

protected long getNodeSize(NodeRef nodeRef){
long size=0;

// Collecting current node size
ContentData contentData = (ContentData) nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
try {
size = contentData.getSize();
} catch (Exception e) {
size = 0;
}
List<ChildAssociationRef> chilAssocsList = nodeService.getChildAssocs(nodeRef);
for (ChildAssociationRef childAssociationRef : chilAssocsList) {
NodeRef childNodeRef = childAssociationRef.getChildRef();
size = size + getNodeSize(childNodeRef);
}
return size;
}


/**
* @author Jamal Kaabi-Mofrad
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.nodes;

import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.framework.WebApiDescription;
import org.alfresco.rest.framework.resource.RelationshipResource;
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction;
import org.alfresco.util.ParameterCheck;
import org.springframework.beans.factory.InitializingBean;

import java.util.Map;

/**
* Node
*
* - folder size
*
* @author Mohit Singh
*/
@RelationshipResource(name = "size", entityResource = NodesEntityResource.class, title = "Folder size")
public class NodeFolderSizeRelation implements
RelationshipResourceAction.FolderSize<Map<String, Object>>, InitializingBean
{
private Nodes nodes;

public void setNodes(Nodes nodes)
{
this.nodes = nodes;
}

@Override
public void afterPropertiesSet()
{
ParameterCheck.mandatory("nodes", this.nodes);
}

/**
* Folder Size - returns a size of folder.
*
* @param NodeId String id of folder - will also accept well-known alias, eg. -root- or -my- or -shared-
* Please refer to OpenAPI spec for more details !
*
* If NodeId does not exist, EntityNotFoundException (status 404).
* If NodeId does not represent a folder, InvalidArgumentException (status 400).
*/
@Override
@WebApiDescription(title = "Size of folder",description = "Return a size of folder")
public Map<String, Object> readAll(String NodeId)
{
return nodes.getFolderSize(NodeId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public class ResourceInspector
ALL_RELATIONSHIP_RESOURCE_INTERFACES.add(RelationshipResourceAction.DeleteSetWithResponse.class);

ALL_RELATIONSHIP_RESOURCE_INTERFACES.add(MultiPartRelationshipResourceAction.Create.class);
ALL_RELATIONSHIP_RESOURCE_INTERFACES.add(RelationshipResourceAction.FolderSize.class);

ALL_PROPERTY_RESOURCE_INTERFACES.add(BinaryResourceAction.Read.class);
ALL_PROPERTY_RESOURCE_INTERFACES.add(BinaryResourceAction.Delete.class);
Expand Down Expand Up @@ -291,6 +292,7 @@ private static List<ResourceMetadata> inspectRelationship(RelationshipResource a
findOperation(RelationshipResourceAction.DeleteSetWithResponse.class, DELETE, helper);

findOperation(MultiPartRelationshipResourceAction.Create.class, POST, helper);
findOperation(RelationshipResourceAction.FolderSize.class, GET, helper);

boolean noAuth = resource.isAnnotationPresent(WebApiNoAuth.class);
if (noAuth)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,13 @@ public static interface DeleteSetWithResponse extends ResourceAction
*/
public void deleteSet(String entityResourceId, Parameters params, WithResponse withResponse);
}
public static interface FolderSize<E> extends ResourceAction
{
/**
* Return the size of Folder.
*
* @param NodeId Entity resource context for this relationship.
*/
public E readAll(String NodeId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ else if (EntityResourceAction.ReadByIdWithResponse.class.isAssignableFrom(resour
CollectionWithPagingInfo<?> relations = relationGetter.readAll(params.getEntityId(),params);
return relations;
}
else if (RelationshipResourceAction.FolderSize.class.isAssignableFrom(resource.getResource().getClass()))
{
if (resource.getMetaData().isDeleted(RelationshipResourceAction.FolderSize.class))
{
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());
return result;
}
else
{
if (resource.getMetaData().isDeleted(RelationshipResourceAction.ReadWithResponse.class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1768,4 +1768,8 @@
</list>
</property>
</bean>

<bean class="org.alfresco.rest.api.nodes.NodeFolderSizeRelation">
<property name="nodes" ref="Nodes" />
</bean>
</beans>

0 comments on commit c0344ff

Please sign in to comment.