refactor: stage uploads before drive and git delivery
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { validateUploadSecret } from '@/lib/auth'
|
||||
import { parseMultiUpload } from '@/lib/parse-upload'
|
||||
import { getRemoteFolder, pushAllToGitHub } from '@/lib/github'
|
||||
import { buildCommitMessage } from '@/lib/commit-message'
|
||||
import { classifyFileChanges } from '@/lib/diff-files'
|
||||
import { prepareGitAssets } from '@/lib/prepare-git-assets'
|
||||
import { cleanupStagingUpload, ensurePreparedStagingAssets, readStagedManifest } from '@/lib/upload-staging'
|
||||
import { acquireUploadLock, releaseUploadLock } from '@/lib/upload-lock'
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
@@ -19,14 +18,17 @@ export async function POST(req: NextRequest) {
|
||||
const authError = validateUploadSecret(req)
|
||||
if (authError) return authError
|
||||
|
||||
// --- Parse all files ---
|
||||
let folderName: string
|
||||
let parsedFiles: Awaited<ReturnType<typeof parseMultiUpload>>['files']
|
||||
let stagingId: string
|
||||
|
||||
try {
|
||||
const parsed = await parseMultiUpload(req)
|
||||
folderName = parsed.folderName
|
||||
parsedFiles = parsed.files
|
||||
const body = await req.json()
|
||||
stagingId = body.stagingId
|
||||
if (!stagingId || typeof stagingId !== 'string') {
|
||||
throw new Error('stagingId manquant')
|
||||
}
|
||||
const manifest = await readStagedManifest(stagingId)
|
||||
folderName = manifest.folderName
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Erreur inconnue'
|
||||
return NextResponse.json({ success: false, error: message }, { status: 400 })
|
||||
@@ -47,7 +49,7 @@ export async function POST(req: NextRequest) {
|
||||
compressed,
|
||||
compressionError,
|
||||
assetSummaries,
|
||||
} = await prepareGitAssets({ folderName, parsedFiles })
|
||||
} = await ensurePreparedStagingAssets(stagingId)
|
||||
|
||||
// --- Detect existing files and classify changes ---
|
||||
const folderPath = `public/models/${folderName}`
|
||||
@@ -67,6 +69,7 @@ export async function POST(req: NextRequest) {
|
||||
|
||||
// If nothing changed, don't create an empty commit
|
||||
if (changedFilesToPush.length === 0 && deletePaths.length === 0) {
|
||||
await cleanupStagingUpload(stagingId).catch(() => {})
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
folderName,
|
||||
@@ -90,6 +93,7 @@ export async function POST(req: NextRequest) {
|
||||
// --- Push all in one commit ---
|
||||
try {
|
||||
const { commitUrl } = await pushAllToGitHub(changedFilesToPush, deletePaths, commitMessage)
|
||||
await cleanupStagingUpload(stagingId).catch(() => {})
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
|
||||
Reference in New Issue
Block a user