load method Null safety

Future<LoadResult> load(
  1. {required String url,
  2. String group = 'no_group'}
)

Load file from server or internal db.

Implementation

Future<LoadResult> load({
  required String url,
  String group = 'no_group',
}) async {
  final cache = await _db.getFile(url);
  if (cache != null) {
    return LoadResult.cached;
  }

  try {
    final response = await http.get(Uri.parse(url));
    if (response.statusCode == 200) {
      String? filename;
      final disposition = response.headers['Content-Disposition']?.toString();
      if (disposition != null) {
        final reg = RegExp('''/filename[^;=\n]*=((['"]).*?2|[^;\n]*)/''');
        final match = reg.firstMatch(disposition);
        filename = match?[0];
      }

      final entry = createEntity(
        url: url,
        group: group,
        bytes: response.bodyBytes,
        filename: filename,
      );
      _db.insertCache(entry);

      return LoadResult.load;
    }
  } on SocketException {
    return LoadResult.connectionError;
  } on HttpException {
    return LoadResult.urlError;
  } on FormatException {
    return LoadResult.formatError;
  }

  return LoadResult.failed;
}