Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_guess_mime_file_path(self):
mime = filetype.guess_mime(FIXTURES + '/sample.jpg')
self.assertTrue(mime is not None)
self.assertEqual(mime, 'image/jpeg')
def test_guess_mime_buffer(self):
buf = bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])
mime = filetype.guess_mime(buf)
self.assertTrue(mime is not None)
self.assertEqual(mime, 'image/jpeg')
def test_guess_mime_memoryview(self):
buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
mime = filetype.guess_mime(buf)
self.assertTrue(mime is not None)
self.assertEqual(mime, 'image/jpeg')
def test_guess_mime_buffer_invalid(self):
buf = bytearray([0xFF, 0x00, 0x00, 0x00, 0x00])
mime = filetype.guess_mime(buf)
self.assertTrue(mime is None)
data = data.lstrip(b"\x00")
if data:
data_len = len(data)
if data_len >= _NUM_SIGNATURE_BYTES:
buf = data[:_NUM_SIGNATURE_BYTES]
else:
buf = data + signature.read(_NUM_SIGNATURE_BYTES - data_len)
# Special casing .id3/.tag because extended filetype with add_type() prepends, meaning
# all mp3 would be labeled mimetype id3, while appending would mean each .id3 would be
# mime mpeg.
if path.suffix in ID3_MIME_TYPE_EXTENSIONS:
if Id3Tag().match(buf) or Id3TagExt().match(buf):
return Id3TagExt.MIME
return filetype.guess_mime(buf)
@iterate
def filetype(path):
try:
return magic.from_file(path, mime=True)
except NameError:
pass
return guess_mime(path)