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,
})
}
+9 -51
View File
@@ -1,14 +1,10 @@
import { NextRequest, NextResponse } from 'next/server'
import { join } from 'path'
import { mkdir, writeFile, readFile, unlink, rm } from 'fs/promises'
import { existsSync } from 'fs'
import { validateUploadSecret } from '@/lib/auth'
import { parseMultiUpload } from '@/lib/parse-upload'
import { compressWithBlender } from '@/lib/blender'
import { getRemoteFolder, pushAllToGitHub } from '@/lib/github'
import { buildCommitMessage } from '@/lib/commit-message'
import { classifyFileChanges } from '@/lib/diff-files'
import { TMP_DIR } from '@/lib/constants'
import { prepareGitAssets } from '@/lib/prepare-git-assets'
export const runtime = 'nodejs'
export const dynamic = 'force-dynamic'
@@ -37,52 +33,14 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ success: false, error: message }, { status: 400 })
}
// --- Process files (compress model if possible) ---
const filesToPush: { path: string; contentBase64: string }[] = []
let modelFilename = ''
let compressed = false
let compressionError: string | undefined
const textureNames: string[] = []
for (const pf of parsedFiles) {
let content = pf.buffer
if (pf.isModel) {
modelFilename = pf.filename
// Write to /tmp for Blender compression
const tmpFolder = join(TMP_DIR, folderName)
await mkdir(tmpFolder, { recursive: true })
const tmpFilePath = join(tmpFolder, pf.filename)
await writeFile(tmpFilePath, pf.buffer)
const stem = pf.filename.replace(/\.[^.]+$/, '')
const compressedPath = join(tmpFolder, `${stem}_compressed.glb`)
try {
const result = await compressWithBlender(tmpFilePath, compressedPath)
if (result.success && existsSync(compressedPath)) {
content = await readFile(compressedPath)
compressed = true
await unlink(compressedPath).catch(() => {})
} else {
compressionError = result.error
}
} finally {
// Always cleanup temp files
await unlink(tmpFilePath).catch(() => {})
await rm(tmpFolder, { recursive: true, force: true }).catch(() => {})
}
} else {
textureNames.push(pf.filename)
}
filesToPush.push({
path: `public/models/${destination}/${folderName}/${pf.filename}`,
contentBase64: content.toString('base64'),
})
}
// --- Process files (compress model + textures for Git) ---
const {
filesToPush,
modelFilename,
compressed,
compressionError,
textureNames,
} = await prepareGitAssets({ folderName, destination, parsedFiles })
// --- Detect existing files and classify changes ---
const folderPath = `public/models/${destination}/${folderName}`