mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-01-23 17:26:18 +01:00
734807733c
Fix OOME when downloading large files
51 lines
1.3 KiB
Dart
51 lines
1.3 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
class Download {
|
|
static Future<int> downloadUrl({
|
|
required String url,
|
|
Map<String, String>? headers,
|
|
String? mimeType,
|
|
required String filename,
|
|
bool? shouldNotify,
|
|
}) async {
|
|
return (await _channel.invokeMethod<int>("downloadUrl", <String, dynamic>{
|
|
"url": url,
|
|
"headers": headers,
|
|
"mimeType": mimeType,
|
|
"filename": filename,
|
|
"shouldNotify": shouldNotify,
|
|
}))!;
|
|
}
|
|
|
|
/// The download job has failed
|
|
static const exceptionCodeDownloadError = "downloadError";
|
|
|
|
static const _channel = MethodChannel("com.nkming.nc_photos/download");
|
|
}
|
|
|
|
class DownloadEvent {
|
|
static StreamSubscription<DownloadCompleteEvent> listenDownloadComplete() =>
|
|
_stream.listen(null);
|
|
|
|
/// User canceled the download job
|
|
static const exceptionCodeUserCanceled = "userCanceled";
|
|
|
|
static const _downloadCompleteChannel = EventChannel(
|
|
"com.nkming.nc_photos/download_event/action_download_complete");
|
|
|
|
static late final _stream = _downloadCompleteChannel
|
|
.receiveBroadcastStream()
|
|
.map((data) => DownloadCompleteEvent(
|
|
data["downloadId"],
|
|
data["uri"],
|
|
));
|
|
}
|
|
|
|
class DownloadCompleteEvent {
|
|
const DownloadCompleteEvent(this.downloadId, this.uri);
|
|
|
|
final int downloadId;
|
|
final String uri;
|
|
}
|