Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a feature whereby users can validate file size before being uploaded. #380

Open
aayobam opened this issue Dec 6, 2023 · 3 comments
Open
Assignees

Comments

@aayobam
Copy link

aayobam commented Dec 6, 2023

Feature request for Cloudinary Python SDK

Add a feature whereby users can validate file size before being uploaded.

Explain your use case

I want the cloudinary python sdk team to ensure that users can check or validate the filesize of a file before being uploaded just like in native django file upload without cloudinaryfile field.

Describe the problem you’re trying to solve

I am a backend web developer and i use python and django a lot. for backend web development. Initially in a django project when uploading a file, one could determine or validate the file size(bytes, kb, mb, gb) before it's being uploaded to the database . But recently i tried achieve that with CloudinaryField and i got the error that the file field doesnt have attribute "file.size".
A typical django file upload validation without the use of CloudinaryField looks like the below code.

import mimetypes
from django.db import models
from django.core.exceptions import ValidationError
from django.core.files.storage import FileSystemStorage

def file_validation(file):
    FILE_UPLOAD_MAX_MEMORY_SIZE = 1024 * 1024 * 1  # 1mb
    file_types = ["image/png", "image/jpeg", "image/jpg", "application/pdf"]

    if not file:
        raise ValidationError("No file selected.")

    if file.size > FILE_UPLOAD_MAX_MEMORY_SIZE:
        raise ValidationError("File shouldn't be larger than 1MB.")

    fs = FileSystemStorage()
    filename = fs.save(file.name, file)
    file_type = mimetypes.guess_type(filename)[0]
    if file_type not in file_types:
        raise ValidationError(
            "Invalid file, please upload a clearer image or pdf file that shows your full name, picture and date of birth.")

class UploadFile(models):
    document = models.FileField(upload_to="media", validators=[file_validation])
    profile_picture = models.FileField(upload_to="media", validators=[file_validation], blank=True, null=True)

The above code works well but when CloudinaryField is been used, file size validation doesn't work since "size" attribute doesn't exist. I'll want this to be added and enabled to make file upload validation to cloudinary possible and seamless.

Do you have a proposed solution?

I do not have code sample to achieve this, which is why i'm suggesting this to the developers of cloudinary python sdk to resolve this or add such feature.

@wissam-khalili
Copy link
Collaborator

Hi @aayobam,
Thank you for sharing your input.
We will review it internally and will keep you posted on our findings.
Regards,
Wissam

@const-cloudinary
Copy link
Contributor

Hello @aayobam ,

We have added an example of your use case here:
https://github.com/cloudinary/cloudinary-django-sample/blob/e628cd198f819070f5020865280f49c7c3b633f7/photo_album/models.py#L34

Please let us know if it helps!

@wissam-khalili
Copy link
Collaborator

Hi @aayobam ,

I'm following up to see whether you had a chance to look into our last response.
Please let us know if you have any additional questions, or if there's anything else we can help with.

Regards,
Wissam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants