pythondjangopython-magiclibmagic

Django with python-magic (libmagic) to verify uploaded files


I'm trying to save the mime type of an uploaded file in django. I don't need to reject certain types of files, I just need to keep track of the mime type of uploaded files. I'm doing this:

class Foo(models.Model):
    document = models.FileField(upload_to="foo", null=False)
    file_type = models.CharField(max_length=14)

    def save(self, *args, **kwargs):
        print(self.document.read()) #confirms that the file exists, and this prints a load of bytes, so it's a bytes  object
        filetype = magic.from_file(self.document.read())
        self.file_type = filetype
        return super().save(*args, **kwargs)

The problem is that filetype = magic.from_file(self.document.read()) throws the error: "ValueError: embedded null byte". The file is definitely not corrupt (in this case, it's a png, so I'm expecting image/png). from_file definitely seems to want a bytes object, and self.document.read() definitely produces bytes, so I'm not sure what the problem is...


Solution

  • From the documentation:

    >>> import magic
    >>> magic.from_file("testdata/test.pdf")
    'PDF document, version 1.2'
    >>> magic.from_buffer(open("testdata/test.pdf").read(1024))
    'PDF document, version 1.2'
    >>> magic.from_file("testdata/test.pdf", mime=True)
    'application/pdf'
    

    from_file takes a file name, or you could use from_buffer. More details python-magic.