Skip to content

Commit e4030de

Browse files
committed
[FIX] core: square up guess_mimetype implementations
`_odoo_guess_mimetype` requires bytes upfront as `_Entry.signatures` are lists of bytes, thus `bin_data.startswith(signature)` requires that `bin_data` be `bytes`. Forthe libmagic version this is a bit more dodgy: `magic` accepts `str` (and encodes under the hood if so) for Python 2 compatibility reasons, but the `guess_mimetypes` wrapper will trigger an unhelpful warning if libmagic recognizes a zip or ole file. Update the libmagic version to reject str inputs. Part-of: odoo#238151 Related: odoo/enterprise#100985 Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
1 parent fa4307b commit e4030de

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

odoo/tools/mimetypes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ def _check_webp(data):
150150
# zip, but will include jar, odt, ods, odp, docx, xlsx, pptx, apk
151151
_Entry('application/zip', [b'PK\x03\x04'], [_check_ooxml, _check_open_container_format]),
152152
)
153-
def _odoo_guess_mimetype(bin_data, default='application/octet-stream'):
153+
def _odoo_guess_mimetype(bin_data: bytes, default='application/octet-stream'):
154154
""" Attempts to guess the mime type of the provided binary data, similar
155155
to but significantly more limited than libmagic
156156
157-
:param str bin_data: binary data to try and guess a mime type for
157+
:param bin_data: binary data to try and guess a mime type for
158158
:returns: matched mimetype or ``application/octet-stream`` if none matched
159159
"""
160160
# by default, guess the type using the magic number of file hex signature (like magic, but more limited)
@@ -189,6 +189,8 @@ def _odoo_guess_mimetype(bin_data, default='application/octet-stream'):
189189
def guess_mimetype(bin_data, default=None):
190190
if isinstance(bin_data, bytearray):
191191
bin_data = bytes(bin_data[:1024])
192+
elif not isinstance(bin_data, bytes):
193+
raise TypeError('`bin_data` must be bytes or bytearray')
192194
mimetype = magic.from_buffer(bin_data[:1024], mime=True)
193195
if mimetype in ('application/CDFV2', 'application/x-ole-storage'):
194196
# Those are the generic file format that Microsoft Office

0 commit comments

Comments
 (0)