Files
odoo_source/addons/mrp/controller/main.py
T
Nicolas Galler 757a4c7b55 [FIX] base: preserve mime type for uploaded Word documents
Behavior before the fix:

- When uploading an office 2007 attachment (.xlsx / .docx) as a regular
(non admin) user, the preview shows it as text, and the download option
by default saves it as a text document (in certain browsers, at least).
This stems from an over-aggressive check on mime type (anything that
contains 'xml' **anywhere** in the mime type is taken to be XML, but the
mime type for an Office document is
`application/vnd.openxmlformats-officedocument.wordprocessingml.document`
so it falls in the XML case.

Behavior after the fix:

- The mime type is preserved
- Mime type for XML-like documents (including HTML and SVG) continues to
be validated

opw-2352712

closes odoo/odoo#60164

X-original-commit: 2820ec7d5763a5856274709a4d2024267313106b
Related: odoo/enterprise#14168
Signed-off-by: Nicolas Lempereur (nle) <nle@odoo.com>
Signed-off-by: Nicolas Galler <nicocrm@users.noreply.github.com>
2020-10-16 11:24:15 +00:00

36 lines
1.2 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import base64
import json
import logging
from odoo import http
from odoo.http import request
from odoo.tools.translate import _
logger = logging.getLogger(__name__)
class MrpDocumentRoute(http.Controller):
@http.route('/mrp/upload_attachment', type='http', methods=['POST'], auth="user")
def upload_document(self, ufile, **kwargs):
files = request.httprequest.files.getlist('ufile')
result = {'success': _("All files uploaded")}
for ufile in files:
try:
mimetype = ufile.content_type
request.env['mrp.document'].create({
'name': ufile.filename,
'res_model': kwargs.get('res_model'),
'res_id': int(kwargs.get('res_id')),
'mimetype': mimetype,
'datas': base64.encodebytes(ufile.read()),
})
except Exception as e:
logger.exception("Fail to upload document %s" % ufile.filename)
result = {'error': str(e)}
return json.dumps(result)