From c512f098a7a3bb0773e3aede3c71765ffa7bfad2 Mon Sep 17 00:00:00 2001 From: Benoit Socias Date: Tue, 4 Apr 2023 13:00:06 +0000 Subject: [PATCH] [IMP] web_editor: introduce a tool method to compute size of data URL This commit extracts the calculation of the number of bytes within a base64 data URL to a method named `getDataURLBinarySize`. task-2835144 Part-of: odoo/odoo#103398 --- .../static/src/js/editor/image_processing.js | 10 ++++++++++ .../static/src/js/editor/snippets.options.js | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/addons/web_editor/static/src/js/editor/image_processing.js b/addons/web_editor/static/src/js/editor/image_processing.js index b974ae9e321..1f87360f9c4 100644 --- a/addons/web_editor/static/src/js/editor/image_processing.js +++ b/addons/web_editor/static/src/js/editor/image_processing.js @@ -496,6 +496,15 @@ function createDataURL(blob) { }); } +/** + * @param {String} dataURL + * @returns {Number} number of bytes represented with base64 + */ +function getDataURLBinarySize(dataURL) { + // Every 4 bytes of base64 represent 3 bytes. + return dataURL.split(',')[1].length / 4 * 3; +} + export default { applyModifications, cropperDataFields, @@ -507,4 +516,5 @@ export default { isImageSupportedForStyle, createDataURL, isGif, + getDataURLBinarySize, }; diff --git a/addons/web_editor/static/src/js/editor/snippets.options.js b/addons/web_editor/static/src/js/editor/snippets.options.js index 7cfb7e1d4b5..19bc4711649 100644 --- a/addons/web_editor/static/src/js/editor/snippets.options.js +++ b/addons/web_editor/static/src/js/editor/snippets.options.js @@ -31,6 +31,7 @@ import { isImageSupportedForStyle, createDataURL, isGif, + getDataURLBinarySize, } from "web_editor.image_processing"; import * as OdooEditorLib from "@web_editor/js/editor/odoo-editor/src/OdooEditor"; import {SIZES, MEDIAS_BREAKPOINTS} from "@web/core/ui/ui_service"; @@ -5874,7 +5875,7 @@ const ImageHandlerOption = SnippetOptionWidget.extend({ return; } const dataURL = await applyModifications(img, {mimetype: this._getImageMimetype(img)}); - this._filesize = dataURL.split(',')[1].length / 4 * 3 / 1024; + this._filesize = getDataURLBinarySize(dataURL) / 1024; if (update) { img.classList.add('o_modified_image_to_save');