From 8e2fc41747a726ae6283936ddffb350a70f7ef63 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 2 Jun 2025 15:55:38 -0700 Subject: [PATCH] Enable svg on cloudinary --- src/lib/server/cloudinary.ts | 40 +++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/lib/server/cloudinary.ts b/src/lib/server/cloudinary.ts index a4d33ec..bce8c29 100644 --- a/src/lib/server/cloudinary.ts +++ b/src/lib/server/cloudinary.ts @@ -106,14 +106,22 @@ export async function uploadFile( const arrayBuffer = await file.arrayBuffer() const buffer = Buffer.from(arrayBuffer) + // Check if file is SVG + const isSvg = file.type === 'image/svg+xml' || file.name.toLowerCase().endsWith('.svg') + + // Prepare upload options + const uploadOptions = { + ...uploadPresets[type], + ...customOptions, + public_id: `${Date.now()}-${file.name.replace(/\.[^/.]+$/, '')}`, + // Override resource_type for SVG files + ...(isSvg && { resource_type: 'raw' as const }) + } + // Upload to Cloudinary const result = await new Promise((resolve, reject) => { const uploadStream = cloudinary.uploader.upload_stream( - { - ...uploadPresets[type], - ...customOptions, - public_id: `${Date.now()}-${file.name.replace(/\.[^/.]+$/, '')}` - }, + uploadOptions, (error, result) => { if (error) reject(error) else if (result) resolve(result) @@ -124,11 +132,13 @@ export async function uploadFile( uploadStream.end(buffer) }) - // Generate thumbnail URL - const thumbnailUrl = cloudinary.url(result.public_id, { - ...imageSizes.thumbnail, - secure: true - }) + // Generate thumbnail URL (for SVG, just use the original URL) + const thumbnailUrl = isSvg + ? result.secure_url + : cloudinary.url(result.public_id, { + ...imageSizes.thumbnail, + secure: true + }) logger.mediaUpload(file.name, file.size, file.type, true) @@ -164,13 +174,19 @@ export async function uploadFiles( } // Delete a file from Cloudinary -export async function deleteFile(publicId: string): Promise { +export async function deleteFile(publicId: string, resourceType: string = 'image'): Promise { try { if (!isCloudinaryConfigured()) { throw new Error('Cloudinary is not configured') } - const result = await cloudinary.uploader.destroy(publicId) + // Check if this is an SVG file by looking at the public ID + const isSvg = publicId.includes('-svg') || publicId.endsWith('.svg') + const actualResourceType = isSvg ? 'raw' : resourceType + + const result = await cloudinary.uploader.destroy(publicId, { + resource_type: actualResourceType + }) return result.result === 'ok' } catch (error) { logger.error('Cloudinary delete failed', error as Error)