Introduction

This blog describes how to limit upload of files with size exceeding particular value to Alfresco repository.

Implementation

Restriction can be done in ContentServiceImpl class. Let’s extend this class in class CustomContentServiceImpl. We can add sizeLimit variable which is going to keep limit value in bytes and then use this limit in getWriter method.

package com.jmuras.alfresco.repo.content;
 
public class CustomContentServiceImpl extends ContentServiceImpl {
...
 
private Long sizeLimit;
 
public void setSizeLimit(long sizeLimit) {
    sizeLimit = sizeLimit;
}
 
@Override
public ContentWriter getWriter(NodeRef nodeRef, QName propertyQName, boolean update) {
 
        // Get content writer from parent method
        ContentWriter contentWriter = super.getWriter(nodeRef, propertyQName, update);
 
        // If size limit variable is set
        if (contentWriter instanceof AbstractContentWriter && sizeLimit != null) {
            AbstractContentWriter abstractContentWriter = (AbstractContentWriter) contentWriter;
 
            // Set size limit for content provider
            ContentLimitProvider contentLimitProvider = new ContentLimitProvider.SimpleFixedLimitProvider(sizeLimit);
            abstractContentWriter.setContentLimitProvider(contentLimitProvider);
 
            return abstractContentWriter;
        }
        return contentWriter;
    }
 
...
}

Now you have to declare new content service bean (contentService). You can do it in alfresco/patch/custom-content-services-context.xml file as follows:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
 
<beans>
 
    <bean id="customBaseContentService" class="com.jmuras.alfresco.repo.content.CustomContentServiceImpl" abstract="true"
          parent="baseContentService">
        <property name="authenticationService">
            <ref bean="authenticationService"/>
        </property>
        <property name="nodeService">
            <ref bean="nodeService" />
        </property>
 
        <!--Limit for file size in bytes-->
        <property name="sizeLimit" value="2000000"/>
    </bean>
 
    <bean id="contentServiceOrig" parent="baseContentService">
        <property name="store">
            <ref bean="fileContentStore"/>
        </property>
    </bean>
 
    <bean id="contentService" parent="customBaseContentService">
        <property name="store">
            <ref bean="fileContentStore"/>
        </property>
    </bean>
 
</beans>

Size limit variable (sizeLimit) can be also defined in alfresco-global.properties and then instead of value, e.g., 2000000 ${sizeLimit} placeholder should be used.

That change should prevent possibility of upload of files, which exceed particular size. In addition, change in Alfresco WebScript (alfresco/templates/webscripts/org/alfresco/repository/upload/upload.post.js) can be made to return appropriate error to Alfresco Share.

Existing code:

   catch (e)
   {
      // NOTE: Do not clean formdata temp files to allow for retries. It's possible for a temp file
      //       to remain if max retry attempts are made, but this is rare, so leave to usual temp
      //       file cleanup.
 
      // capture exception, annotate it accordingly and re-throw
      if (e.message && e.message.indexOf("org.alfresco.service.cmr.usage.ContentQuotaException") == 0)
      {
         e.code = 413;
      }
      else
      {
         e.code = 500;
         e.message = "Unexpected error occurred during upload of new content.";      
      }
      throw e;
   }

New code:

catch (e)
   {
      // NOTE: Do not clean formdata temp files to allow for retries. It's possible for a temp file
      //       to remain if max retry attempts are made, but this is rare, so leave to usual temp
      //       file cleanup.
 
      // capture exception, annotate it accordingly and re-throw
      if (e.message && e.message.indexOf("org.alfresco.service.cmr.usage.ContentQuotaException") == 0)
      {
         e.code = 413;
      }
      else if (e.message && e.message.indexOf("org.alfresco.repo.content.ContentLimitViolationException") == 0)
      {
         e.code = 500;
         e.message = "Your file exceeds upload limit."; 
      }
      else
      {
         e.code = 500;
         e.message = "Unexpected error occurred during upload of new content.";      
      }
      throw e;
   }