refactor: stage uploads before drive and git delivery
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { validateUploadSecret } from '@/lib/auth'
|
||||
import { parseMultiUpload } from '@/lib/parse-upload'
|
||||
import { getRemoteFolder } from '@/lib/github'
|
||||
import { classifyFileChanges } from '@/lib/diff-files'
|
||||
import { prepareGitAssets } from '@/lib/prepare-git-assets'
|
||||
import { ensurePreparedStagingAssets } from '@/lib/upload-staging'
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
export const dynamic = 'force-dynamic'
|
||||
@@ -16,22 +15,22 @@ export async function POST(req: NextRequest) {
|
||||
const authError = validateUploadSecret(req)
|
||||
if (authError) return authError
|
||||
|
||||
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')
|
||||
}
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Erreur inconnue'
|
||||
return NextResponse.json({ success: false, error: message }, { status: 400 })
|
||||
}
|
||||
|
||||
const folderPath = `public/models/${folderName}`
|
||||
|
||||
try {
|
||||
const { filesToPush } = await prepareGitAssets({ folderName, parsedFiles })
|
||||
const { folderName, filesToPush } = await ensurePreparedStagingAssets(stagingId)
|
||||
const folderPath = `public/models/${folderName}`
|
||||
const { exists, files } = await getRemoteFolder(folderPath)
|
||||
|
||||
if (exists) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { validateUploadSecret } from '@/lib/auth'
|
||||
import { parseMultiUpload } from '@/lib/parse-upload'
|
||||
import { readStagedOriginalFiles } from '@/lib/upload-staging'
|
||||
import {
|
||||
mkdirRecursive,
|
||||
moveFolder,
|
||||
@@ -17,8 +17,8 @@ export const dynamic = 'force-dynamic'
|
||||
//
|
||||
// Upload **original** files (no Blender compression) to Nextcloud Drive.
|
||||
//
|
||||
// FormData fields:
|
||||
// - folderName, files[], fileTypes[], textureNames[] (same as /api/upload/git)
|
||||
// JSON body:
|
||||
// - stagingId
|
||||
// - action: "new" | "replace"
|
||||
//
|
||||
// Versioning logic:
|
||||
@@ -42,16 +42,21 @@ export async function POST(req: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
// --- Parse files (includes extra fields like "action") ---
|
||||
// --- Parse staging request ---
|
||||
let folderName: string
|
||||
let parsedFiles: Awaited<ReturnType<typeof parseMultiUpload>>['files']
|
||||
let parsedFiles: Awaited<ReturnType<typeof readStagedOriginalFiles>>['files']
|
||||
let action: string
|
||||
|
||||
try {
|
||||
const parsed = await parseMultiUpload(req)
|
||||
folderName = parsed.folderName
|
||||
parsedFiles = parsed.files
|
||||
action = parsed.extra.action?.trim() || 'new'
|
||||
const body = await req.json()
|
||||
const stagingId = body.stagingId
|
||||
if (!stagingId || typeof stagingId !== 'string') {
|
||||
throw new Error('stagingId manquant')
|
||||
}
|
||||
action = typeof body.action === 'string' ? body.action.trim() || 'new' : 'new'
|
||||
const staged = await readStagedOriginalFiles(stagingId)
|
||||
folderName = staged.folderName
|
||||
parsedFiles = staged.files
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Erreur inconnue'
|
||||
return NextResponse.json({ success: false, error: message }, { status: 400 })
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { validateUploadSecret } from '@/lib/auth'
|
||||
import { parseMultiUpload } from '@/lib/parse-upload'
|
||||
import { createStagingUpload } from '@/lib/upload-staging'
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const authError = validateUploadSecret(req)
|
||||
if (authError) return authError
|
||||
|
||||
try {
|
||||
const parsed = await parseMultiUpload(req)
|
||||
const staged = await createStagingUpload(parsed.folderName, parsed.files)
|
||||
return NextResponse.json({ success: true, ...staged })
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Erreur inconnue'
|
||||
return NextResponse.json({ success: false, error: message }, { status: 400 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user