Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Moved getFile() outside lock + Implemented simple Download queue

Simon Dubrulle32ed84f48da0
diff --git a/mobile/lib/ui/viewer/actions/file_selection_actions_widget.dart b/mobile/lib/ui/viewer/actions/file_selection_actions_widget.dart
index 7410795f56..16f23e3f73 100644
--- a/mobile/lib/ui/viewer/actions/file_selection_actions_widget.dart
+++ b/mobile/lib/ui/viewer/actions/file_selection_actions_widget.dart
@@ -832,11 +832,13 @@ class _FileSelectionActionsWidgetState
);
await dialog.show();
try {
+ final downloadQueue = DownloadQueue(maxConcurrent: 5);
final futures = <Future>[];
for (final file in files) {
if (file.localID == null) {
futures.add(
- downloadToGallery(file).then((_) {
+ downloadQueue.add(() async {
+ await downloadToGallery(file);
downloadedFiles++;
dialog.update(message: S.of(context).downloading + " ($downloadedFiles/$totalFiles)");
}),
diff --git a/mobile/lib/utils/file_download_util.dart b/mobile/lib/utils/file_download_util.dart
index d91b62efef..bb5fbeb323 100644
--- a/mobile/lib/utils/file_download_util.dart
+++ b/mobile/lib/utils/file_download_util.dart
@@ -1,3 +1,5 @@
+import "dart:async";
+import "dart:collection";
import 'dart:io';
import 'package:dio/dio.dart';
@@ -189,11 +191,11 @@ Future<void> downloadToGallery(EnteFile file) async {
final bool downloadLivePhotoOnDroid =
type == FileType.livePhoto && Platform.isAndroid;
AssetEntity? savedAsset;
+ final File? fileToSave = await getFile(file);
// We use a lock to prevent synchronisation to occur while it is downloading
// as this introduces wrong entry in FilesDB due to race condition
// This is a fix for https://github.com/ente-io/ente/issues/4296
await LocalSyncService.instance.getLock().synchronized(() async {
- final File? fileToSave = await getFile(file);
//Disabling notifications for assets changing to insert the file into
//files db before triggering a sync.
await PhotoManager.stopChangeNotify();
@@ -278,3 +280,37 @@ Future<void> _saveLivePhotoOnDroid(
);
await IgnoredFilesService.instance.cacheAndInsert([ignoreVideoFile]);
}
+
+class DownloadQueue {
+ final int maxConcurrent;
+ final Queue<Future<void> Function()> _queue = Queue();
+ int _runningTasks = 0;
+
+ DownloadQueue({this.maxConcurrent = 5});
+
+ Future<void> add(Future<void> Function() task) async {
+ final completer = Completer<void>();
+ _queue.add(() async {
+ try {
+ await task();
+ completer.complete();
+ } catch (e) {
+ completer.completeError(e);
+ } finally {
+ _runningTasks--;
+ _processQueue();
+ }
+ return completer.future;
+ });
+ _processQueue();
+ return completer.future;
+ }
+
+ void _processQueue() {
+ while (_runningTasks < maxConcurrent && _queue.isNotEmpty) {
+ final task = _queue.removeFirst();
+ _runningTasks++;
+ task();
+ }
+ }
+}
\ No newline at end of file