[FIX] tools: properly guess image/svg+xml mimetype with magic lib
Magic return image/svg instead of image/svg+xml if file starts with <svg + refactor the import/else to if/else to avoid pylint error: function already defined line 137 (E0102) at odoo/odoo/tools/mimetypes.py:181 That raises an error during CI/runbot closes odoo/odoo#77193 X-original-commit: b76f13853a25a93307db2bcafb6a83ad75b92244 Signed-off-by: Jérémy Kersten (jke) <jke@openerp.com> Signed-off-by: Quentin Smetz (qsm) <qsm@odoo.com>
This commit is contained in:
committed by
Jeremy Kersten
parent
5208c68f0c
commit
0e293f1b7e
+19
-7
@@ -5,6 +5,7 @@ Mimetypes-related utilities
|
||||
# TODO: reexport stdlib mimetypes?
|
||||
"""
|
||||
import collections
|
||||
import functools
|
||||
import io
|
||||
import logging
|
||||
import re
|
||||
@@ -133,7 +134,7 @@ _mime_mappings = (
|
||||
# zip, but will include jar, odt, ods, odp, docx, xlsx, pptx, apk
|
||||
_Entry('application/zip', [b'PK\x03\x04'], [_check_ooxml, _check_open_container_format]),
|
||||
)
|
||||
def guess_mimetype(bin_data, default='application/octet-stream'):
|
||||
def _odoo_guess_mimetype(bin_data, default='application/octet-stream'):
|
||||
""" Attempts to guess the mime type of the provided binary data, similar
|
||||
to but significantly more limited than libmagic
|
||||
|
||||
@@ -166,17 +167,28 @@ try:
|
||||
import magic
|
||||
except ImportError:
|
||||
magic = None
|
||||
else:
|
||||
# There are 2 python libs named 'magic' with incompatible api.
|
||||
|
||||
if magic:
|
||||
# There are 2 python libs named 'magic' with incompatible api.
|
||||
# magic from pypi https://pypi.python.org/pypi/python-magic/
|
||||
if hasattr(magic,'from_buffer'):
|
||||
guess_mimetype = lambda bin_data, default=None: magic.from_buffer(bin_data, mime=True)
|
||||
if hasattr(magic, 'from_buffer'):
|
||||
_guesser = functools.partial(magic.from_buffer, mime=True)
|
||||
# magic from file(1) https://packages.debian.org/squeeze/python-magic
|
||||
elif hasattr(magic,'open'):
|
||||
elif hasattr(magic, 'open'):
|
||||
ms = magic.open(magic.MAGIC_MIME_TYPE)
|
||||
ms.load()
|
||||
guess_mimetype = lambda bin_data, default=None: ms.buffer(bin_data)
|
||||
_guesser = ms.buffer
|
||||
|
||||
def guess_mimetype(bin_data, default=None):
|
||||
mimetype = _guesser(bin_data)
|
||||
# upgrade incorrect mimetype to official one, fixed upstream
|
||||
# https://github.com/file/file/commit/1a08bb5c235700ba623ffa6f3c95938fe295b262
|
||||
if mimetype == 'image/svg':
|
||||
return 'image/svg+xml'
|
||||
return mimetype
|
||||
else:
|
||||
guess_mimetype = _odoo_guess_mimetype
|
||||
|
||||
|
||||
def neuter_mimetype(mimetype, user):
|
||||
wrong_type = 'ht' in mimetype or 'xml' in mimetype or 'svg' in mimetype
|
||||
|
||||
Reference in New Issue
Block a user