Skip to content

armgit / ente

Public Imported from GitHub
0

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

Implement addToMultipleCollections method for batch adding files to collections

Aman Raj Singh Mourya4bbc0d1f4682
diff --git a/mobile/lib/ui/actions/collection/collection_file_actions.dart b/mobile/lib/ui/actions/collection/collection_file_actions.dart
index cf28e6687c..08dce93630 100644
--- a/mobile/lib/ui/actions/collection/collection_file_actions.dart
+++ b/mobile/lib/ui/actions/collection/collection_file_actions.dart
@@ -84,6 +84,103 @@ extension CollectionFileActions on CollectionActions {
}
}
+ Future<bool> addToMultipleCollections(
+ BuildContext context,
+ List<Collection> collections,
+ bool showProgressDialog, {
+ List<EnteFile>? selectedFiles,
+ }) async {
+ final List errors = [];
+ final ProgressDialog? dialog = showProgressDialog
+ ? createProgressDialog(
+ context,
+ S.of(context).uploadingFilesToAlbum,
+ isDismissible: true,
+ )
+ : null;
+ await dialog?.show();
+ try {
+ for (final collection in collections) {
+ try {
+ final List<EnteFile> files = [];
+ final List<EnteFile> filesPendingUpload = [];
+ final int currentUserID = Configuration.instance.getUserID()!;
+ for (final file in selectedFiles!) {
+ EnteFile? currentFile;
+ if (file.uploadedFileID != null) {
+ currentFile = file.copyWith();
+ } else if (file.generatedID != null) {
+ // when file is not uploaded, refresh the state from the db to
+ // ensure we have latest upload status for given file before
+ // queueing it up as pending upload
+ currentFile = await (FilesDB.instance.getFile(file.generatedID!));
+ } else if (file.generatedID == null) {
+ logger.severe("generated id should not be null");
+ }
+ if (currentFile == null) {
+ logger.severe("Failed to find fileBy genID");
+ continue;
+ }
+
+ if (currentFile.uploadedFileID == null) {
+ currentFile.collectionID = collection.id;
+ } else {
+ files.add(currentFile);
+ }
+ }
+ if (filesPendingUpload.isNotEmpty) {
+ // Newly created collection might not be cached
+ final Collection? c =
+ CollectionsService.instance.getCollectionByID(collection.id);
+ if (c != null && c.owner.id != currentUserID) {
+ final Collection uncat = await CollectionsService.instance
+ .getUncategorizedCollection();
+ for (EnteFile unuploadedFile in filesPendingUpload) {
+ final uploadedFile = await FileUploader.instance.forceUpload(
+ unuploadedFile,
+ uncat.id,
+ );
+ files.add(uploadedFile);
+ }
+ } else {
+ for (final file in filesPendingUpload) {
+ file.collectionID = collection.id;
+ }
+ // filesPendingUpload might be getting ignored during auto-upload
+ // because the user deleted these files from ente in the past.
+ await IgnoredFilesService.instance
+ .removeIgnoredMappings(filesPendingUpload);
+ await FilesDB.instance.insertMultiple(filesPendingUpload);
+ Bus.instance.fire(
+ CollectionUpdatedEvent(
+ collection.id,
+ filesPendingUpload,
+ "pendingFilesAdd",
+ ),
+ );
+ }
+ }
+ if (files.isNotEmpty) {
+ await CollectionsService.instance
+ .addOrCopyToCollection(collection.id, files);
+ }
+ } catch (e, s) {
+ logger.severe("Failed to add to album", e, s);
+ errors.add(e);
+ }
+ }
+
+ unawaited(RemoteSyncService.instance.sync(silently: true));
+ await dialog?.hide();
+ return true;
+ } catch (e, s) {
+ logger.severe("Failed to add to multiple collections", e, s);
+ await showGenericErrorDialog(context: context, error: errors.first);
+ await dialog?.hide();
+ return false;
+ }
+ }
+
Future<bool> addToCollection(
BuildContext context,
int collectionID,