check for valid .GLD file

This commit is contained in:
2025-04-28 10:22:33 +02:00
parent 7cd19ceed8
commit fac5cc80a9

View File

@@ -47,6 +47,11 @@
success = false;
}
if (!(await check_valid_glb_file())) {
formErrors = { file: 'Keine gültige .GLD-Datei', ...formErrors };
success = false;
}
return success;
}
@@ -97,6 +102,47 @@
name = '';
files = null;
}
// `val` is hex string
function swap_endian(val) {
// from https://www.geeksforgeeks.org/bit-manipulation-swap-endianness-of-a-number/
let leftmost_byte = (val & eval(0x000000FF)) >> 0;
let left_middle_byte = (val & eval(0x0000FF00)) >> 8;
let right_middle_byte = (val & eval(0x00FF0000)) >> 16;
let rightmost_byte = (val & eval(0xFF000000)) >> 24;
leftmost_byte <<= 24;
left_middle_byte <<= 16;
right_middle_byte <<= 8;
rightmost_byte <<= 0;
let res = (leftmost_byte | left_middle_byte | right_middle_byte | rightmost_byte)
return res
}
async function check_valid_glb_file() {
// GLD Header, magic value 0x46546C67, identifies data as binary glTF, 4 bytes
// little endian!
const GLD_MAGIC = 0x46546C67;
// big endian!
let file = files[0];
let file_header = file.slice(0, 4)
let header_bytes = await file_header.bytes()
let file_header_hex = '0x' + header_bytes.toHex().toString();
if (GLD_MAGIC == swap_endian(file_header_hex)) {
return true;
} else {
return false;
}
}
</script>
<div class="mx-auto max-w-2xl">