fix: theme exports and imports sometimes failing due to attached images

This commit is contained in:
SethBurkart123
2025-02-14 17:49:31 +11:00
parent a321a482cc
commit 8972a5a8bf
2 changed files with 24 additions and 6 deletions
+17 -4
View File
@@ -22,9 +22,21 @@ type ThemeContent = {
}; };
function stripBase64Prefix(base64String: string): string { function stripBase64Prefix(base64String: string): string {
const prefixRegex = /^data:image\/\w+;base64,/; 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, ''); 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 }) => { export const StoreDownloadTheme = async (theme: { themeContent: Theme }) => {
if (!theme.themeContent.id) return; if (!theme.themeContent.id) return;
@@ -37,13 +49,14 @@ export const StoreDownloadTheme = async (theme: { themeContent: Theme }) => {
export const InstallTheme = async (themeData: ThemeContent) => { export const InstallTheme = async (themeData: ThemeContent) => {
const strippedCoverImage = stripBase64Prefix(themeData.coverImage); const strippedCoverImage = stripBase64Prefix(themeData.coverImage);
console.log('1!')
const coverImageBlob = base64ToBlob(strippedCoverImage); const coverImageBlob = base64ToBlob(strippedCoverImage);
console.log('2!')
const images = themeData.images.map((image) => ({ const images = themeData.images.map((image) => ({
...image, ...image,
blob: base64ToBlob(image.data) blob: base64ToBlob(stripBase64Prefix(image.data))
})); }));
console.log('3!')
let availableThemes = await localforage.getItem('customThemes') as string[]; let availableThemes = await localforage.getItem('customThemes') as string[];
if (availableThemes && !availableThemes.includes(themeData.id)) { if (availableThemes && !availableThemes.includes(themeData.id)) {
availableThemes.push(themeData.id); availableThemes.push(themeData.id);
+6 -1
View File
@@ -28,7 +28,12 @@ const shareTheme = async (themeID: string) => {
// Helper function to convert Blob to Base64 // Helper function to convert Blob to Base64
const blobToBase64 = (blob: Blob) => new Promise<string>((resolve, reject) => { const blobToBase64 = (blob: Blob) => new Promise<string>((resolve, reject) => {
const reader = new FileReader(); 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.onerror = reject;
reader.readAsDataURL(blob); reader.readAsDataURL(blob);
}); });