update: drag and drop + compression des textures

This commit is contained in:
Tom Boullay
2026-04-24 15:37:45 +02:00
parent 8bbc0dc0eb
commit 61a0146545
8 changed files with 302 additions and 119 deletions
+36 -20
View File
@@ -1,44 +1,60 @@
import { NextRequest, NextResponse } from 'next/server'
import { validateUploadSecret } from '@/lib/auth'
import { sanitizeFilename } from '@/lib/sanitize'
import { VALID_DESTINATIONS } from '@/lib/constants'
import { parseMultiUpload } from '@/lib/parse-upload'
import { getRemoteFolder } from '@/lib/github'
import { classifyFileChanges } from '@/lib/diff-files'
import { prepareGitAssets } from '@/lib/prepare-git-assets'
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.
* POST /api/upload/check
* Build the final Git payload, then compare it with the remote folder.
*/
export async function GET(req: NextRequest) {
export async function POST(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)) {
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}`
let folderName: string
let destination: string
let parsedFiles: Awaited<ReturnType<typeof parseMultiUpload>>['files']
try {
const parsed = await parseMultiUpload(req)
folderName = parsed.folderName
destination = parsed.destination
parsedFiles = parsed.files
} catch (err) {
const message = err instanceof Error ? err.message : 'Erreur inconnue'
return NextResponse.json({ success: false, error: message }, { status: 400 })
}
const folderPath = `public/models/${destination}/${folderName}`
try {
const { filesToPush } = await prepareGitAssets({ folderName, destination, parsedFiles })
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,
files,
diffs,
})
}