π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Implement file deletion and orphaned file cleanup in collections database
AmanRajSinghMourya102bd72a2ab0
diff --git a/mobile/apps/locker/lib/services/collections/collections_db.dart b/mobile/apps/locker/lib/services/collections/collections_db.dartindex 5e755a9320..a9fd586a0d 100644--- a/mobile/apps/locker/lib/services/collections/collections_db.dart+++ b/mobile/apps/locker/lib/services/collections/collections_db.dart@@ -347,6 +347,31 @@ class CollectionDB extends EnteBaseDatabase {await batch.commit();}+ Future<void> deleteFilesByUploadedFileIDs(+ List<int> uploadedFileIDs,+ ) async {+ if (uploadedFileIDs.isEmpty) {+ return;+ }++ final batch = _db.batch();++ for (final uploadedFileID in uploadedFileIDs) {+ batch.delete(+ _collectionFilesTable,+ where: 'uploaded_file_id = ?',+ whereArgs: [uploadedFileID],+ );+ batch.delete(+ _filesTable,+ where: 'uploaded_file_id = ?',+ whereArgs: [uploadedFileID],+ );+ }++ await batch.commit();+ }+Future<List<EnteFile>> getFilesInCollection(Collection collection) async {final result = await _db.rawQuery('''@@ -426,6 +451,35 @@ class CollectionDB extends EnteBaseDatabase {return result.map((row) => _mapToFile(row)).toList();}+ /// Removes orphaned files that exist in files table but have no collection mappings.+ /// This can happen if files were deleted from trash before the cleanup fix was applied.+ Future<void> cleanupOrphanedFiles() async {+ final orphanedFiles = await _db.rawQuery(+ '''+ SELECT f.uploaded_file_id, f.title+ FROM $_filesTable f+ LEFT JOIN $_collectionFilesTable cf ON f.uploaded_file_id = cf.uploaded_file_id+ WHERE cf.uploaded_file_id IS NULL+ ''',+ );++ if (orphanedFiles.isEmpty) {+ return;+ }++ final batch = _db.batch();+ for (final row in orphanedFiles) {+ final fileId = row['uploaded_file_id'];+ batch.delete(+ _filesTable,+ where: 'uploaded_file_id = ?',+ whereArgs: [fileId],+ );+ }++ await batch.commit();+ }+Map<String, dynamic> _collectionToMap(Collection collection) {return {'id': collection.id,diff --git a/mobile/apps/locker/lib/services/collections/collections_service.dart b/mobile/apps/locker/lib/services/collections/collections_service.dartindex 61fd5ef64a..6da0c90a57 100644--- a/mobile/apps/locker/lib/services/collections/collections_service.dart+++ b/mobile/apps/locker/lib/services/collections/collections_service.dart@@ -236,6 +236,18 @@ class CollectionService {}}+ /// Removes orphaned files that exist in files table but have no collection mappings.+ /// This is a one-time cleanup for files deleted from trash before the fix was applied.+ Future<void> cleanupOrphanedFiles() async {+ try {+ await _db.cleanupOrphanedFiles();+ _logger.info("Cleaned up orphaned files from database");+ } catch (e) {+ _logger.severe("Failed to cleanup orphaned files: $e");+ rethrow;+ }+ }+/// Adds a file to a collection. By default this triggers a full sync to/// update local state. Set [runSync] to false to delay syncing (useful when/// adding the same file to multiple collections during an upload).@@ -277,8 +289,8 @@ class CollectionService {await _db.deleteFilesFromCollection(collection, [file]);if (runSync) {- await sync();await TrashService.instance.syncTrash();+ await sync();}} catch (e) {_logger.severe("Failed to remove file from collections: $e");@@ -316,6 +328,9 @@ class CollectionService {}Future<void> _init() async {+ // One-time cleanup of orphaned files from before the trash deletion fix+ unawaited(cleanupOrphanedFiles());+// ignore: unawaited_futuressync().then((_) {if (Configuration.instance.getKey() != null) {diff --git a/mobile/apps/locker/lib/services/trash/trash_service.dart b/mobile/apps/locker/lib/services/trash/trash_service.dartindex eeade26eca..77e6c3cbea 100644--- a/mobile/apps/locker/lib/services/trash/trash_service.dart+++ b/mobile/apps/locker/lib/services/trash/trash_service.dart@@ -8,6 +8,7 @@ import "package:ente_events/event_bus.dart";import "package:ente_events/models/signed_in_event.dart";import 'package:ente_network/network.dart';import "package:locker/events/collections_updated_event.dart";+import 'package:locker/services/collections/collections_db.dart';import 'package:locker/services/collections/collections_service.dart';import 'package:locker/services/collections/models/collection.dart';import 'package:locker/services/collections/models/collection_file_item.dart';@@ -29,11 +30,13 @@ class TrashService {late SharedPreferences _prefs;late Dio _enteDio;late TrashDB _trashDB;+ late CollectionDB _collectionDB;Future<void> init(SharedPreferences preferences) async {_prefs = preferences;_enteDio = Network.instance.enteDio;_trashDB = TrashDB.instance;+ _collectionDB = CollectionDB.instance;if (Configuration.instance.hasConfiguredAccount()) {unawaited(syncTrash());@@ -207,6 +210,8 @@ class TrashService {data: params,);await _trashDB.delete(uniqueFileIds);++ await _collectionDB.deleteFilesByUploadedFileIDs(uniqueFileIds);} catch (e, s) {_logger.severe("failed to delete from trash", e, s);rethrow;@@ -219,11 +224,17 @@ class TrashService {final params = <String, dynamic>{};params["lastUpdatedAt"] = _getSyncTime();try {+ final trashFiles = await _trashDB.getAllTrashFiles();+ final fileIDs =+ trashFiles.map((trashFile) => trashFile.uploadedFileID!).toList();+await _enteDio.post("/trash/empty",data: params,);+await _trashDB.clearTable();+ await _collectionDB.deleteFilesByUploadedFileIDs(fileIDs);unawaited(syncTrash());} catch (e, s) {_logger.severe("failed to empty trash", e, s);