resolve method

Uri resolve(
  1. String reference
)

Converts reference into a fetchable URI.

HTTP(S) references are returned unchanged. IPFS references and bare CIDs are routed through template.

Implementation

Uri resolve(String reference) {
  final value = reference.trim();
  if (value.isEmpty) {
    throw const FormatException('IPFS reference cannot be empty.');
  }

  final direct = Uri.tryParse(value);
  if (direct != null &&
      (direct.scheme == 'https' || direct.scheme == 'http')) {
    return direct;
  }

  final ipfsPath = _ipfsPath(value);
  final encodedPath = ipfsPath.split('/').map(Uri.encodeComponent).join('/');
  return Uri.parse(template.replaceFirst('{cid}', encodedPath));
}