#34704 closed New feature (duplicate)

File Size Validator

Reported by: Reza Shakeri Owned by: nobody
Component: File uploads/storage Version: 4.2
Severity: Normal Keywords: file, file validator, file validation, validator, file, file size validation
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi

I had requested a file validator (#34700), but you said I couldn't add it to its core and I have a question that can I add file size validator?
This code has been tested

@deconstructible
class FileSizeValidator:
    """
    file size validator 
    """

    def __init__(
        self,
        max_upload_file_size: int = None,
    ):
        """
        :type max_upload_file_size: int
        :param max_upload_file_size: If you want the file size to be checked,
            the file size must be in bytes,
            example: file_size=1048576 (1MB), defaults to 0, optional
        :return: If everything is OK, it will return None, otherwise it will
            return a ValidationError.
        """
        if max_upload_file_size is None:
            raise ValueError("max_upload_file_size is None, You Must Be Fill max_upload_file_size ")

        self.max_upload_file_size = max_upload_file_size

    def __call__(self, value):
        current_file = value.file
        file_size = current_file.size
        if self.max_upload_file_size is not None and file_size > self.max_upload_file_size:
            raise ValidationError("file size not valid")

    def __eq__(self, other):
        return (
            isinstance(other, self.__class__)
            and self.max_upload_file_size == other.max_upload_file_size
        )

Change History (1)

in reply to:  description comment:1 by Mariusz Felisiak, 17 months ago

Resolution: duplicate
Status: newclosed

I had requested a file validator (#34700), but you said I couldn't add it to its core and I have a question that can I add file size validator?
This code has been tested

For me, this is another (4th) duplicate of #34263. You've already mentioned the size validator there. Please write to the DevelopersMailingList if you want other opinions, and follow the triaging guidelines with regards to wontfix tickets. Don't create more duplicates of the same ticket. I'll close all of them immediately.

Note: See TracTickets for help on using tickets.
Back to Top