Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django python-magic identify ppt,docx,word uploaded file as 'application/zip'

I am trying to identify the file type of the files uploaded, after searching, I plan to use the python-magic to check the mime types of a file.

The FileField is used in my models, a ModelForm is used to help save the files.

After all the files have been uploaded, I check the mime type in my python shell

I find that using

magic.from_file("path_to_the_file", mime=True)

woud give the expected mime type for image,txt,pdf files that have been saved.

However, for all the docx, ppt, excel files, it would identify them as 'application/zip'

Can anyone explain why this is happening(the django auto save the ms files as zip??). And is there any good way to make the magic identify the docx, ppt, excel files as they are?

Thank you very much.

like image 653
Mona Avatar asked Jul 07 '26 21:07

Mona


1 Answers

I too came across this issue recently. Python-magic uses the Unix command file which uses a database file to identify documents (see man file). By default this database does not include instructions on how to identify .docx, .pptx, and .xlsx file types.

You can give additional information to file command to identify these types by adding instructions to /etc/magic (see https://serverfault.com/a/377792).

This should then work:

magic.from_file("path_to_the_file.docx", mime=True)

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

One thing to note from the python-magic usage instruction on GitHub - this does not seem work for .docx, .pptx, and .xlsx file types (with the additional information in /etc/magic):

magic.from_buffer(open("testdata/test.pdf").read(1024), mime=True)

Returns 'application/zip'

It seems you need to give it more data to correctly identify these file types:

magic.from_buffer(open("testdata/test.pdf").read(2000), mime=True)

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

I'm not sure of the exact amount needed.

like image 154
Sion Avatar answered Jul 10 '26 19:07

Sion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!