Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why python-magic returns wrong mime-type if file size is too small?

In case of the file size is under 5000 bytes (InMemoryUploadedFile). This code doesn't work

mime_type = magic.from_buffer(file.read(), mime=True)

It returns wrong mime_type. For example, I have a file cv.docx with 4074 bytes size. It returns a mime_type:

'application/x-empty'

instead of

'application/vnd.openxmlformats-officedocument.wordprocessingml.document'

Could you please suggest me any advices to solve this case?

like image 689
Thạnh Nguyên Avatar asked Sep 02 '25 10:09

Thạnh Nguyên


1 Answers

I had this problem as well. It's very likely not to do with the file size, because I have tested magic.from_buffer on 90 byte text/plain files as well and it returned the right value. The problem is that the file has somehow become empty. In my case, this is because the file was a stream and I had already read from the stream (remember if you read from a stream and read again, the second read will start where the first read finished -- unlike reading from the start of a file each time). This example is from flask

mime_type1 = magic.from_buffer(request.stream.read(2048), mime=True) // returns text/plain
mime_type = magic.from_buffer(request.files["file"].stream.read(2048), mime=True) // returns application/x-empty because the stream has already been read from

It's hard to exactly diagnose without seeing your earlier code but check where else you are working with the file and comment those out. You might need to do something like

file.seek(0)
mime_type = magic.from_buffer(file.read(), mime=True)
like image 56
alice Avatar answered Sep 13 '25 08:09

alice