(urlOrPath: string | null)
| 4 | * Extract the storage path from a full URL or return the path as-is if it's already relative |
| 5 | */ |
| 6 | export function extractStoragePath(urlOrPath: string | null): string | null { |
| 7 | if (!urlOrPath || urlOrPath === 'n/a') { |
| 8 | return null; |
| 9 | } |
| 10 | |
| 11 | // If it's already a relative path, return it |
| 12 | if (!urlOrPath.startsWith('http')) { |
| 13 | return urlOrPath; |
| 14 | } |
| 15 | |
| 16 | // Extract path from full URL |
| 17 | try { |
| 18 | const url = new URL(urlOrPath); |
| 19 | const pathMatch = url.pathname.match(/\/storage\/v1\/object\/public\/(.+)/); |
| 20 | if (pathMatch) { |
| 21 | return pathMatch[1]; |
| 22 | } |
| 23 | |
| 24 | // Try authenticated path pattern |
| 25 | const authPathMatch = url.pathname.match(/\/storage\/v1\/object\/sign\/(.+)/); |
| 26 | if (authPathMatch) { |
| 27 | return authPathMatch[1]; |
| 28 | } |
| 29 | |
| 30 | // If it's a storage URL but doesn't match patterns, try to extract the path after /storage/v1/object/ |
| 31 | if (url.pathname.includes('/storage/v1/object/')) { |
| 32 | const parts = url.pathname.split('/storage/v1/object/'); |
| 33 | if (parts[1]) { |
| 34 | // Remove 'public/' or 'sign/' prefix if present |
| 35 | return parts[1].replace(/^(public|sign)\//, ''); |
| 36 | } |
| 37 | } |
| 38 | } catch (e) { |
| 39 | console.error('Error parsing URL:', e); |
| 40 | } |
| 41 | |
| 42 | return null; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get a signed URL for a protected storage object |
no outgoing calls
no test coverage detected