debut refacto

This commit is contained in:
Tom Boullay
2026-04-14 14:18:40 +02:00
parent ab9685b6ee
commit e9ae6ffc41
13 changed files with 721 additions and 554 deletions
+50
View File
@@ -0,0 +1,50 @@
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 })
}
}