// --------------------------------------------------------------------------- // 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 } /** Discriminated union: either valid (with model) or invalid (with errors). */ export type ValidationResult = | { ok: true; model: File; textures: TextureFile[]; warnings: string[] } | { ok: false; errors: string[] } export function validateFolder(files: File[]): ValidationResult { const textures: TextureFile[] = [] const warnings: string[] = [] const errors: string[] = [] const modelFiles = files.filter((f) => { const name = f.name.toLowerCase() return name === 'model.glb' || name === 'model.gltf' }) if (modelFiles.length === 0) { return { ok: false, errors: ['model.glb ou model.gltf manquant (obligatoire)'] } } 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) { textures.push({ name: tf.name, file: tf }) } const foundTextures = new Set( textures.map((t) => t.name.toLowerCase().replace(/\.[^.]+$/, '')), ) for (const req of REQUIRED_TEXTURES) { if (!foundTextures.has(req)) { warnings.push(`${req}.webp/png/jpg manquant`) } } if (errors.length > 0) { return { ok: false, errors } } return { ok: true, model: modelFiles[0], textures, warnings } }