|
| 1 | +import { createClient } from '@lib/supabase/client'; |
| 2 | +import { v4 as uuidv4 } from 'uuid'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Result type for content image upload |
| 6 | + */ |
| 7 | +export interface ContentImageUploadResult { |
| 8 | + url: string; |
| 9 | + path: string; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Validation result type |
| 14 | + */ |
| 15 | +export interface ValidationResult { |
| 16 | + valid: boolean; |
| 17 | + error?: string; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Allowed image MIME types for content images |
| 22 | + */ |
| 23 | +const ALLOWED_IMAGE_TYPES = [ |
| 24 | + 'image/jpeg', |
| 25 | + 'image/jpg', |
| 26 | + 'image/png', |
| 27 | + 'image/webp', |
| 28 | + 'image/gif', |
| 29 | +] as const; |
| 30 | + |
| 31 | +/** |
| 32 | + * MIME type to file extension mapping |
| 33 | + */ |
| 34 | +const MIME_TO_EXTENSION: Record<string, string> = { |
| 35 | + 'image/jpeg': 'jpg', |
| 36 | + 'image/jpg': 'jpg', |
| 37 | + 'image/png': 'png', |
| 38 | + 'image/webp': 'webp', |
| 39 | + 'image/gif': 'gif', |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * Maximum file size in bytes (10MB) |
| 44 | + */ |
| 45 | +const MAX_FILE_SIZE = 10 * 1024 * 1024; |
| 46 | + |
| 47 | +/** |
| 48 | + * Storage bucket name for content images |
| 49 | + */ |
| 50 | +const BUCKET_NAME = 'content-images'; |
| 51 | + |
| 52 | +/** |
| 53 | + * Validate image file type and size |
| 54 | + * |
| 55 | + * @param file - File to validate |
| 56 | + * @returns Validation result with error message if invalid |
| 57 | + */ |
| 58 | +export function validateImageFile(file: File): ValidationResult { |
| 59 | + // Check file type |
| 60 | + if ( |
| 61 | + !ALLOWED_IMAGE_TYPES.includes( |
| 62 | + file.type as (typeof ALLOWED_IMAGE_TYPES)[number] |
| 63 | + ) |
| 64 | + ) { |
| 65 | + return { |
| 66 | + valid: false, |
| 67 | + error: `Unsupported file type. Supported formats: ${ALLOWED_IMAGE_TYPES.join(', ')}`, |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + // Check file size |
| 72 | + if (file.size > MAX_FILE_SIZE) { |
| 73 | + return { |
| 74 | + valid: false, |
| 75 | + error: `File too large. Maximum size: ${Math.round(MAX_FILE_SIZE / 1024 / 1024)}MB`, |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + return { valid: true }; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Generate a unique file path for content image |
| 84 | + * |
| 85 | + * Format: user-{userId}/{timestamp}-{uuid}.{ext} |
| 86 | + * |
| 87 | + * @param userId - User ID |
| 88 | + * @param fileName - Original file name (used for fallback only) |
| 89 | + * @param fileType - MIME type of the file |
| 90 | + * @returns Generated file path |
| 91 | + */ |
| 92 | +export function generateContentImagePath( |
| 93 | + userId: string, |
| 94 | + fileName: string, |
| 95 | + fileType: string |
| 96 | +): string { |
| 97 | + const uuid = uuidv4(); |
| 98 | + const timestamp = Date.now(); |
| 99 | + const extension = |
| 100 | + MIME_TO_EXTENSION[fileType] || fileName.split('.').pop() || 'jpg'; |
| 101 | + const safeFileName = `${timestamp}-${uuid}.${extension}`; |
| 102 | + return `user-${userId}/${safeFileName}`; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Extract file path from Supabase Storage URL |
| 107 | + * |
| 108 | + * @param url - Full Supabase Storage URL |
| 109 | + * @returns File path or null if invalid |
| 110 | + */ |
| 111 | +export function extractFilePathFromUrl(url: string): string | null { |
| 112 | + try { |
| 113 | + const urlObj = new URL(url); |
| 114 | + const pathParts = urlObj.pathname.split('/'); |
| 115 | + const bucketIndex = pathParts.indexOf(BUCKET_NAME); |
| 116 | + if (bucketIndex !== -1 && bucketIndex < pathParts.length - 1) { |
| 117 | + return pathParts.slice(bucketIndex + 1).join('/'); |
| 118 | + } |
| 119 | + return null; |
| 120 | + } catch { |
| 121 | + return null; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Upload content image to Supabase Storage |
| 127 | + * |
| 128 | + * @param file - Image file to upload |
| 129 | + * @param userId - User ID for file path generation |
| 130 | + * @returns Upload result with public URL and file path |
| 131 | + * @throws Error if upload fails or validation fails |
| 132 | + */ |
| 133 | +export async function uploadContentImage( |
| 134 | + file: File, |
| 135 | + userId: string |
| 136 | +): Promise<ContentImageUploadResult> { |
| 137 | + // Validate file |
| 138 | + const validation = validateImageFile(file); |
| 139 | + if (!validation.valid) { |
| 140 | + throw new Error(validation.error); |
| 141 | + } |
| 142 | + |
| 143 | + const supabase = createClient(); |
| 144 | + |
| 145 | + // Generate file path |
| 146 | + const filePath = generateContentImagePath(userId, file.name, file.type); |
| 147 | + |
| 148 | + // Upload to Supabase Storage |
| 149 | + const { error: uploadError } = await supabase.storage |
| 150 | + .from(BUCKET_NAME) |
| 151 | + .upload(filePath, file, { |
| 152 | + cacheControl: '3600', |
| 153 | + upsert: false, |
| 154 | + }); |
| 155 | + |
| 156 | + if (uploadError) { |
| 157 | + throw new Error(`Upload failed: ${uploadError.message}`); |
| 158 | + } |
| 159 | + |
| 160 | + // Get public URL |
| 161 | + const { data: urlData } = supabase.storage |
| 162 | + .from(BUCKET_NAME) |
| 163 | + .getPublicUrl(filePath); |
| 164 | + |
| 165 | + if (!urlData?.publicUrl) { |
| 166 | + throw new Error('Failed to get public URL'); |
| 167 | + } |
| 168 | + |
| 169 | + return { |
| 170 | + url: urlData.publicUrl, |
| 171 | + path: filePath, |
| 172 | + }; |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * Delete content image from Supabase Storage |
| 177 | + * |
| 178 | + * @param filePath - File path in storage (e.g., user-{userId}/filename.jpg) |
| 179 | + * @throws Error if deletion fails |
| 180 | + */ |
| 181 | +export async function deleteContentImage(filePath: string): Promise<void> { |
| 182 | + const supabase = createClient(); |
| 183 | + |
| 184 | + const { error } = await supabase.storage.from(BUCKET_NAME).remove([filePath]); |
| 185 | + |
| 186 | + if (error) { |
| 187 | + throw new Error(`Delete failed: ${error.message}`); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +/** |
| 192 | + * List all content images for a specific user |
| 193 | + * |
| 194 | + * @param userId - User ID |
| 195 | + * @returns Array of file paths |
| 196 | + * @throws Error if listing fails |
| 197 | + */ |
| 198 | +export async function listUserContentImages(userId: string): Promise<string[]> { |
| 199 | + const supabase = createClient(); |
| 200 | + |
| 201 | + const { data, error } = await supabase.storage |
| 202 | + .from(BUCKET_NAME) |
| 203 | + .list(`user-${userId}`, { |
| 204 | + sortBy: { column: 'created_at', order: 'desc' }, |
| 205 | + }); |
| 206 | + |
| 207 | + if (error) { |
| 208 | + throw new Error(`Failed to list images: ${error.message}`); |
| 209 | + } |
| 210 | + |
| 211 | + return data.map(file => `user-${userId}/${file.name}`); |
| 212 | +} |
0 commit comments