64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
// ---------------------------------------------------------------------------
|
|
// Client-side folder validation
|
|
// ---------------------------------------------------------------------------
|
|
|
|
import { REQUIRED_TEXTURES, TEXTURE_EXTENSIONS } from '@/lib/constants'
|
|
import type { TextureFile } from '@/lib/client-types'
|
|
|
|
const TEXTURE_EXT_ARRAY = [...TEXTURE_EXTENSIONS]
|
|
|
|
function getTextureType(filename: string): string | null {
|
|
const name = filename.toLowerCase().replace(/\.[^.]+$/, '')
|
|
if ((REQUIRED_TEXTURES as readonly string[]).includes(name)) return name
|
|
return null
|
|
}
|
|
|
|
export function validateFolder(files: File[]): {
|
|
model?: File
|
|
textures: TextureFile[]
|
|
errors: string[]
|
|
warnings: string[]
|
|
} {
|
|
const result: {
|
|
model?: File
|
|
textures: TextureFile[]
|
|
errors: string[]
|
|
warnings: string[]
|
|
} = {
|
|
textures: [],
|
|
errors: [],
|
|
warnings: [],
|
|
}
|
|
|
|
const modelFiles = files.filter((f) => {
|
|
const name = f.name.toLowerCase()
|
|
return name === 'model.glb' || name === 'model.gltf'
|
|
})
|
|
|
|
if (modelFiles.length === 0) {
|
|
result.errors.push('model.glb ou model.gltf manquant (obligatoire)')
|
|
} else {
|
|
result.model = modelFiles[0]
|
|
}
|
|
|
|
const textureFiles = files.filter((f) => {
|
|
const ext = f.name.slice(f.name.lastIndexOf('.')).toLowerCase()
|
|
return TEXTURE_EXT_ARRAY.includes(ext) && getTextureType(f.name) !== null
|
|
})
|
|
|
|
for (const tf of textureFiles) {
|
|
result.textures.push({ name: tf.name, file: tf })
|
|
}
|
|
|
|
const foundTextures = new Set(
|
|
result.textures.map((t) => t.name.toLowerCase().replace(/\.[^.]+$/, '')),
|
|
)
|
|
for (const req of REQUIRED_TEXTURES) {
|
|
if (!foundTextures.has(req)) {
|
|
result.warnings.push(`${req}.webp/png/jpg manquant`)
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|