From 8972a5a8bf7b6c7ab5b1cc0bae30dfd1bc4ed304 Mon Sep 17 00:00:00 2001 From: SethBurkart123 Date: Fri, 14 Feb 2025 17:49:31 +1100 Subject: [PATCH] fix: theme exports and imports sometimes failing due to attached images --- src/seqta/ui/themes/downloadTheme.ts | 23 ++++++++++++++++++----- src/seqta/ui/themes/shareTheme.ts | 7 ++++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/seqta/ui/themes/downloadTheme.ts b/src/seqta/ui/themes/downloadTheme.ts index 2c277a57..763e210d 100644 --- a/src/seqta/ui/themes/downloadTheme.ts +++ b/src/seqta/ui/themes/downloadTheme.ts @@ -22,8 +22,20 @@ type ThemeContent = { }; function stripBase64Prefix(base64String: string): string { - const prefixRegex = /^data:image\/\w+;base64,/; - return base64String.replace(prefixRegex, ''); + if (!base64String) return ''; + + const prefixRegex = /^data:[^;]+;base64,/; + try { + // Check if the string actually has a base64 prefix + if (prefixRegex.test(base64String)) { + return base64String.replace(prefixRegex, ''); + } + // If no prefix found, return the original string (assuming it's already base64) + return base64String; + } catch(err) { + console.error('Error stripping base64 prefix:', err); + return ''; + } } export const StoreDownloadTheme = async (theme: { themeContent: Theme }) => { @@ -37,13 +49,14 @@ export const StoreDownloadTheme = async (theme: { themeContent: Theme }) => { export const InstallTheme = async (themeData: ThemeContent) => { const strippedCoverImage = stripBase64Prefix(themeData.coverImage); + console.log('1!') const coverImageBlob = base64ToBlob(strippedCoverImage); - + console.log('2!') const images = themeData.images.map((image) => ({ ...image, - blob: base64ToBlob(image.data) + blob: base64ToBlob(stripBase64Prefix(image.data)) })); - + console.log('3!') let availableThemes = await localforage.getItem('customThemes') as string[]; if (availableThemes && !availableThemes.includes(themeData.id)) { availableThemes.push(themeData.id); diff --git a/src/seqta/ui/themes/shareTheme.ts b/src/seqta/ui/themes/shareTheme.ts index 445d98e2..eebb11b6 100644 --- a/src/seqta/ui/themes/shareTheme.ts +++ b/src/seqta/ui/themes/shareTheme.ts @@ -28,7 +28,12 @@ const shareTheme = async (themeID: string) => { // Helper function to convert Blob to Base64 const blobToBase64 = (blob: Blob) => new Promise((resolve, reject) => { const reader = new FileReader(); - reader.onloadend = () => resolve(reader.result as string); + reader.onloadend = () => { + const base64String = reader.result as string; + // Extract just the base64 data without the data URL prefix + const base64Data = base64String.split(',')[1]; + resolve(base64Data); + }; reader.onerror = reject; reader.readAsDataURL(blob); });