From c0344fff0b22c22a978fe9a565a65dcd3a2556ba Mon Sep 17 00:00:00 2001 From: Mohit Singh Date: Fri, 1 Mar 2024 17:59:22 +0530 Subject: [PATCH] [APPS-2564]: Added endpoint to get Folder Size. --- .../java/org/alfresco/rest/api/Nodes.java | 16 ++-- .../org/alfresco/rest/api/impl/NodesImpl.java | 49 +++++++++--- .../api/nodes/NodeFolderSizeRelation.java | 77 +++++++++++++++++++ .../framework/core/ResourceInspector.java | 2 + .../RelationshipResourceAction.java | 9 +++ .../webscripts/ResourceWebScriptGet.java | 10 +++ .../alfresco/public-rest-context.xml | 4 + 7 files changed, 148 insertions(+), 19 deletions(-) create mode 100644 remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeFolderSizeRelation.java diff --git a/remote-api/src/main/java/org/alfresco/rest/api/Nodes.java b/remote-api/src/main/java/org/alfresco/rest/api/Nodes.java index 25f3c102051..d8fff43c526 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/Nodes.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/Nodes.java @@ -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; @@ -419,6 +412,13 @@ default DirectAccessUrl requestContentDirectUrl(NodeRef nodeRef, boolean attachm void validateProperties(Map properties, List excludedNS, List excludedProperties); + /** + * Get the size of Folder. + * + * @param nodeId String + * @return Map Object. + */ + Map getFolderSize(String nodeId); /** * API Constants - query parameters, etc diff --git a/remote-api/src/main/java/org/alfresco/rest/api/impl/NodesImpl.java b/remote-api/src/main/java/org/alfresco/rest/api/impl/NodesImpl.java index e7fd991eea3..0d084fb6ae3 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/impl/NodesImpl.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/impl/NodesImpl.java @@ -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; @@ -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; @@ -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; @@ -3555,6 +3550,38 @@ public void validateProperties(Map properties, List excl }); } } + public Map 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 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 chilAssocsList = nodeService.getChildAssocs(nodeRef); + for (ChildAssociationRef childAssociationRef : chilAssocsList) { + NodeRef childNodeRef = childAssociationRef.getChildRef(); + size = size + getNodeSize(childNodeRef); + } + return size; + } + /** * @author Jamal Kaabi-Mofrad diff --git a/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeFolderSizeRelation.java b/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeFolderSizeRelation.java new file mode 100644 index 00000000000..212ec03e907 --- /dev/null +++ b/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeFolderSizeRelation.java @@ -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 . + * #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>, 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 readAll(String NodeId) + { + return nodes.getFolderSize(NodeId); + } + +} diff --git a/remote-api/src/main/java/org/alfresco/rest/framework/core/ResourceInspector.java b/remote-api/src/main/java/org/alfresco/rest/framework/core/ResourceInspector.java index 146450667e9..7aa02749d5b 100644 --- a/remote-api/src/main/java/org/alfresco/rest/framework/core/ResourceInspector.java +++ b/remote-api/src/main/java/org/alfresco/rest/framework/core/ResourceInspector.java @@ -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); @@ -291,6 +292,7 @@ private static List 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) diff --git a/remote-api/src/main/java/org/alfresco/rest/framework/resource/actions/interfaces/RelationshipResourceAction.java b/remote-api/src/main/java/org/alfresco/rest/framework/resource/actions/interfaces/RelationshipResourceAction.java index 071641503fc..3d94f718b0c 100644 --- a/remote-api/src/main/java/org/alfresco/rest/framework/resource/actions/interfaces/RelationshipResourceAction.java +++ b/remote-api/src/main/java/org/alfresco/rest/framework/resource/actions/interfaces/RelationshipResourceAction.java @@ -163,4 +163,13 @@ public static interface DeleteSetWithResponse extends ResourceAction */ public void deleteSet(String entityResourceId, Parameters params, WithResponse withResponse); } + public static interface FolderSize extends ResourceAction + { + /** + * Return the size of Folder. + * + * @param NodeId Entity resource context for this relationship. + */ + public E readAll(String NodeId); + } } diff --git a/remote-api/src/main/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptGet.java b/remote-api/src/main/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptGet.java index 87ee84c074f..f0cf1ef9d53 100644 --- a/remote-api/src/main/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptGet.java +++ b/remote-api/src/main/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptGet.java @@ -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> relationGetter = (RelationshipResourceAction.FolderSize>) resource.getResource(); + Object result = relationGetter.readAll(params.getEntityId()); + return result; + } else { if (resource.getMetaData().isDeleted(RelationshipResourceAction.ReadWithResponse.class)) diff --git a/remote-api/src/main/resources/alfresco/public-rest-context.xml b/remote-api/src/main/resources/alfresco/public-rest-context.xml index f5627ad8239..c182bd0c987 100644 --- a/remote-api/src/main/resources/alfresco/public-rest-context.xml +++ b/remote-api/src/main/resources/alfresco/public-rest-context.xml @@ -1768,4 +1768,8 @@ + + + +