download method

Future<MediaType?> download(
  1. Uri url,
  2. File destination, {
  3. String? mediaType,
})

Implementation

Future<MediaType?> download(Uri url, File destination,
    {String? mediaType}) async {
  try {
    HttpClientResponse response =
        await HttpClient().getUrl(url).then((request) => request.close());
    if (response.statusCode >= 400) {
      throw LcpException.network(
          NetworkException(status: response.statusCode));
    }
    List<String> extensions =
        [p.extension(url.path)].where((ext) => ext.isNotEmpty).toList();
    return response.pipe(destination.openWrite()).then((_) => MediaType.of(
        mediaTypes: [mediaType].whereType<String>().toList(),
        fileExtensions: extensions));
  } on Exception catch (e, stacktrace) {
    Fimber.e("download ERROR", ex: e, stacktrace: stacktrace);
    throw LcpException.network(e);
  }
}