From 0d0afcdc78890e2b9b227e435b2c82a6e5b707c9 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 8 Apr 2026 17:48:02 -0700 Subject: [PATCH] fix: use null instead of undefined for optional D1 bind params in media upload D1 (Cloudflare's SQLite) throws D1_TYPE_ERROR when binding undefined values. Non-image uploads (PDFs, documents) left width, height, and thumbnailUrl as undefined. Initialize these fields as null from the start so null is carried through the entire flow. Fixes both single and multi-file upload endpoints. Closes #622 Co-Authored-By: Claude Opus 4.6 --- packages/core/src/routes/api-media.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/core/src/routes/api-media.ts b/packages/core/src/routes/api-media.ts index 5d145e3e3..b5124d47e 100644 --- a/packages/core/src/routes/api-media.ts +++ b/packages/core/src/routes/api-media.ts @@ -99,8 +99,8 @@ apiMediaRoutes.post('/upload', async (c) => { const publicUrl = `https://pub-${bucketName}.r2.dev/${r2Key}` // Extract image dimensions if it's an image - let width: number | undefined - let height: number | undefined + let width: number | null = null + let height: number | null = null if (file.type.startsWith('image/') && !file.type.includes('svg')) { try { @@ -113,7 +113,7 @@ apiMediaRoutes.post('/upload', async (c) => { } // Generate thumbnail URL for images - let thumbnailUrl: string | undefined + let thumbnailUrl: string | null = null if (file.type.startsWith('image/') && c.env.IMAGES_ACCOUNT_ID) { thumbnailUrl = `https://imagedelivery.net/${c.env.IMAGES_ACCOUNT_ID}/${r2Key}/thumbnail` } @@ -149,12 +149,12 @@ apiMediaRoutes.post('/upload', async (c) => { mediaRecord.original_name, mediaRecord.mime_type, mediaRecord.size, - mediaRecord.width ?? null, - mediaRecord.height ?? null, + mediaRecord.width, + mediaRecord.height, mediaRecord.folder, mediaRecord.r2_key, mediaRecord.public_url, - mediaRecord.thumbnail_url ?? null, + mediaRecord.thumbnail_url, mediaRecord.uploaded_by, mediaRecord.uploaded_at ).run() @@ -258,8 +258,8 @@ apiMediaRoutes.post('/upload-multiple', async (c) => { const publicUrl = `https://pub-${bucketName}.r2.dev/${r2Key}` // Extract image dimensions if it's an image - let width: number | undefined - let height: number | undefined + let width: number | null = null + let height: number | null = null if (file.type.startsWith('image/') && !file.type.includes('svg')) { try { @@ -272,7 +272,7 @@ apiMediaRoutes.post('/upload-multiple', async (c) => { } // Generate thumbnail URL for images - let thumbnailUrl: string | undefined + let thumbnailUrl: string | null = null if (file.type.startsWith('image/') && c.env.IMAGES_ACCOUNT_ID) { thumbnailUrl = `https://imagedelivery.net/${c.env.IMAGES_ACCOUNT_ID}/${r2Key}/thumbnail` } @@ -307,12 +307,12 @@ apiMediaRoutes.post('/upload-multiple', async (c) => { mediaRecord.original_name, mediaRecord.mime_type, mediaRecord.size, - mediaRecord.width ?? null, - mediaRecord.height ?? null, + mediaRecord.width, + mediaRecord.height, mediaRecord.folder, mediaRecord.r2_key, mediaRecord.public_url, - mediaRecord.thumbnail_url ?? null, + mediaRecord.thumbnail_url, mediaRecord.uploaded_by, mediaRecord.uploaded_at ).run()