[FIX] base: improve last chance for detection of mimetype

Since we return nosniff header, we need to avoid the magic mimetype.

In case of picture in compute field (eg. image_small on product),there are
no mimetype saved in column, and impossible to detect it from name/extention
because url are like: /web/image/product.product/47/image_small.

So before to arbitrary return default_mimetype, we try to detect it from the
content itself.

This commit continue to fix odoo/odoo@13193
This commit is contained in:
Jeremy Kersten
2016-08-23 21:09:20 +02:00
parent 1b2dab3f50
commit 5a9e1af64a
2 changed files with 7 additions and 5 deletions
+3 -2
View File
@@ -22,6 +22,7 @@ import openerp
import openerp.exceptions
import openerp.models
from openerp import http
from openerp.tools.mimetypes import guess_mimetype
from openerp.http import request, STATIC_CACHE, content_disposition
from openerp.modules.module import get_resource_path, get_module_path
from openerp.osv import osv, orm
@@ -295,7 +296,8 @@ class ir_http(osv.AbstractModel):
attach_mimetype = env['ir.attachment'].search_read(domain=[('res_model', '=', model), ('res_id', '=', id), ('res_field', '=', field)], fields=['mimetype'], limit=1)
mimetype = attach_mimetype and attach_mimetype[0]['mimetype']
if not mimetype:
mimetype = default_mimetype
mimetype = guess_mimetype(base64.b64decode(content), default=default_mimetype)
headers += [('Content-Type', mimetype), ('X-Content-Type-Options', 'nosniff')]
# cache
@@ -308,7 +310,6 @@ class ir_http(osv.AbstractModel):
# content-disposition default name
if download:
headers.append(('Content-Disposition', self.content_disposition(filename)))
return (status, headers, content)
+4 -3
View File
@@ -103,10 +103,11 @@ _Entry = collections.namedtuple('_Entry', ['mimetype', 'signatures', 'discrimina
_mime_mappings = (
# pdf
_Entry('application/pdf', ['%PDF'], []),
# jpg, jpeg, png, gif
# jpg, jpeg, png, gif, bmp
_Entry('image/jpeg', ['\xFF\xD8\xFF\xE0', '\xFF\xD8\xFF\xE2', '\xFF\xD8\xFF\xE3', '\xFF\xD8\xFF\xE1'], []),
_Entry('image/png', ['\x89PNG\r\n\x1A\n'], []),
_Entry('image/gif', ['GIF87a', 'GIF89a'], []),
_Entry('image/bmp', ['BM'], []),
# OLECF files in general (Word, Excel, PPT, default to word because why not?)
_Entry('application/msword', ['\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1', '\x0D\x44\x4F\x43'], [
_check_olecf
@@ -114,7 +115,7 @@ _mime_mappings = (
# zip, but will include jar, odt, ods, odp, docx, xlsx, pptx, apk
_Entry('application/zip', ['PK\x03\x04'], [_check_ooxml, _check_open_container_format]),
)
def guess_mimetype(bin_data):
def 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
@@ -140,7 +141,7 @@ def guess_mimetype(bin_data):
# if no discriminant or no discriminant matches, return
# primary mime type
return entry.mimetype
return 'application/octet-stream'
return default
try: