// --------------------------------------------------------------------------- // Client-side folder validation // --------------------------------------------------------------------------- import { ASSET_EXTENSIONS, TEXTURE_EXTENSIONS } from '@/lib/constants' import type { TextureFile } from '@/lib/client-types' const SUPPORT_FILE_EXT_ARRAY = [...TEXTURE_EXTENSIONS, ...ASSET_EXTENSIONS] interface GltfBufferReference { uri?: unknown } interface GltfJson { buffers?: GltfBufferReference[] } /** 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[] } function isGltfJson(value: unknown): value is GltfJson { return typeof value === 'object' && value !== null } function getReferencedBufferNames(gltf: GltfJson) { return (gltf.buffers || []) .map((buffer) => (typeof buffer.uri === 'string' ? buffer.uri : undefined)) .filter((uri): uri is string => typeof uri === 'string' && uri.length > 0) .filter((uri) => !uri.startsWith('data:')) .map((uri) => decodeURIComponent(uri.split(/[?#]/)[0] || '').split(/[\\/]/).pop()?.toLowerCase()) .filter((filename): filename is string => Boolean(filename)) } async function getGltfWarnings(model: File, supportFiles: File[]) { const warnings: string[] = [] let parsed: unknown try { parsed = JSON.parse(await model.text()) } catch { return warnings } if (!isGltfJson(parsed)) return warnings const supportFilenames = new Set(supportFiles.map((file) => file.name.toLowerCase())) const binFiles = supportFiles.filter((file) => file.name.toLowerCase().endsWith('.bin')) for (const bufferName of getReferencedBufferNames(parsed)) { if (!bufferName.endsWith('.bin') || supportFilenames.has(bufferName)) continue if (binFiles.length === 1) { warnings.push( `model.gltf reference ${bufferName} mais le dossier contient ${binFiles[0].name}. La preview peut utiliser ${binFiles[0].name}, mais l'upload final risque d'etre casse. Veillez changer le nom du fichier .bin pour ne pas casser l'export.`, ) continue } warnings.push(`model.gltf reference ${bufferName}, mais ce fichier .bin est absent du dossier.`) } return warnings } export async function validateFolder(files: File[]): Promise { const textures: TextureFile[] = [] const errors: string[] = [] const modelFiles = files.filter((f) => { const name = f.name.toLowerCase() return name === 'model.gltf' }) if (modelFiles.length === 0) { return { ok: false, errors: ['model.gltf manquant (obligatoire)'] } } if (modelFiles.length > 1) { return { ok: false, errors: ['Un seul fichier model.gltf est autorise'] } } const supportFiles = files.filter((f) => { const ext = f.name.slice(f.name.lastIndexOf('.')).toLowerCase() return SUPPORT_FILE_EXT_ARRAY.includes(ext) }) for (const tf of supportFiles) { textures.push({ name: tf.name, file: tf }) } if (errors.length > 0) { return { ok: false, errors } } const warnings = await getGltfWarnings(modelFiles[0], supportFiles) return { ok: true, model: modelFiles[0], textures, warnings } }