Skip to content

Commit b575850

Browse files
committed
fix: get_content
1 parent c1f7ac7 commit b575850

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

src/js/page/odoo/utils/get_content.mjs

+51-10
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,59 @@
33
// License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
44

55
import getOdooService from './get_odoo_service';
6-
import getOdooSession from './get_odoo_session';
6+
import save2file from '@terminal/utils/save2file';
77

88
export type GetContentOnErrorCallback = (error: {[string]: mixed}) => void;
99

10-
export default function (data_opts: {[string]: mixed}, onerror: GetContentOnErrorCallback): boolean | void {
11-
return getOdooSession()?.get_file({
12-
complete: getOdooService('web.framework')?.unblockUI,
13-
data: Object.assign({}, data_opts, {
14-
download: true,
15-
data: getOdooService('web.utils')?.is_bin_size(data_opts.data) ? null : data_opts.data,
16-
}),
17-
error: onerror,
18-
url: '/web/content',
10+
function getFilenameFromContentDisposition(contentDisposition: string | null): string | null {
11+
if (typeof contentDisposition !== 'string') {
12+
return null;
13+
}
14+
try {
15+
const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/i;
16+
const matches = contentDisposition.match(filenameRegex);
17+
if (!matches || matches.length < 1) {
18+
return null;
19+
}
20+
let filename = matches[1].replace(/['"]/g, '');
21+
// TODO: Support more charsets?
22+
if (filename.startsWith('UTF-8')) {
23+
const utf8Match = contentDisposition.match(/filename\*=UTF-8''(.+?)(?:;|$)/i);
24+
if (utf8Match && utf8Match[1]) {
25+
filename = decodeURIComponent(utf8Match[1]);
26+
}
27+
} else {
28+
filename = decodeURIComponent(filename);
29+
}
30+
31+
return filename.trim() || null;
32+
} catch (error) {
33+
console.error('Error parsing Content-Disposition:', error);
34+
}
35+
return null;
36+
}
37+
38+
export default async function (data_opts: {[string]: mixed}, onerror: GetContentOnErrorCallback): Promise<boolean | void> {
39+
const data = Object.assign({}, data_opts, {
40+
csrf_token: odoo.csrf_token,
41+
download: true,
42+
data: getOdooService('web.utils')?.is_bin_size(data_opts.data) ? null : data_opts.data,
1943
});
44+
const search_data = new URLSearchParams();
45+
for (const [key, value] of Object.entries(data)) {
46+
search_data.append(key, value);
47+
}
48+
49+
try {
50+
const response = await fetch('/web/content', {
51+
method: 'POST',
52+
body: search_data,
53+
});
54+
const filename = getFilenameFromContentDisposition(response.headers.get('Content-Disposition'));
55+
const mime = response.headers.get("content-type") || 'text/plain';
56+
// $FlowFixMe
57+
return save2file(filename || 'unnamed', mime, await response.bytes());
58+
} catch (_err) {
59+
onerror(_err);
60+
}
2061
}

0 commit comments

Comments
 (0)