partsFromHttpUrl function

Map<String, String?>? partsFromHttpUrl(
  1. String url
)

Returns a path from a given http:// or https:// URL.

If url fails to parse, null is returned If no path exists, the root path will be returned.

Implementation

Map<String, String?>? partsFromHttpUrl(String url) {
  assert(url.startsWith('http'));
  String? decodedUrl = _decodeHttpUrl(url);
  if (decodedUrl == null) {
    return null;
  }

  // firebase storage url
  if (decodedUrl.contains(_firebaseStorageHost) ||
      decodedUrl.contains('localhost')) {
    String origin;
    if (decodedUrl.contains('localhost')) {
      Uri uri = Uri.parse(url);
      origin = '^http?://${uri.host}:${uri.port}';
    } else {
      origin = '^https?://$_firebaseStorageHost';
    }

    RegExp firebaseStorageRegExp = RegExp(
      '$origin$_optionalPort/$_version/b/$_bucketDomain/o$_firebaseStoragePath',
      caseSensitive: false,
    );

    RegExpMatch? match = firebaseStorageRegExp.firstMatch(decodedUrl);

    if (match == null) {
      return null;
    }

    return {
      'bucket': match.group(1),
      'path': match.group(3),
    };
    // google cloud storage url
  } else {
    RegExp cloudStorageRegExp = RegExp(
      '^https?://$_cloudStorageHost$_optionalPort/$_bucketDomain/$_cloudStoragePath',
      caseSensitive: false,
    );

    RegExpMatch? match = cloudStorageRegExp.firstMatch(decodedUrl);

    if (match == null) {
      return null;
    }

    return {
      'bucket': match.group(1),
      'path': match.group(2),
    };
  }
}