How to solve django magic-library-bin that breaks the Railway build?

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…

Thank you!


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)

The Magic library doesn’t just check the extension. It looks inside the file to try and determine its structure, and then match that structure to the known file formats.

I could name any file with a .pdf extension, but that doesn’t mean it’s a pdf.

(Sorry, no, I don’t know how to install that library in that environment.)

Thank you. I understand. This is working and it checks even if i rename the file. I mean it is not working anymore, all the sudden it is showing an error failed to load libmagic.

That is the error I saw in Railway when the deploy broke.

I just wanted to know is there an alternative to do the same thing with file checking?