update: drag and drop + compression des textures
This commit is contained in:
@@ -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}`
|
||||
|
||||
Reference in New Issue
Block a user