decodeDataUri function

Uint8List decodeDataUri(
  1. String uri
)

Decodes a data: URI payload.

glTF uses several media types here (application/octet-stream, application/gltf-buffer, image/png), so the type is ignored and only the base64 payload matters.

Implementation

Uint8List decodeDataUri(String uri) {
  final comma = uri.indexOf(',');
  if (comma < 0) {
    throw GltfFormatException('Malformed data URI: no comma. "${_clip(uri)}"');
  }
  final meta = uri.substring(5, comma);
  final payload = uri.substring(comma + 1);

  if (!meta.contains('base64')) {
    // Percent-encoded payloads are legal and rare. Each `%XX` is one byte, so
    // they are unescaped straight to bytes — going through a UTF-8 string
    // would turn `%FF` into two bytes, or refuse it outright.
    return UriData.parse(uri).contentAsBytes();
  }
  return base64Decode(payload);
}