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_string_with_confidence(self):
"""String identification: magic_string """
ext = puremagic.magic_string(bytes(self.mp4magic))
self.assertEqual(self.expect_ext, ext[0].extension)
self.assertRaises(ValueError, puremagic.magic_string, "")
def test_magic_string_with_filename_hint(self):
"""String identification: magic_string with hint """
filename = os.path.join(OFFICE_DIR, "test.xlsx")
with open(filename, "rb") as f:
data = f.read()
ext = puremagic.magic_string(data, filename=filename)
self.assertEqual(".xlsx", ext[0].extension)
def test_string_with_confidence(self):
"""String identification: magic_string """
ext = puremagic.magic_string(bytes(self.mp4magic))
self.assertEqual(self.expect_ext, ext[0].extension)
self.assertRaises(ValueError, puremagic.magic_string, "")
async def get_mimetype(self):
"""Return the mimetype for the file."""
if self._mimetype:
return self._mimetype
try:
results = puremagic.magic_string(await self.get_file_bytes())
except puremagic.PureError:
# If no results return none
return ""
# If for some reason we get a len 0 list
if not results: # pragma: nocover
return ""
# If we only have one result use it.
if len(results) == 1: # pragma: nocover
return results[0].mime_type
# If we have multiple matches with the same confidence, pick one that
# actually has a mime_type.
confidence = results[0].confidence
results = filter(lambda x: x.confidence == confidence, results)