Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/typecode/contenttype.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ def is_binary(self):
self._is_binary = False
if self.is_file is True:
self._is_binary = is_binary(self.location)
if not self._is_binary:
try:
with open(self.location, "rb") as f:
if f.read(5) == b"%PDF-":
self._is_binary = True
except Exception:
pass

return self._is_binary

@property
Expand Down
7 changes: 7 additions & 0 deletions tests/test_contenttype.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,10 @@ def test_size(self):
test_dir = self.get_test_loc("contenttype/size")
result = size(test_dir)
assert result == 18

def test_is_binary_handles_pdf_signature(self):
test_dir = self.get_temp_dir()
test_file = os.path.join(test_dir, "test_pdf.pdf")
with open(test_file, "wb") as f:
f.write(b"%PDF-1.4\nSome binary content \x00\xff")
assert is_binary(test_file) is True
Loading