normalizeURI static method

String normalizeURI(
  1. String uri
)

Normalizes the passed URI.

Implementation

static String normalizeURI(String uri) {
  if (uri.startsWith(_kAssetScheme)) {
    // Handle asset:// scheme. Only for Flutter.
    final key = encodeAssetKey(uri);
    final String asset;
    if (Platform.isWindows) {
      asset = path.normalize(
        path.join(
          path.dirname(Platform.resolvedExecutable),
          'data',
          'flutter_assets',
          key,
        ),
      );
    } else if (Platform.isLinux) {
      asset = path.normalize(
        path.join(
          path.dirname(Platform.resolvedExecutable),
          'data',
          'flutter_assets',
          key,
        ),
      );
    } else if (Platform.isMacOS) {
      asset = path.normalize(
        path.join(
          path.dirname(Platform.resolvedExecutable),
          '..',
          'Frameworks',
          'App.framework',
          'Resources',
          'flutter_assets',
          key,
        ),
      );
    } else if (Platform.isIOS) {
      asset = path.normalize(
        path.join(
          path.dirname(Platform.resolvedExecutable),
          'Frameworks',
          'App.framework',
          'flutter_assets',
          key,
        ),
      );
    } else if (Platform.isAndroid) {
      asset = path.normalize(
        AndroidAssetLoader.loadSync(
          path.join(
            'flutter_assets',
            key,
          ),
        ),
      );
    } else {
      throw UnimplementedError(
        '$_kAssetScheme is not supported on ${Platform.operatingSystem}',
      );
    }
    if (!File(asset).existsSync_()) {
      throw Exception('Unable to load asset: $asset');
    }
    uri = asset;
  }
  // content:// URI support for Android.
  try {
    if (Platform.isAndroid) {
      if (Uri.parse(uri).isScheme('CONTENT')) {
        final fd = AndroidContentUriProvider.openFileDescriptorSync(uri);
        if (fd > 0) {
          return 'fd://$fd';
        }
      }
    }
  } catch (exception, stacktrace) {
    print(exception);
    print(stacktrace);
  }
  // Keep the resulting URI normalization same as used by libmpv internally.
  // [File] or network URIs.
  final parser = URIParser(uri);
  switch (parser.type) {
    case URIType.file:
      {
        return parser.file!.path;
      }
    case URIType.network:
      {
        return parser.uri!.toString();
      }
    default:
      return uri;
  }
}