flutter_cache_manager icon indicating copy to clipboard operation
flutter_cache_manager copied to clipboard

Cancelling file(Or stop downloading option)

Open shyamkawale opened this issue 4 years ago • 7 comments

🚀 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)

  • [x] :iphone: iOS
  • [x] :robot: Android

shyamkawale avatar Aug 05 '20 06:08 shyamkawale

Such a feature is not implemented yet.

renefloor avatar Oct 08 '20 08:10 renefloor

@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.

aytunch avatar Jan 10 '21 16:01 aytunch

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

renefloor avatar Jan 11 '21 14:01 renefloor

@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;
  }
}

bohdan1krokhmaliuk avatar Mar 01 '21 20:03 bohdan1krokhmaliuk

Hi @renefloor,

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

sahhill avatar Apr 05 '22 10:04 sahhill

@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.

renefloor avatar Apr 05 '22 11:04 renefloor

how's the progress?

fingerart avatar Mar 27 '23 09:03 fingerart