BloomResponse.file constructor

BloomResponse.file(
  1. File file, {
  2. String? contentType,
  3. int statusCode = 200,
  4. Map<String, String>? headers,
})

Streams file from disk without loading its entire contents into memory.

Unlike BloomResponse.stream, sets content-length from File.lengthSync, enabling clients to calculate download progress without chunked transfer overhead.

Throws FileSystemException if file does not exist.

Example

final res = BloomResponse.file(File('assets/report.pdf'), contentType: 'application/pdf');

Implementation

factory BloomResponse.file(
  File file, {
  String? contentType,
  int statusCode = 200,
  Map<String, String>? headers,
}) {
  if (!file.existsSync()) {
    throw FileSystemException('File not found', file.path);
  }
  return BloomResponse.stream(
    file.openRead(),
    statusCode: statusCode,
    contentType: contentType,
    headers: {
      'content-length': file.lengthSync().toString(),
      ...?headers,
    },
  );
}