Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Fix handling spaces in file names being URL encoded

Scott Petrovic1141307fb333
diff --git a/src/lib/processes/load-model/ZipGLTFLoader.ts b/src/lib/processes/load-model/ZipGLTFLoader.ts
index a6e4982..abba55d 100644
--- a/src/lib/processes/load-model/ZipGLTFLoader.ts
+++ b/src/lib/processes/load-model/ZipGLTFLoader.ts
@@ -39,14 +39,22 @@ export class ZipGLTFLoader {
const binNameOnly = bin_filename.split('/').pop()
gltf_json.buffers[0].uri = binNameOnly
}
- // Map all relevant files by filename only
+ // Map all relevant files by filename only (case-insensitive, handle spaces)
const fileMap: Record<string, Uint8Array> = {}
const filePromises = Object.keys(zip.files).map(async (filename) => {
const file = zip.files[filename]
if (!file.dir) {
const nameOnly = filename.split('/').pop() ?? filename
if (/\.(bin|gltf|png|jpg|jpeg|webp)$/i.test(nameOnly)) {
- fileMap[nameOnly] = new Uint8Array(await file.async('arraybuffer'))
+ // Map by original, lowercased, and decoded name
+ const buffer = new Uint8Array(await file.async('arraybuffer'))
+ fileMap[nameOnly] = buffer
+ fileMap[nameOnly.toLowerCase()] = buffer
+ try {
+ const decoded = decodeURIComponent(nameOnly)
+ fileMap[decoded] = buffer
+ fileMap[decoded.toLowerCase()] = buffer
+ } catch {}
}
}
})
@@ -54,14 +62,23 @@ export class ZipGLTFLoader {
// Patch loader to use in-memory blobs for all referenced files
this.loader.manager.setURLModifier((url: string) => {
const cleanUrl = url.split(/[?#]/)[0]
- const nameOnly = cleanUrl.split('/').pop() ?? cleanUrl
- if (fileMap[nameOnly]) {
+ let nameOnly = cleanUrl.split('/').pop() ?? cleanUrl
+ // Try original, lowercased, and decoded
+ let buffer = fileMap[nameOnly]
+ if (!buffer) buffer = fileMap[nameOnly.toLowerCase()]
+ if (!buffer) {
+ try {
+ const decoded = decodeURIComponent(nameOnly)
+ buffer = fileMap[decoded] || fileMap[decoded.toLowerCase()]
+ } catch {}
+ }
+ if (buffer) {
let mime = 'application/octet-stream'
if (/\.png$/i.test(nameOnly)) mime = 'image/png'
else if (/\.jpe?g$/i.test(nameOnly)) mime = 'image/jpeg'
else if (/\.webp$/i.test(nameOnly)) mime = 'image/webp'
else if (/\.gltf$/i.test(nameOnly)) mime = 'application/json'
- const blob = new Blob([fileMap[nameOnly]], { type: mime })
+ const blob = new Blob([buffer], { type: mime })
return URL.createObjectURL(blob)
}
return url