generated from nhcarrigan/template
fix: resolve base64 image upload issues
This resolves issue #65 by addressing multiple problems that were preventing base64-encoded cover images from being uploaded: 1. Increased Fastify body limit from 1MB to 10MB to accommodate base64-encoded images 2. Removed duplicate coverImage string length validation that was blocking base64 data URLs 3. Fixed base64 size calculation to properly extract and measure just the base64 data portion 4. Updated validateDataUrl regex to allow whitespace in base64 strings 5. Added proper error handling with 400 status codes and helpful error messages instead of 500 errors Users can now successfully upload cover images as base64 data URLs up to 5MB (decoded size) and will receive clear validation error messages if uploads fail. Closes #65
This commit is contained in:
@@ -33,9 +33,6 @@ export class GameService {
|
||||
if (!validateStringLength(data.notes, MAX_LENGTHS.NOTES)) {
|
||||
throw new Error(`Notes must be ${MAX_LENGTHS.NOTES} characters or less.`);
|
||||
}
|
||||
if (!validateStringLength(data.coverImage, MAX_LENGTHS.URL)) {
|
||||
throw new Error(`Cover image URL must be ${MAX_LENGTHS.URL} characters or less.`);
|
||||
}
|
||||
|
||||
// Validate rating
|
||||
if (!validateRating(data.rating)) {
|
||||
@@ -45,8 +42,14 @@ export class GameService {
|
||||
// Validate cover image URL
|
||||
if (data.coverImage) {
|
||||
if (data.coverImage.startsWith("data:")) {
|
||||
const sizeInBytes = data.coverImage.length * 0.75;
|
||||
if (sizeInBytes > MAX_LENGTHS.DATA_URL) {
|
||||
// Extract just the base64 data (after the comma)
|
||||
const base64Data = data.coverImage.split(",")[1];
|
||||
if (!base64Data) {
|
||||
throw new Error("Invalid image data URL format.");
|
||||
}
|
||||
// Calculate decoded size: base64 is ~4/3 larger than original, so multiply by 0.75
|
||||
const decodedSizeInBytes = base64Data.length * 0.75;
|
||||
if (decodedSizeInBytes > MAX_LENGTHS.DATA_URL) {
|
||||
throw new Error("Cover image must be under 5MB.");
|
||||
}
|
||||
if (!validateDataUrl(data.coverImage)) {
|
||||
|
||||
Reference in New Issue
Block a user