fix: nahravanie velkych obrazkov - priamo do Supabase cez podpisanu URL, zmensenie na 2000 px

Vercel odmietal poziadavky nad 4,5 MB (Request Entity Too Large) a bucket
berie najviac 10 MB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jakub_HostacnyandClaude Opus 5.5 committed 2026-10-21 13:29:29 +02:00
1 parent 3ece5abf6a
commit 17c852e2bf
4 files changed
+76 -49

No files matched your search

+14 -26
View File
@@ -1,42 +1,30 @@
import { NextRequest, NextResponse } from 'next/server';
import { getSupabaseAdmin } from '@/lib/supabase';
const BUCKET = 'newsletter-assets';
// Vydá podpísanú adresu na nahratie súboru priamo z prehliadača do Supabase Storage.
// Samotný súbor cez server nejde – Vercel by odmietol požiadavky nad 4,5 MB („Request Entity Too Large“).
export async function POST(req: NextRequest) {
try {
const formData = await req.formData();
const file = formData.get('file') as File | null;
if (!file) {
return NextResponse.json({ error: 'Súbor nebol poskytnutý.' }, { status: 400 });
const { name } = (await req.json()) as { name?: string };
if (!name) {
return NextResponse.json({ error: 'Chýba názov súboru.' }, { status: 400 });
}
const admin = getSupabaseAdmin();
const safeName = file.name.replace(/[^a-zA-Z0-9._-]/g, '_');
const safeName = name.replace(/[^a-zA-Z0-9._-]/g, '_');
const path = `uploads/${Date.now()}-${safeName}`;
const arrayBuffer = await file.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
const { error: uploadError } = await admin.storage
.from('newsletter-assets')
.upload(path, buffer, {
contentType: file.type || 'image/png',
upsert: true
});
if (uploadError) {
console.error('Upload to Supabase Storage error:', uploadError);
return NextResponse.json({ error: uploadError.message }, { status: 500 });
const { data, error } = await admin.storage.from(BUCKET).createSignedUploadUrl(path);
if (error || !data) {
console.error('Signed upload URL error:', error);
return NextResponse.json({ error: error?.message || 'Nepodarilo sa pripraviť nahratie' }, { status: 500 });
}
const { data: pubData } = admin.storage
.from('newsletter-assets')
.getPublicUrl(path);
const { data: pubData } = admin.storage.from(BUCKET).getPublicUrl(path);
return NextResponse.json({
success: true,
url: pubData.publicUrl
});
return NextResponse.json({ success: true, path, token: data.token, url: pubData.publicUrl });
} catch (err: any) {
console.error('API /upload error:', err);
return NextResponse.json({ error: err.message || 'Chyba servera pri nahrávaní súboru' }, { status: 500 });