stream method

Future<StreamFile> stream(
  1. String filename
)

return the file as downloadable/stream file

StreamFile file = Storage().stream('images/avatar/sample.jpeg');
return file;

or

StreamFile file = Storage().stream('images/avatar/sample.jpeg');
return response(file);

Implementation

Future<StreamFile> stream(String filename) async {
  List<int>? image = await get(filename);
  if (image == null) {
    throw NotFoundHttpException(message: 'file not found');
  }

  String? mimeType = lookupMimeType(filename, headerBytes: image);

  if (mimeType == null) {
    throw NotFoundHttpException(message: 'invalid file');
  }

  String primaryType = mimeType.split('/').first;
  String subType = mimeType.split('/').last;

  ContentType contentType = ContentType(primaryType, subType);
  Stream<List<int>> stream =
      Stream<List<int>>.fromIterable(<List<int>>[image]);
  return StreamFile(contentType, stream);
}