Skip to content

TagSpaces is an offline, open source, document manager with tagging support

chore: optimizing base64ToUint8Array conversion

Ilian Sapundshieve6fd7abd5a8b
diff --git a/src/renderer/components/EntryContainer.tsx b/src/renderer/components/EntryContainer.tsx
index b400da640..637bd7934 100644
--- a/src/renderer/components/EntryContainer.tsx
+++ b/src/renderer/components/EntryContainer.tsx
@@ -220,7 +220,7 @@ function EntryContainer() {
if (data.content) {
getResizedImageThumbnail(data.content)
.then((base64Image) => {
- const data = base64ToUint8Array(base64Image.split(',').pop());
+ const data = base64ToUint8Array(base64Image);
const thumbPath = cLocation.getThumbEntryPath(openedEntry);
saveBinaryFilePromise(
diff --git a/src/renderer/hooks/IOActionsContextProvider.tsx b/src/renderer/hooks/IOActionsContextProvider.tsx
index 1e43dfc25..bf29f6fe2 100644
--- a/src/renderer/hooks/IOActionsContextProvider.tsx
+++ b/src/renderer/hooks/IOActionsContextProvider.tsx
@@ -941,8 +941,7 @@ export const IOActionsContextProvider = ({
location?.getDirSeparator(),
).then((dataURL) => {
if (dataURL && dataURL.length > 6) {
- const baseString = dataURL.split(',').pop();
- const fileContent = base64ToUint8Array(baseString);
+ const fileContent = base64ToUint8Array(dataURL);
return saveBinaryFilePromise(
{
path: getThumbFileLocationForFile(
@@ -1107,8 +1106,7 @@ export const IOActionsContextProvider = ({
)
.then((dataURL) => {
if (dataURL && dataURL.length > 6) {
- const baseString = dataURL.split(',').pop();
- const fileContent = base64ToUint8Array(baseString);
+ const fileContent = base64ToUint8Array(dataURL);
const thumbPath = getThumbFileLocationForFile(
fileTargetPath,
currentLocation?.getDirSeparator(),
@@ -1439,8 +1437,7 @@ export const IOActionsContextProvider = ({
currentLocation.getDirSeparator(),
).then((dataURL) => {
if (dataURL && dataURL.length > 6) {
- const baseString = dataURL.split(',').pop();
- const fileContent = base64ToUint8Array(baseString);
+ const fileContent = base64ToUint8Array(dataURL);
return uploadFile(
filePath,
fileType,
@@ -2269,7 +2266,7 @@ export const IOActionsContextProvider = ({
) // 4K -> 3840, 2K -> 2560
.then((base64Image) => {
if (base64Image) {
- const data = base64ToUint8Array(base64Image.split(',').pop());
+ const data = base64ToUint8Array(base64Image);
return saveBinaryFilePromise({ path: folderBgndPath }, data, true)
.then(() => {
// props.setLastBackgroundImageChange(new Date().getTime());
diff --git a/src/renderer/hooks/ThumbGenerationContextProvider.tsx b/src/renderer/hooks/ThumbGenerationContextProvider.tsx
index 33cfba18b..d9ad3a916 100644
--- a/src/renderer/hooks/ThumbGenerationContextProvider.tsx
+++ b/src/renderer/hooks/ThumbGenerationContextProvider.tsx
@@ -520,8 +520,7 @@ export const ThumbGenerationContextProvider = ({
// data:,
return Promise.reject(new Error('Invalid dataURL'));
}
- const baseString = dataURL.split(',').pop();
- const content = base64ToUint8Array(baseString);
+ const content = base64ToUint8Array(dataURL);
return saveBinaryFilePromise(
{ path: filePath, locationID },
content,
diff --git a/src/renderer/utils/dom.ts b/src/renderer/utils/dom.ts
index b52d38564..70dfdaa0f 100644
--- a/src/renderer/utils/dom.ts
+++ b/src/renderer/utils/dom.ts
@@ -138,8 +138,23 @@ export function dataURLtoBlob(dataURI) {
* @param base64
*/
export function base64ToUint8Array(base64) {
- const binaryString = window.atob(base64);
- return Uint8Array.from(binaryString, (char) => char.charCodeAt(0));
+ // Find where the actual data starts (after the comma)
+ // If no comma is found, assume the string is raw base64
+ const commaIndex = base64.indexOf(',');
+ const startOffset = commaIndex !== -1 ? commaIndex + 1 : 0;
+
+ // atob can take a portion of a string via substring/slice
+ // Most modern engines optimize slice() to be more memory-efficient than split()
+ const binaryString = window.atob(base64.slice(startOffset));
+
+ const len = binaryString.length;
+ const bytes = new Uint8Array(len);
+
+ for (let i = 0; i < len; i++) {
+ bytes[i] = binaryString.charCodeAt(i);
+ }
+
+ return bytes;
}
export function generateClipboardLink(url, name?) {