enqueue method
Implementation
Future<UDownloadTask> enqueue(UDownloadRequest request) async {
await init();
if (request.url.isEmpty && request.sourceId == null) throw ArgumentError("A download needs a url or a sourceId.");
if (request.destination.target == UDownloadTarget.file && UDownloadPlatform.isWeb) throw UnsupportedError("File destinations are native only.");
// Same source to the same place while still running: reuse it instead of downloading twice.
for (final UDownloadTask existing in _tasks) {
if (!existing.isFinished &&
existing.request.url == request.url &&
existing.request.sourceId == request.sourceId &&
jsonEncode(existing.request.destination.toJson()) == jsonEncode(request.destination.toJson())) {
return existing;
}
}
final UDownloadTask task = UDownloadTask(id: UUUID.uuidV4(), request: request);
final UDownloadDestination destination = request.destination;
if (request.conflict == UDownloadConflict.skip && destination.target == UDownloadTarget.storage && UFileStorage.contains(destination.key!, bucket: destination.bucket)) {
task
..status = UDownloadStatus.completed
..result = destination.key
..completedAt = DateTime.now();
task.complete();
return task;
}
_tasks.add(task);
if (request.startAt != null && request.startAt!.isAfter(DateTime.now())) {
task.status = UDownloadStatus.scheduled;
_schedule(task);
}
_changed(task);
_pump();
return task;
}