Adrià Vilanova Martínez | 5af8651 | 2023-12-02 20:44:16 +0100 | [diff] [blame] | 1 | /* (license-header) |
| 2 | * hores |
| 3 | * Copyright (c) 2023 Adrià Vilanova Martínez |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Affero General Public License as |
| 7 | * published by the Free Software Foundation, either version 3 of the |
| 8 | * License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU Affero General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public |
| 16 | * License along with this program. |
| 17 | * If not, see http://www.gnu.org/licenses/. |
| 18 | */ |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 19 | /** |
| 20 | * convert RFC 1342-like base64 strings to array buffer |
| 21 | * @param {mixed} obj |
| 22 | * @returns {undefined} |
| 23 | */ |
| 24 | function recursiveBase64StrToArrayBuffer(obj) { |
| 25 | let prefix = '=?BINARY?B?'; |
| 26 | let suffix = '?='; |
| 27 | if (typeof obj === 'object') { |
| 28 | for (let key in obj) { |
| 29 | if (typeof obj[key] === 'string') { |
| 30 | let str = obj[key]; |
| 31 | if (str.substring(0, prefix.length) === prefix && str.substring(str.length - suffix.length) === suffix) { |
| 32 | str = str.substring(prefix.length, str.length - suffix.length); |
| 33 | |
| 34 | let binary_string = window.atob(str); |
| 35 | let len = binary_string.length; |
| 36 | let bytes = new Uint8Array(len); |
| 37 | for (var i = 0; i < len; i++) { |
| 38 | bytes[i] = binary_string.charCodeAt(i); |
| 39 | } |
| 40 | obj[key] = bytes.buffer; |
| 41 | } |
| 42 | } else { |
| 43 | recursiveBase64StrToArrayBuffer(obj[key]); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Convert a ArrayBuffer to Base64 |
| 51 | * @param {ArrayBuffer} buffer |
| 52 | * @returns {String} |
| 53 | */ |
| 54 | function arrayBufferToBase64(buffer) { |
| 55 | var binary = ''; |
| 56 | var bytes = new Uint8Array(buffer); |
| 57 | var len = bytes.byteLength; |
| 58 | for (var i = 0; i < len; i++) { |
| 59 | binary += String.fromCharCode( bytes[ i ] ); |
| 60 | } |
| 61 | return window.btoa(binary); |
| 62 | } |