From 02dad87ba61d2155a6ff8a5a9737c71b56e95bdb Mon Sep 17 00:00:00 2001 From: "Maruan Aguerdouh (magm)" Date: Thu, 30 Mar 2023 11:35:24 +0000 Subject: [PATCH] [FIX] web: documents can download html documents withouth error Steps to reproduce: - Install Documents App. - Inside Documents, upload a html file. - Download the file. Issue: We won't be able to download the file because an error is raised. This is because we are blocking every document that has a mimetype of "text/html" as it is the same mimetype as i.e JSON errors. Solution: We can get the headers and try to get the filename of the file without error even if they are not available, so normally the errors won't have a filename, so we check that the mimetype is "text/html" and that we have a proper filename for the file. opw-3231102 closes odoo/odoo#118990 X-original-commit: bb6ae580dbcc6c46ffa8f89f95c372d8fb9e3a07 Signed-off-by: Aaron Bohy (aab) Signed-off-by: Maruan Aguerdouh Mohtar (magm) --- addons/web/static/src/core/network/download.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/addons/web/static/src/core/network/download.js b/addons/web/static/src/core/network/download.js index ea79ab19462..a4f9b9e09a4 100644 --- a/addons/web/static/src/core/network/download.js +++ b/addons/web/static/src/core/network/download.js @@ -504,15 +504,15 @@ download._download = (options) => { xhr.responseType = "blob"; xhr.onload = () => { const mimetype = xhr.response.type; + const header = (xhr.getResponseHeader("Content-Disposition") || "").replace( + /;$/, + "" + ); + // replace because apparently we send some C-D headers with a trailing ";" + const filename = header ? parse(header).parameters.filename : null; // In Odoo, the default mimetype, including for JSON errors is text/html (ref: http.py:Root.get_response ) - // in that case, we have to assume the file is not valid, hence that there was an error - if (xhr.status === 200 && mimetype !== "text/html") { - // replace because apparently we send some C-D headers with a trailing ";" - const header = (xhr.getResponseHeader("Content-Disposition") || "").replace( - /;$/, - "" - ); - const filename = header ? parse(header).parameters.filename : null; + // in that case, in order to also be able to download html files, we check if we get a proper filename to be able to download + if (xhr.status === 200 && (mimetype !== "text/html" || filename)) { _download(xhr.response, filename, mimetype); return resolve(filename); } else if (xhr.status === 502) {