All files / Bandstand/src/app/api/band/icon route.ts

0% Statements 0/41
0% Branches 0/1
0% Functions 0/1
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                                                                                               
export const dynamic = 'force-dynamic'
 
import {prisma} from '@/lib/db'
import {requireAdmin} from '@/lib/guard'
import {sniffImage} from '@/lib/avatars'
import {logActivity} from '@/lib/songs'
import {route} from '@/lib/route'
 
// A 512px PNG is ~50-300 KB; this refuses anything that skipped resizing
const MAX_BYTES = 800_000
 
/** The band's home-screen icon. Body: the image bytes (made in the browser). */
export const PUT = route(async (req: Request) => {
  const {user, band} = await requireAdmin()
  const data = Buffer.from(await req.arrayBuffer())
  if (!data.length || data.length > MAX_BYTES)
    return new Response('Payload Too Large', {status: 413})
  const mime = sniffImage(data)
  if (!mime) return new Response('Unsupported Media Type', {status: 415})
  const at = new Date()
  await prisma.$transaction(async (tx) => {
    await tx.bandIcon.upsert({
      where: {bandId: band.id},
      create: {bandId: band.id, mime, data},
      update: {mime, data},
    })
    await tx.band.update({where: {id: band.id}, data: {iconAt: at}})
    await logActivity(tx, {
      bandId: band.id,
      userId: user.id,
      action: 'band.icon',
      targetType: 'band',
      targetId: band.id,
      summary: 'changed the home-screen icon',
    })
  })
  return Response.json({iconAt: at.toISOString()})
})
 
export const DELETE = route(async () => {
  const {band} = await requireAdmin()
  await prisma.$transaction([
    prisma.bandIcon.deleteMany({where: {bandId: band.id}}),
    prisma.band.update({where: {id: band.id}, data: {iconAt: null}}),
  ])
  return new Response(null, {status: 204})
})