All files / web/src/app/api/admin/homepage-previews/generate route.ts

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

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                                                   
import { type NextRequest, NextResponse } from 'next/server'
import { storeImage } from '@/lib/image-storage'
import { generateAndStoreImage } from '@/lib/image-generation'
import { getPreviewTarget } from '@/lib/homepage-previews'
import { withAuth } from '@/lib/auth/withAuth'

/**
 * POST /api/admin/homepage-previews/generate
 *
 * Two modes:
 *
 * 1. Canvas capture (client sends rendered PNG):
 *    Body: { id, imageData }   — imageData is a base64 data URL
 *
 * 2. AI generation:
 *    Body: { id, provider, model }
 */
export const POST = withAuth(
  async (request: NextRequest) => {
    try {
      const body = await request.json()
      const { id, imageData, provider, model } = body

      if (!id || typeof id !== 'string') {
        return NextResponse.json({ error: 'id is required' }, { status: 400 })
      }

      const target = getPreviewTarget(id)
      if (!target) {
        return NextResponse.json({ error: `Unknown preview target: ${id}` }, { status: 404 })
      }

      const storageTarget = {
        type: 'static' as const,
        relativePath: `images/homepage/${id}.png`,
      }

      // Mode 1: Client-side canvas capture
      if (imageData) {
        // Expect "data:image/png;base64,..."
        const match = (imageData as string).match(/^data:image\/png;base64,(.+)$/)
        if (!match) {
          return NextResponse.json(
            { error: 'imageData must be a base64 PNG data URL' },
            { status: 400 }
          )
        }

        const buffer = Buffer.from(match[1], 'base64')
        const result = storeImage(storageTarget, buffer)

        return NextResponse.json({
          id,
          status: 'stored',
          publicUrl: result.publicUrl,
          sizeBytes: result.sizeBytes,
        })
      }

      // Mode 2: AI generation
      if (target.type !== 'ai') {
        return NextResponse.json(
          { error: 'Canvas targets must provide imageData from client-side capture' },
          { status: 400 }
        )
      }

      if (!provider || !model) {
        return NextResponse.json(
          { error: 'AI targets require provider and model' },
          { status: 400 }
        )
      }

      const result = await generateAndStoreImage({
        provider,
        model,
        prompt: target.prompt,
        storageTarget,
        imageOptions: {
          size: { width: target.width, height: target.height },
        },
      })

      return NextResponse.json({
        id,
        status: result.status,
        publicUrl: result.publicUrl,
        sizeBytes: result.sizeBytes,
      })
    } catch (error) {
      console.error('Error generating homepage preview:', error)
      return NextResponse.json({ error: 'Failed to generate homepage preview' }, { status: 500 })
    }
  },
  { role: 'admin' }
)