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' import { readStagingRequestBody, uploadErrorResponse } from '@/lib/upload-request' import type { FileDiff } from '@/lib/types' 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 { stagingId = (await readStagingRequestBody(req)).stagingId } catch (err) { return uploadErrorResponse(err, 400) } try { const { folderName, filesToPush, deliveryMode, compressionError } = 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: FileDiff[] = [] 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, deliveryMode, compressionError, }) } return NextResponse.json({ success: true, exists: false, deliveryMode, compressionError, }) } catch (err) { return uploadErrorResponse(err, 500) } }