| 1 | import { NextResponse } from 'next/server' |
| 2 | import { getSession } |
| 3 | import { db } |
| 4 | import { media } |
| 5 | import { s3Service } |
| 6 | |
| 7 | const MAX_SIZE = 10 |
| 8 | const ALLOWED_TYPES = [ |
| 9 | 'image/jpeg', |
| 10 | 'image/png', |
| 11 | 'image/gif', |
| 12 | 'image/webp', |
| 13 | 'image/svg+xml', |
| 14 | ] |
| 15 | |
| 16 | export async function POST |
| 17 | const session = |
| 18 | if (!session) |
| 19 | return NextResponse.json |
| 20 | } |
| 21 | |
| 22 | const formData = |
| 23 | const file = |
| 24 | |
| 25 | if (!file) |
| 26 | return NextResponse.json |
| 27 | } |
| 28 | |
| 29 | if (!ALLOWED_TYPES.includes |
| 30 | return NextResponse.json |
| 31 | { error: 'File type not allowed. Use JPEG, PNG, GIF, WebP, or SVG.' |
| 32 | { status: 400 } |
| 33 | ) |
| 34 | } |
| 35 | |
| 36 | if (file.size |
| 37 | return NextResponse.json |
| 38 | { error: 'File too large. Maximum size is 5MB.' |
| 39 | { status: 400 } |
| 40 | ) |
| 41 | } |
| 42 | |
| 43 | const buffer = |
| 44 | const { key |
| 45 | buffer, |
| 46 | file.type, |
| 47 | `uploads/${session.user |
| 48 | ) |
| 49 | |
| 50 | const [mediaRecord] |
| 51 | .insert(media) |
| 52 | .values({ |
| 53 | name: file.name |
| 54 | key, |
| 55 | contentType: file.type |
| 56 | size: file.size |
| 57 | userId: session.user |
| 58 | }) |
| 59 | .returning({ id: |
| 60 | |
| 61 | return NextResponse.json |
| 62 | } |