vid_download.dart 1004 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import 'dart:typed_data';
  2. import 'package:dio/dio.dart' as dio;
  3. import 'package:fis_common/logger/logger.dart';
  4. class VidFileDownloadCancel extends dio.CancelToken {}
  5. typedef VidFileDownloadProgressCallback = void Function(int count, int total);
  6. class VidFileDownloader {
  7. static dio.Dio? _httpClient;
  8. static Future<Uint8List?> download(
  9. String url, {
  10. VidFileDownloadCancel? cancelToken,
  11. VidFileDownloadProgressCallback? onReceiveProgress,
  12. }) async {
  13. try {
  14. _httpClient ??= dio.Dio(
  15. dio.BaseOptions(
  16. responseType: dio.ResponseType.bytes,
  17. sendTimeout: 1 * 1000,
  18. receiveTimeout: 10 * 1000,
  19. ),
  20. );
  21. final response = await _httpClient!.get(
  22. url,
  23. cancelToken: cancelToken,
  24. onReceiveProgress: onReceiveProgress,
  25. );
  26. final bytes = response.data as Uint8List;
  27. return bytes;
  28. } catch (e) {
  29. logger.e("VidFileDownloader call download error", e);
  30. return null;
  31. }
  32. }
  33. }