fix: restore BottleGrid and apply storage URL normalization

This commit is contained in:
2025-12-18 09:36:13 +01:00
parent 689dddd1ac
commit a3aa4f8b25
3 changed files with 65 additions and 20 deletions

View File

@@ -9,4 +9,32 @@ if (!supabaseUrl || !supabaseAnonKey) {
export const supabase = (supabaseUrl && supabaseAnonKey)
? createClient(supabaseUrl, supabaseAnonKey)
: null as any; // Fallback or handle null in services
: null as any;
/**
* Normalisiert eine Storage-URL oder einen Pfad.
* Verhindert "Mixed Content"-Fehler in Self-Hosted Setups,
* indem interne IPs durch die öffentliche URL ersetzt werden.
*/
export function getStorageUrl(path: string | null | undefined): string {
if (!path) return '';
const baseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL?.replace(/\/$/, '');
if (!baseUrl) return path;
// Wenn es bereits eine URL ist
if (path.startsWith('http')) {
// Falls die URL eine interne IP enthält (z.B. 192.168.x.x oder localhost)
// oder falls sie über http statt https kommt (obwohl die App https nutzt)
if (path.includes('192.168.') || path.includes('localhost') || (baseUrl.startsWith('https') && path.startsWith('http:'))) {
const storagePathMatch = path.match(/\/storage\/v1\/object\/public\/bottles\/(.+)$/);
if (storagePathMatch) {
return `${baseUrl}/storage/v1/object/public/bottles/${storagePathMatch[1]}`;
}
}
return path;
}
// Wenn es nur ein Pfad ist, vervollständigen
return `${baseUrl}/storage/v1/object/public/bottles/${path}`;
}