100 lines
3.1 KiB
TypeScript
100 lines
3.1 KiB
TypeScript
import { extname } from 'path'
|
|
import { NextRequest } from 'next/server'
|
|
import { sanitizeFilename } from './sanitize'
|
|
import { ALL_ALLOWED_EXTENSIONS, MODEL_EXTENSIONS, MAX_FILE_SIZE, TEXTURE_EXTENSIONS } from './constants'
|
|
import { getTextureNamingError } from './asset-naming'
|
|
import type { ParsedFile } from './types'
|
|
|
|
interface ParsedUpload {
|
|
folderName: string
|
|
files: ParsedFile[]
|
|
}
|
|
|
|
export async function parseMultiUpload(req: NextRequest): Promise<ParsedUpload> {
|
|
const formData = await req.formData()
|
|
const folderValue = formData.get('folderName')
|
|
const folderName = typeof folderValue === 'string' ? folderValue.trim() || 'assets' : 'assets'
|
|
const safeFolderName = sanitizeFilename(folderName).replace(/[^a-zA-Z0-9-_]/g, '-')
|
|
|
|
const rawFiles = formData.getAll('files')
|
|
const fileTypes = formData.getAll('fileTypes').filter((value): value is string => typeof value === 'string')
|
|
const textureNames = formData.getAll('textureNames').filter((value): value is string => typeof value === 'string')
|
|
|
|
const fileEntries: File[] = []
|
|
for (const entry of rawFiles) {
|
|
if (!(entry instanceof File)) {
|
|
throw new Error('Donnees de fichier invalides')
|
|
}
|
|
fileEntries.push(entry)
|
|
}
|
|
|
|
if (fileEntries.length === 0) {
|
|
throw new Error('Aucun fichier recu')
|
|
}
|
|
|
|
const parsed: ParsedFile[] = []
|
|
let modelCount = 0
|
|
|
|
for (let i = 0; i < fileEntries.length; i++) {
|
|
const file = fileEntries[i]
|
|
if (!file || file.size === 0) continue
|
|
|
|
if (file.size > MAX_FILE_SIZE) {
|
|
throw new Error(
|
|
`Fichier "${file.name}" trop volumineux (${(file.size / 1024 / 1024).toFixed(1)} MB). Maximum: ${MAX_FILE_SIZE / 1024 / 1024} MB.`,
|
|
)
|
|
}
|
|
|
|
const fileType = fileTypes[i] || 'model'
|
|
const texName = textureNames[i] || ''
|
|
|
|
const originalSafe = sanitizeFilename(file.name)
|
|
const originalExt = extname(originalSafe).toLowerCase()
|
|
|
|
if (!ALL_ALLOWED_EXTENSIONS.has(originalExt)) {
|
|
throw new Error(`Extension non autorisee: "${originalExt}"`)
|
|
}
|
|
|
|
let filename: string
|
|
if (fileType === 'texture' && texName) {
|
|
filename = sanitizeFilename(texName)
|
|
} else {
|
|
filename = originalSafe
|
|
}
|
|
|
|
const filenameExt = extname(filename).toLowerCase()
|
|
if (filenameExt !== originalExt) {
|
|
throw new Error(`Nom de fichier incoherent : ${filename} ne correspond pas a l'extension originale ${originalExt}`)
|
|
}
|
|
|
|
const textureNamingError = TEXTURE_EXTENSIONS.has(filenameExt)
|
|
? getTextureNamingError(filename)
|
|
: null
|
|
|
|
if (textureNamingError) {
|
|
throw new Error(textureNamingError)
|
|
}
|
|
|
|
const isModel = MODEL_EXTENSIONS.has(filenameExt)
|
|
if (isModel) {
|
|
if (filename.toLowerCase() !== 'model.gltf') {
|
|
throw new Error('Le modele doit etre nomme model.gltf')
|
|
}
|
|
modelCount += 1
|
|
}
|
|
const buffer = Buffer.from(await file.arrayBuffer())
|
|
|
|
parsed.push({ filename, buffer, isModel })
|
|
}
|
|
|
|
if (modelCount === 0) {
|
|
throw new Error('model.gltf manquant (obligatoire)')
|
|
}
|
|
|
|
if (modelCount > 1) {
|
|
throw new Error('Un seul fichier model.gltf est autorise')
|
|
}
|
|
|
|
return { folderName: safeFolderName, files: parsed }
|
|
}
|