import { NextRequest, NextResponse } from 'next/server' import { validateUploadSecret } from '@/lib/auth' import { sanitizeFilename } from '@/lib/sanitize' import { VALID_DESTINATIONS } from '@/lib/constants' import { getRemoteFolder } from '@/lib/github' export const runtime = 'nodejs' export const dynamic = 'force-dynamic' /** * GET /api/upload/check?destination=...&folderName=... * Check if a folder already exists on the remote repo and return file SHAs. */ export async function GET(req: NextRequest) { const authError = validateUploadSecret(req) if (authError) return authError const { searchParams } = new URL(req.url) const destination = searchParams.get('destination')?.trim() const folderName = searchParams.get('folderName')?.trim() if (!destination || !folderName) { return NextResponse.json({ success: false, error: 'Parametres manquants' }, { status: 400 }) } if (!VALID_DESTINATIONS.has(destination as never)) { return NextResponse.json({ success: false, error: 'Destination invalide' }, { status: 400 }) } const safeFolderName = sanitizeFilename(folderName).replace(/[^a-zA-Z0-9-_]/g, '-') const folderPath = `public/models/${destination}/${safeFolderName}` try { const { exists, files } = await getRemoteFolder(folderPath) if (exists) { return NextResponse.json({ success: true, exists: true, path: folderPath, files, }) } return NextResponse.json({ success: true, exists: false }) } catch (err) { const message = err instanceof Error ? err.message : 'Erreur inconnue' return NextResponse.json({ success: false, error: message }, { status: 500 }) } }