// Nahrá statické obrázky aplikácie (supabase/assets/) do Supabase Storage bucketu newsletter-assets. // Spustenie: npm run db:upload-assets (číta NEXT_PUBLIC_SUPABASE_URL a SUPABASE_SERVICE_ROLE_KEY z .env.local) // Existujúce súbory s rovnakým názvom sa prepíšu. import { createClient } from '@supabase/supabase-js'; import { readdir, readFile } from 'node:fs/promises'; import path from 'node:path'; const BUCKET = 'newsletter-assets'; const ASSETS_DIR = path.join(process.cwd(), 'supabase', 'assets'); const CONTENT_TYPES = { '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml', '.webp': 'image/webp' }; const url = process.env.NEXT_PUBLIC_SUPABASE_URL; const serviceKey = process.env.SUPABASE_SERVICE_ROLE_KEY; if (!url || !serviceKey) { console.error('Chýba NEXT_PUBLIC_SUPABASE_URL alebo SUPABASE_SERVICE_ROLE_KEY v .env.local'); process.exit(1); } const supabase = createClient(url, serviceKey); const files = (await readdir(ASSETS_DIR)).filter((f) => CONTENT_TYPES[path.extname(f).toLowerCase()]); let failed = 0; for (const file of files) { const body = await readFile(path.join(ASSETS_DIR, file)); const { error } = await supabase.storage.from(BUCKET).upload(file, body, { contentType: CONTENT_TYPES[path.extname(file).toLowerCase()], upsert: true }); if (error) { failed++; console.error(`✗ ${file}: ${error.message}`); } else { console.log(`✓ ${file}`); } } console.log(`\nNahraté ${files.length - failed}/${files.length} súborov do bucketu ${BUCKET}.`); if (failed) process.exit(1);