Skip to content
Snippets Groups Projects
Commit 3d3f9ed9 authored by Nicolas Lempereur's avatar Nicolas Lempereur
Browse files

[FIX] tools: fix guess_mimetype coming from libmagic

Since 5a9e1af6 our guess_mimetype accepted a `default` named argument.

This commit fix the use of library used instead of our implementation, so
we don't get an error from an unexpected argument when having python-magic
installed on a given server.

Since python-magic functions always return a default mimetype:

- empty content: application/x-empty
- text content: text/plain
- binary content: application/octet-stream

the guess_mimetype default argument is not used with python-magic
implementations.

closes #13343
opw-687354
parent 372395c7
Branches
Tags
No related merge requests found
......@@ -21,11 +21,18 @@ class test_guess_mimetype(unittest.TestCase):
def test_default_mimetype_empty(self):
mimetype = guess_mimetype('')
self.assertEqual(mimetype, 'application/octet-stream')
# odoo implementation returns application/octet-stream by default
# if available, python-magic returns application/x-empty
self.assertIn(mimetype, ('application/octet-stream', 'application/x-empty'))
def test_default_mimetype(self):
mimetype = guess_mimetype('', default='test')
self.assertEqual(mimetype, 'test')
# if available, python-magic returns application/x-empty
self.assertIn(mimetype, ('test', 'application/x-empty'))
def test_mimetype_octet_stream(self):
mimetype = guess_mimetype('\0')
self.assertEqual(mimetype, 'application/octet-stream')
def test_mimetype_png(self):
content = base64.b64decode(PNG)
......@@ -35,7 +42,8 @@ class test_guess_mimetype(unittest.TestCase):
def test_mimetype_bmp(self):
content = base64.b64decode(BMP)
mimetype = guess_mimetype(content, default='test')
self.assertEqual(mimetype, 'image/bmp')
# mimetype should match image/bmp, image/x-ms-bmp, ...
self.assertRegexpMatches(mimetype, r'image/.*\bbmp')
def test_mimetype_jpg(self):
content = base64.b64decode(JPG)
......
......@@ -153,9 +153,9 @@ else:
# magic from pypi https://pypi.python.org/pypi/python-magic/
if hasattr(magic,'from_buffer'):
guess_mimetype = lambda bin_data: magic.from_buffer(bin_data, mime=True)
guess_mimetype = lambda bin_data, default=None: magic.from_buffer(bin_data, mime=True)
# magic from file(1) https://packages.debian.org/squeeze/python-magic
elif hasattr(magic,'open'):
ms = magic.open(magic.MAGIC_MIME_TYPE)
ms.load()
guess_mimetype = ms.buffer
guess_mimetype = lambda bin_data, default=None: ms.buffer(bin_data)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment