|
3 | 3 | // License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
4 | 4 |
|
5 | 5 | import getOdooService from './get_odoo_service';
|
6 |
| -import getOdooSession from './get_odoo_session'; |
| 6 | +import save2file from '@terminal/utils/save2file'; |
7 | 7 |
|
8 | 8 | export type GetContentOnErrorCallback = (error: {[string]: mixed}) => void;
|
9 | 9 |
|
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, |
19 | 43 | });
|
| 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 | + } |
20 | 61 | }
|
0 commit comments