UNPKG

@asgerami/zemenay-blog

Version:

Plug-and-play blog system for Next.js - Get a fully functional blog running in minutes with zero configuration

123 lines (122 loc) 4.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.uploadImage = uploadImage; exports.getImageUrl = getImageUrl; exports.deleteImage = deleteImage; const supabase_1 = require("./supabase"); /** * Upload an image to Supabase Storage and create a database record */ async function uploadImage({ file, altText = '', folder = 'uploads' }) { try { const supabase = (0, supabase_1.getSupabaseClient)(); // Validate file type if (!file.type.startsWith('image/')) { return { success: false, error: 'File must be an image' }; } // Validate file size (5MB limit) const maxSize = 5 * 1024 * 1024; // 5MB if (file.size > maxSize) { return { success: false, error: 'Image must be smaller than 5MB' }; } // Generate unique filename const fileExt = file.name.split('.').pop(); const fileName = `${Date.now()}-${Math.random().toString(36).substring(2)}.${fileExt}`; const filePath = `${folder}/${fileName}`; // Upload to Supabase Storage const { data: uploadData, error: uploadError } = await supabase.storage .from('blog-images') .upload(filePath, file, { cacheControl: '3600', upsert: false }); if (uploadError) { console.error('Upload error:', uploadError); return { success: false, error: uploadError.message }; } // Get public URL const { data: { publicUrl } } = supabase.storage .from('blog-images') .getPublicUrl(filePath); // Create database record const { data: imageData, error: dbError } = await supabase .from('images') .insert([{ filename: fileName, original_name: file.name, file_path: filePath, file_size: file.size, mime_type: file.type, alt_text: altText }]) .select() .single(); if (dbError) { console.error('Database error:', dbError); // Try to clean up uploaded file await supabase.storage.from('blog-images').remove([filePath]); return { success: false, error: dbError.message }; } return { success: true, image: imageData, publicUrl }; } catch (error) { console.error('Upload error:', error); return { success: false, error: error instanceof Error ? error.message : 'Upload failed' }; } } /** * Get public URL for an image */ function getImageUrl(filePath) { const supabase = (0, supabase_1.getSupabaseClient)(); const { data: { publicUrl } } = supabase.storage .from('blog-images') .getPublicUrl(filePath); return publicUrl; } /** * Delete an image from storage and database */ async function deleteImage(imageId) { try { const supabase = (0, supabase_1.getSupabaseClient)(); // Get image data first const { data: image, error: fetchError } = await supabase .from('images') .select('file_path') .eq('id', imageId) .single(); if (fetchError) { return { success: false, error: fetchError.message }; } // Delete from storage const { error: storageError } = await supabase.storage .from('blog-images') .remove([image.file_path]); if (storageError) { console.error('Storage deletion error:', storageError); } // Delete from database const { error: dbError } = await supabase .from('images') .delete() .eq('id', imageId); if (dbError) { return { success: false, error: dbError.message }; } return { success: true }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Delete failed' }; } }