From e792cf10fa122f76f32a54962b8b2a100783bca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 18 May 2021 22:08:59 +0200 Subject: [PATCH] Support Binary field in CSV import/export issue10350 review346101002 --- CHANGELOG | 2 ++ src/common.js | 27 +++++++++++++++++++++++++++ src/rpc.js | 23 ++--------------------- src/window.js | 3 +++ 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index aa8682cc11..a232a534d4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Support Binary field in CSV import/export + Version 6.0.0 - 2021-05-03 * Bug fixes (see mercurial logs for details) * Use appropriate types for email and url widgets diff --git a/src/common.js b/src/common.js index a49513d87c..43de84d9c3 100644 --- a/src/common.js +++ b/src/common.js @@ -451,6 +451,33 @@ return Sao.TimeDelta(null, seconds); }; + Sao.common.btoa = function(value) { + var strings = [], chunksize = 0xffff; + // JavaScript Core has hard-coded argument limit of 65536 + // String.fromCharCode can not be called with too many + // arguments + for (var j = 0; j * chunksize < value.length; j++) { + strings.push(String.fromCharCode.apply( + null, value.subarray( + j * chunksize, (j + 1) * chunksize))); + } + return btoa(strings.join('')); + }; + + Sao.common.atob = function(value) { + // javascript's atob does not understand linefeed + // characters + var byte_string = atob(value.base64.replace(/\s/g, '')); + // javascript decodes base64 string as a "DOMString", we + // need to convert it to an array of bytes + var array_buffer = new ArrayBuffer(byte_string.length); + var uint_array = new Uint8Array(array_buffer); + for (var j=0; j < byte_string.length; j++) { + uint_array[j] = byte_string.charCodeAt(j); + } + return uint_array; + }; + Sao.common.ModelAccess = Sao.class_(Object, { init: function() { this.batchnum = 100; diff --git a/src/rpc.js b/src/rpc.js index 03d9a5593a..b9a8b3fdba 100644 --- a/src/rpc.js +++ b/src/rpc.js @@ -195,17 +195,7 @@ value = Sao.TimeDelta(null, value.seconds); break; case 'bytes': - // javascript's atob does not understand linefeed - // characters - var byte_string = atob(value.base64.replace(/\s/g, '')); - // javascript decodes base64 string as a "DOMString", we - // need to convert it to an array of bytes - var array_buffer = new ArrayBuffer(byte_string.length); - var uint_array = new Uint8Array(array_buffer); - for (var j=0; j < byte_string.length; j++) { - uint_array[j] = byte_string.charCodeAt(j); - } - value = uint_array; + value = Sao.common.atob(value); break; case 'Decimal': value = new Sao.Decimal(value.decimal); @@ -272,18 +262,9 @@ 'decimal': value.toString() }; } else if (value instanceof Uint8Array) { - var strings = [], chunksize = 0xffff; - // JavaScript Core has hard-coded argument limit of 65536 - // String.fromCharCode can not be called with too many - // arguments - for (var j = 0; j * chunksize < value.length; j++) { - strings.push(String.fromCharCode.apply( - null, value.subarray( - j * chunksize, (j + 1) * chunksize))); - } value = { '__class__': 'bytes', - 'base64': btoa(strings.join('')) + 'base64': Sao.common.btoa(value), }; } else { value = jQuery.extend({}, value); diff --git a/src/window.js b/src/window.js index ff3998893c..1db62894cd 100644 --- a/src/window.js +++ b/src/window.js @@ -2063,6 +2063,9 @@ if ((i === 0) && indent && (typeof(val) == 'string')) { val = ' '.repeat(indent) + val; } + if (val instanceof Uint8Array) { + val = Sao.common.btoa(val); + } row.push(val); }); return row;