[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) <len@odoo.com>
This commit is contained in:
Fanny He
2019-08-30 14:29:09 +00:00
committed by Nans Lefebvre
parent 52f6e38cea
commit df85ddc28f
@@ -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()