-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Maho | ||
* | ||
* @category Maho | ||
* @package js | ||
* @copyright Copyright (c) 2018 Thomas Bleeker (CBOR & ByteBuffer part) | ||
* @copyright Copyright (c) 2022 Lukas Buchs | ||
* @copyright Copyright (c) 2025 Maho (https://mahocommerce.com) | ||
* @license https://opensource.org/license/mit MIT | ||
*/ | ||
|
||
/** | ||
* convert RFC 1342-like base64 strings to array buffer | ||
* @param {mixed} obj | ||
* @returns {undefined} | ||
*/ | ||
function recursiveBase64StrToArrayBuffer(obj) { | ||
let prefix = '=?BINARY?B?'; | ||
let suffix = '?='; | ||
if (typeof obj === 'object') { | ||
for (let key in obj) { | ||
if (typeof obj[key] === 'string') { | ||
let str = obj[key]; | ||
if (str.substring(0, prefix.length) === prefix && str.substring(str.length - suffix.length) === suffix) { | ||
str = str.substring(prefix.length, str.length - suffix.length); | ||
|
||
let binary_string = window.atob(str); | ||
let len = binary_string.length; | ||
let bytes = new Uint8Array(len); | ||
for (let i = 0; i < len; i++) { | ||
bytes[i] = binary_string.charCodeAt(i); | ||
} | ||
obj[key] = bytes.buffer; | ||
} | ||
} else { | ||
recursiveBase64StrToArrayBuffer(obj[key]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Convert a ArrayBuffer to Base64 | ||
* @param {ArrayBuffer} buffer | ||
* @returns {String} | ||
*/ | ||
function arrayBufferToBase64(buffer) { | ||
let binary = ''; | ||
let bytes = new Uint8Array(buffer); | ||
let len = bytes.byteLength; | ||
for (let i = 0; i < len; i++) { | ||
binary += String.fromCharCode( bytes[ i ] ); | ||
} | ||
return window.btoa(binary); | ||
} |