putFileStream method

  1. @override
Future<File> putFileStream(
  1. String url,
  2. Stream<List<int>> source, {
  3. String? key,
  4. String? eTag,
  5. Duration maxAge = const Duration(days: 30),
  6. String fileExtension = 'file',
})
inherited

Put a byte stream in the cache. When using an existing file you can use file.openRead(). It is recommended to specify the eTag and the maxAge. When maxAge is passed and the eTag is not set the file will always be downloaded again. The fileExtension should be without a dot, for example "jpg". When cache info is available for the url that path is re-used. The returned File is saved on disk.

Implementation

@override
Future<File> putFileStream(
  String url,
  Stream<List<int>> source, {
  String? key,
  String? eTag,
  Duration maxAge = const Duration(days: 30),
  String fileExtension = 'file',
}) async {
  key ??= url;
  var cacheObject = await _store.retrieveCacheData(key);
  cacheObject ??= CacheObject(url,
      key: key,
      relativePath: '${const Uuid().v1()}'
          '.$fileExtension',
      validTill: DateTime.now().add(maxAge));

  cacheObject = cacheObject.copyWith(
    validTill: DateTime.now().add(maxAge),
    eTag: eTag,
  );

  final file = await _config.fileSystem.createFile(cacheObject.relativePath);

  // Always copy file
  final sink = file.openWrite();
  await source
      // this map is need to map UInt8List to List<int>
      .map((event) => event)
      .pipe(sink);

  _store.putFile(cacheObject);
  return file;
}