load method

  1. @override
Future<bool> load(
  1. FileAsset asset,
  2. Uint8List? embeddedBytes
)
override

Implementation

@override
Future<bool> load(FileAsset asset, Uint8List? embeddedBytes) async {
  // if the asset is embedded, or does not have a cdn uuid, do not attempt
  // to load it
  if (embeddedBytes != null || asset.cdnUuid.isEmpty) {
    return false;
  }
  // TODO (Max): Do we have a URL builder?
  // TODO (Max): We should aim to get loading errors exposed where
  // possible, this includes failed network requests but also
  // failed asset.decode

  var url = asset.cdnBaseUrl;

  if (!url.endsWith('/')) {
    url += '/';
  }
  url += formatUuid(
    uuidVariant2(asset.cdnUuid),
  );

  final res = await http.get(Uri.parse(url));

  if ((res.statusCode / 100).floor() == 2) {
    try {
      await asset.decode(
        Uint8List.view(res.bodyBytes.buffer),
      );
    } on Exception catch (e) {
      printDebugMessage(
        '''Unable to parse response ${asset.runtimeType}.
- Url: $url
- Exception: $e''',
      );
      return false;
    }

    return true;
  } else {
    return false;
  }
}