[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) <aab@odoo.com>
Signed-off-by: Maruan Aguerdouh Mohtar (magm) <magm@odoo.com>
This commit is contained in:
Maruan Aguerdouh (magm)
2023-05-08 15:18:23 +02:00
parent a0a78f807a
commit 02dad87ba6
@@ -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) {