Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use python-magic to get the file type of a file over the Internet?

Usually I would download it to StringIO object, then run this:

m = magic.Magic()
m.from_buffer(thefile.read(1024))

But this time , I can't download the file, because the image might be 20 Megabytes. I want to use Python magic to find the file type without downloading the entire file.

If python-magic can't do it...is the next best way to observe the mime type in the headers? But how accurate is this??

I need accuracy.

like image 313
TIMEX Avatar asked Jan 20 '23 18:01

TIMEX


2 Answers

You can call read(1024) without downloading the whole file:

thefile = urllib2.urlopen(someURL)

Then, just use your existing code. urlopen returns a file-like object, so this works naturally.

like image 98
Matthew Flaschen Avatar answered Jan 26 '23 00:01

Matthew Flaschen


If it is one of the common image formats like png of jpg, and you see the server is a reliable one, then you can use the 'Content-Type' header to give what you are looking for.

But this is not as reliable as using the portion of the file and passing it to python-magic, because if server had not identified the proper format and it might have set it to application/octet-stream. This is more common with video formats, but pictures, I think Content-Type is okay.

Sorry, I can't find any statistics or research on Content-Type's accuracy. The suggested answer of downloading only part of the file is a good option too.

like image 39
Senthil Kumaran Avatar answered Jan 25 '23 23:01

Senthil Kumaran