Baseflow / flutter_cache_manager

Generic cache manager for flutter
https://baseflow.com
MIT License
749 stars 435 forks source link

Cancelling file(Or stop downloading option) #208

Open shyamkawale opened 4 years ago

shyamkawale commented 4 years ago

🚀 Feature Requests

Describe the feature

Like if i am storing video file in cache and in between i want to cancel the ongoing download.

I tried doing this with remove method but in background downloading was still going no matter what i do ,file gets downloaded fully in background..

If there is solution available already then pls tell me how to do!!!!

Platforms affected (mark all that apply)

renefloor commented 3 years ago

Such a feature is not implemented yet.

aytunch commented 3 years ago

@shyamkawale I have the exact same need. I have a list of videos and if the user browses the list quickly, I end up with 10s of started downloads and no way to stop the past ones @renefloor are we planning on implementing this feature? it would be very beneficial for performance.

renefloor commented 3 years ago

The http library is currently implementing such a feature and when they do it won't be difficult to use it in this package. See https://github.com/dart-lang/http/issues/424

bohdan1krokhmaliuk commented 3 years ago

@renefloor

Isn't it possible to stop download process if you use IOStreamedResponse from 'package:http/io_client.dart' instead of ordinary StreamedResponse. In case, we need to cancel file download, we can simply make final socket = await _response.detachSocket() and socket.destroy() after.

/// Basic implementation of a [FileServiceResponse] for http requests.
class HttpGetResponse implements FileServiceResponse {
  HttpGetResponse(this._response);

  final DateTime _receivedTime = clock.now();

  final http.StreamedResponse _response;

  @override
  int get statusCode => _response.statusCode;

  bool _hasHeader(String name) {
    return _response.headers.containsKey(name);
  }

  String _header(String name) {
    return _response.headers[name];
  }

  @override
  Stream<List<int>> get content => _response.stream;

  @override
  int get contentLength => _response.contentLength;

  @override
  DateTime get validTill {
    // Without a cache-control header we keep the file for a week
    var ageDuration = const Duration(days: 7);
    if (_hasHeader(HttpHeaders.cacheControlHeader)) {
      final controlSettings =
          _header(HttpHeaders.cacheControlHeader).split(',');
      for (final setting in controlSettings) {
        final sanitizedSetting = setting.trim().toLowerCase();
        if (sanitizedSetting == 'no-cache') {
          ageDuration = const Duration();
        }
        if (sanitizedSetting.startsWith('max-age=')) {
          var validSeconds = int.tryParse(sanitizedSetting.split('=')[1]) ?? 0;
          if (validSeconds > 0) {
            ageDuration = Duration(seconds: validSeconds);
          }
        }
      }
    }

    return _receivedTime.add(ageDuration);
  }

  @override
  String get eTag => _hasHeader(HttpHeaders.etagHeader)
      ? _header(HttpHeaders.etagHeader)
      : null;

  @override
  String get fileExtension {
    var fileExtension = '';
    if (_hasHeader(HttpHeaders.contentTypeHeader)) {
      var contentType =
          ContentType.parse(_header(HttpHeaders.contentTypeHeader));
      fileExtension = contentType.fileExtension ?? '';
    }
    return fileExtension;
  }
}
sahhill commented 2 years ago

Hi @renefloor,

is this issue on the list, if yes, when will it be fixed?

renefloor commented 2 years ago

@sahhill this is definitely still on the list. I'm currently in a busy period personally, so don't expect new features soon. After the summer I'll have more time for open source work again.

fingerart commented 1 year ago

how's the progress?