diff options
author | 2024-05-20 22:13:55 +0200 | |
---|---|---|
committer | 2024-05-20 22:13:55 +0200 | |
commit | 30144aa3f51f5d0dc90f64cf888bc6f83de11b10 (patch) | |
tree | d3aae751a6c49a378b05f2135102628497f8103f /src/utils/base64.ts | |
parent | e876d0360812eab1255e414102a44d65f7c1bfaf (diff) | |
download | it-tools-30144aa3f51f5d0dc90f64cf888bc6f83de11b10.tar.gz it-tools-30144aa3f51f5d0dc90f64cf888bc6f83de11b10.tar.zst it-tools-30144aa3f51f5d0dc90f64cf888bc6f83de11b10.zip |
feat(base64): Base64 enhancements (#905)
* fix(base64): use js-base64 to handle non ascii text
Use js-base64 to handle non ascii text and ignore whitespaces
Fix #879 and #409
* fix(base64): use js-base64 to handle non ascii text
Use js-base64 to handle non ascii text and ignore whitespaces
Fix #879 and #409
* feat(base64 file converter): add a filename and extension fields
Add filename and extension (auto filled if data url) to allow downloading with right extension and filename
Fix #788
* feat(base64 file converter): add a preview image
Fix #594. Taken from #595 (thanks @SAF2k)
Diffstat (limited to 'src/utils/base64.ts')
-rw-r--r-- | src/utils/base64.ts | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/src/utils/base64.ts b/src/utils/base64.ts index 16912ee..44e59f4 100644 --- a/src/utils/base64.ts +++ b/src/utils/base64.ts @@ -1,7 +1,9 @@ +import { Base64 } from 'js-base64'; + export { textToBase64, base64ToText, isValidBase64, removePotentialDataAndMimePrefix }; function textToBase64(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boolean } = {}) { - const encoded = window.btoa(str); + const encoded = Base64.encode(str); return makeUrlSafe ? makeUriSafe(encoded) : encoded; } @@ -16,7 +18,7 @@ function base64ToText(str: string, { makeUrlSafe = false }: { makeUrlSafe?: bool } try { - return window.atob(cleanStr); + return Base64.decode(cleanStr); } catch (_) { throw new Error('Incorrect base64 string'); @@ -34,10 +36,11 @@ function isValidBase64(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boo } try { + const reEncodedBase64 = Base64.fromUint8Array(Base64.toUint8Array(cleanStr)); if (makeUrlSafe) { - return removePotentialPadding(window.btoa(window.atob(cleanStr))) === cleanStr; + return removePotentialPadding(reEncodedBase64) === cleanStr; } - return window.btoa(window.atob(cleanStr)) === cleanStr; + return reEncodedBase64 === cleanStr.replace(/\s/g, ''); } catch (err) { return false; |