miniperplx/app/api/upload/route.ts
2024-09-03 00:10:12 +05:30

27 lines
877 B
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { put } from '@vercel/blob';
export async function POST(request: NextRequest) {
const formData = await request.formData();
const file = formData.get('file') as File;
if (!file) {
return NextResponse.json({ error: 'No file uploaded' }, { status: 400 });
}
try {
const blob = await put(`mplx/image-${Date.now()}.${file.name.split('.').pop()}`, file, {
access: 'public',
});
return NextResponse.json({
name: file.name,
contentType: file.type,
url: blob.url,
size: file.size, // Include the file size in the response
});
} catch (error) {
console.error('Error uploading file:', error);
return NextResponse.json({ error: 'Failed to upload file' }, { status: 500 });
}
}