65 lines
2.1 KiB
TypeScript
65 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { validateUploadSecret } from '@/lib/auth'
|
|
import { getRemoteFolder } from '@/lib/github'
|
|
import { classifyFileChanges } from '@/lib/diff-files'
|
|
import { getModelFolderPath } from '@/lib/model-paths'
|
|
import { ensurePreparedStagingAssets } from '@/lib/upload-staging'
|
|
|
|
export const runtime = 'nodejs'
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
/**
|
|
* POST /api/upload/check
|
|
* Build the final Git payload, then compare it with the remote folder.
|
|
*/
|
|
export async function POST(req: NextRequest) {
|
|
const authError = validateUploadSecret(req)
|
|
if (authError) return authError
|
|
|
|
let stagingId: string
|
|
|
|
try {
|
|
const body = await req.json()
|
|
stagingId = body.stagingId
|
|
if (!stagingId || typeof stagingId !== 'string') {
|
|
throw new Error('stagingId manquant')
|
|
}
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : 'Erreur inconnue'
|
|
return NextResponse.json({ success: false, error: message }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
const { folderName, filesToPush } = await ensurePreparedStagingAssets(stagingId)
|
|
const folderPath = getModelFolderPath(folderName)
|
|
const { exists, files } = await getRemoteFolder(folderPath)
|
|
|
|
if (exists) {
|
|
const remoteFileMap = new Map(files.map((file) => [file.name.toLowerCase(), file.size]))
|
|
const { fileChanges, deletedFileNames } = classifyFileChanges(filesToPush, remoteFileMap, folderPath)
|
|
|
|
const diffs: Array<{ name: string; status: 'new' | 'changed' | 'deleted' }> = []
|
|
|
|
for (const [name, status] of fileChanges.entries()) {
|
|
if (status === 'new' || status === 'changed') {
|
|
diffs.push({ name, status })
|
|
}
|
|
}
|
|
|
|
diffs.push(...deletedFileNames.map((name) => ({ name, status: 'deleted' as const })))
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
exists: true,
|
|
path: folderPath,
|
|
diffs,
|
|
})
|
|
}
|
|
|
|
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 })
|
|
}
|
|
}
|