From df85ddc28f3fd3a23abf0d964c020409c524daa6 Mon Sep 17 00:00:00 2001 From: Fanny He Date: Fri, 30 Aug 2019 06:50:51 +0000 Subject: [PATCH] [FIX] website_sale_digital: download file.extension when name has no extension When selling a digital file on the web shop, if the file 'name' has no extension (e.g. the pdf "Free Booklet"), the buyer will download "Free Booklet" even when the mimetype is application/pdf). This causes issues on Mac, as pdf files without extension are renamed with .dms extensions. We follow what is done in ir_http's binary_content for the extension management, using the new one if provided in the name, then the original filename extension if it exists, and we fallback on guessing it from the mimetype. OPW 2057302 closes odoo/odoo#36294 Signed-off-by: Nans Lefebvre (len) --- addons/website_sale_digital/controllers/main.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/addons/website_sale_digital/controllers/main.py b/addons/website_sale_digital/controllers/main.py index c9708912a67..3a5b1434ddb 100644 --- a/addons/website_sale_digital/controllers/main.py +++ b/addons/website_sale_digital/controllers/main.py @@ -3,6 +3,8 @@ import base64 import io +import os +import mimetypes from werkzeug.utils import redirect from odoo import http @@ -71,7 +73,7 @@ class WebsiteSaleDigital(CustomerPortal): # Check if this is a valid attachment id attachment = request.env['ir.attachment'].sudo().search_read( [('id', '=', int(attachment_id))], - ["name", "datas", "file_type", "res_model", "res_id", "type", "url"] + ["name", "datas", "datas_fname", "mimetype", "res_model", "res_id", "type", "url"] ) if attachment: @@ -105,6 +107,11 @@ class WebsiteSaleDigital(CustomerPortal): return request.not_found() elif attachment["datas"]: data = io.BytesIO(base64.standard_b64decode(attachment["datas"])) - return http.send_file(data, filename=attachment['name'], as_attachment=True) + # we follow what is done in ir_http's binary_content for the extension management + extension = os.path.splitext(attachment["datas_fname"] or '')[1] + extension = extension if extension else mimetypes.guess_extension(attachment["mimetype"] or '') + filename = attachment['name'] + filename = filename if os.path.splitext(filename)[1] else filename + extension + return http.send_file(data, filename=filename, as_attachment=True) else: return request.not_found()