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?
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
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