Files
gestion/src/app/api/tickets/[id]/attachments/[attachmentId]/route.ts
T

30 lines
1.2 KiB
TypeScript

import { NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { getCurrentUser } from '@/lib/auth'
export async function GET(request: Request, { params }: { params: { id: string, attachmentId: string } }) {
try {
const user = await getCurrentUser()
if (!user) return NextResponse.json({ error: 'Non authentifié' }, { status: 401 })
const check: any[] = await prisma.$queryRawUnsafe(`SELECT id FROM "Ticket" WHERE id = $1 AND "userId" = $2`, params.id, user.id)
if (!check.length) return NextResponse.json({ error: 'Ticket introuvable' }, { status: 404 })
const att: any[] = await prisma.$queryRawUnsafe(`
SELECT * FROM "TicketAttachment" WHERE id = $1 AND "ticketId" = $2
`, params.attachmentId, params.id)
if (!att.length) return NextResponse.json({ error: 'Fichier introuvable' }, { status: 404 })
const buffer = Buffer.from(att[0].data)
return new NextResponse(buffer, {
headers: {
'Content-Type': att[0].contentType,
'Cache-Control': 'public, max-age=31536000, immutable',
},
})
} catch (error) {
console.error(error)
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 })
}
}