Set upload file size limit for File and Image content types on Plone 5.2

Is there a way to limit the upload file size for files and images on Plone 5.2 (python 3)? I know this was possible with Archetypes but I cannot find a solution with Dexterity content types.
The only thing that comes to my mind is to set the upload_max_filesize parameter in the apache configuration. Would this cause any problem with the bulk upload functionality?

1 Like

This could help you but it isn't tested yet on Plone 5.2 :wink:

This is not an answer to your question, but still a very useful add on – if your users keep adding very big images. I have used it in Plone 5, but not in 5.2.

Thank you @cekk. I will give it a try! :+1:

Implement a plone.namedfile.interfaces.IPluggableImageFieldValidation named adapter. The fields validation looks for adapters with this interfaces adapting field and value. All found adapters are called.

2 Likes

Thank you @jensens this is a very good suggestion!
This was added very recently to plone.namedfile (since 5.1.0 - 2019-10-21) and should simplify things a lot. :partying_face:

Indeed, I added it :wink:

That was easy @jensens :

configure.zcml

<adapter
    factory=".form_setup.FileSizeValidator"
    name="file_size"
/>

validators.py

from zope.interface import Interface
from zope.interface import implementer
from zope.component import adapter
from plone.namedfile.interfaces import IPluggableFileFieldValidation
from plone.namedfile.interfaces import INamedFileField
from zope.schema import ValidationError


@implementer(IPluggableFileFieldValidation)
@adapter(INamedFileField, Interface)
class FileSizeValidator(object):
   
    def __init__(self, field, value):
        self.field = field
        self.value = value

    def __call__(self):
        if self.value.size > 20000000:
            raise InvalidFileSizeError(self.field)


class InvalidFileSizeError(ValidationError):
    """Exception for file size too large"""
    __doc__ = 'File size must be less than 20 MB.'

This works perfectly, but if the files are uploaded through bulk upload from folder_contents the validation is not triggered. Am I missing something or is this supposed to work with single files upload only?

2 Likes

Hmm, interesting, I would say its a bug in bulk upload, because even then validation should be triggered. Please create a bug report at Issues · plone/Products.CMFPlone · GitHub

Done! Bulk file upload is not triggering validations · Issue #2997 · plone/Products.CMFPlone · GitHub