Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to recognize whether it the file is a pdf file directly? #73

Open
pks9862728888 opened this issue Aug 10, 2020 · 4 comments
Labels

Comments

@pks9862728888
Copy link

No description provided.

@h2non
Copy link
Owner

h2non commented Aug 11, 2020

You can import and instantiate this class, then call the match() method passing the bytes buffer:
https://github.com/h2non/filetype.py/blob/master/filetype/types/archive.py#L173

@pks9862728888
Copy link
Author

pks9862728888 commented Aug 17, 2020

@h2non How to find the bytes buffer? I only have the request object containing the file. I am using Django Rest Framework.

@h2non
Copy link
Owner

h2non commented Aug 17, 2020

I guess you would have to read the file contents first, whether from the local file system or remotely (HTTP, SFTP...).

@h2non h2non added the question label Aug 17, 2020
@oz123
Copy link

oz123 commented Jan 2, 2021

Please try this:

>>> from filetype import match
>>> from filetype.types.archive import Pdf, Deb
>>> pdf = open("/home/oznt/1501006622.pdf", "rb")
>>> match(pdf, matchers=(Pdf(), Deb()))
<filetype.types.archive.Pdf at 0x7f570ca796d8>

Now, notice if you call the function again it will return None

>>> match(pdf, matchers=(Pdf(), Deb()))

If you seek(0) it works again:

>>> pdf.seek(0)
0
>>> match(pdf, matchers=(Pdf(), Deb()))
<filetype.types.archive.Pdf at 0x7f570ca32a90>

You see a new object is created.

@h2non IMHO, this is a bug: filetype should not pass silently on EOF and throw an exception here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants