getImageFile method

  1. @override
Stream<FileResponse> getImageFile(
  1. String url, {
  2. String? key,
  3. Map<String, String>? headers,
  4. bool withProgress = false,
  5. int? maxHeight,
  6. int? maxWidth,
})
override

Returns a resized image file to fit within maxHeight and maxWidth.

It tries to keep the aspect ratio. It stores the resized image by adding the size to the key or url. When the resized file is not found in the cache the original is fetched from the cache or online and stored in the cache. Then it is resized and returned to the caller.

Implementation

@override
Stream<FileResponse> getImageFile(
  String url, {
  String? key,
  Map<String, String>? headers,
  bool withProgress = false,
  int? maxHeight,
  int? maxWidth,
}) async* {
  if (maxHeight == null && maxWidth == null) {
    yield* getFileStream(
      url,
      key: key,
      headers: headers,
      withProgress: withProgress,
    );
    return;
  }

  key ??= url;
  var resizedKey = 'resized';
  if (maxWidth != null) resizedKey += '_w$maxWidth';
  if (maxHeight != null) resizedKey += '_h$maxHeight';
  resizedKey += '_$key';

  final fromCache = await getFileFromCache(resizedKey);
  if (fromCache != null) {
    yield fromCache;
    if (fromCache.validTill.isAfter(DateTime.now())) {
      return;
    }
    withProgress = false;
  }

  var runningResize = _runningResizes[resizedKey];
  if (runningResize == null) {
    runningResize = _fetchedResizedFile(
      url,
      key,
      resizedKey,
      headers,
      withProgress,
      maxWidth: maxWidth,
      maxHeight: maxHeight,
    ).asBroadcastStream();
    _runningResizes[resizedKey] = runningResize;
  }
  yield* runningResize;
  _runningResizes.remove(resizedKey);
}