I am using a validator to check the file content upon upload. Saw a youtube tutorial that looked ok and it is working on localhost. But they i tried to deploy to Railway and it showed an error that basically said that it can not find that django-magic-bin library although it was in the requirements.txt and was working fine.
Unfortunally i don’t have that build log error now because i deleted the upload, but i am sending the code i used for that validator, and that brings me to a question:
Can you please help me create this kind of a function but without using any library?
I want to believe there must be a way for Django to check the file extention content so it prevents uploading masked or renamed files…
import magic pdf_ext_validator = FileExtensionValidator(['pdf']) def validate_file_mimetype(file): accept = ['application/pdf'] file_mime_type = magic.from_buffer(file.read(1024), mime=True) print(file_mime_type) if file_mime_type not in accept: raise ValidationError("Unsupported file type") class Pdf(models.Model): title = models.CharField(max_length=100, null=True, blank=True) file = models.FileField(upload_to=course_file_upload_path, validators=[pdf_ext_validator]) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True)