Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile/photos] Address queue review feedback

Neeraj Guptaec0b4ce8655d
diff --git a/mobile/apps/photos/lib/db/gallery_downloads_db.dart b/mobile/apps/photos/lib/db/gallery_downloads_db.dart
index 3a8249318b..f58bfd4416 100644
--- a/mobile/apps/photos/lib/db/gallery_downloads_db.dart
+++ b/mobile/apps/photos/lib/db/gallery_downloads_db.dart
@@ -16,6 +16,7 @@ class GalleryDownloadsDB with SqlDbBase {
static const columnStatus = "status";
static const columnError = "error";
static const columnFilePath = "file_path";
+ static const columnSourceFile = "source_file";
static const columnCreatedAt = "created_at";
static const columnUpdatedAt = "updated_at";
@@ -39,6 +40,9 @@ class GalleryDownloadsDB with SqlDbBase {
CREATE INDEX IF NOT EXISTS gallery_download_tasks_updated_at_index
ON $tableName($columnUpdatedAt);
''',
+ '''
+ ALTER TABLE $tableName ADD COLUMN $columnSourceFile TEXT;
+ ''',
];
GalleryDownloadsDB._privateConstructor();
@@ -74,9 +78,10 @@ class GalleryDownloadsDB with SqlDbBase {
$columnStatus,
$columnError,
$columnFilePath,
+ $columnSourceFile,
$columnCreatedAt,
$columnUpdatedAt
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''',
[
task.id,
@@ -86,6 +91,7 @@ class GalleryDownloadsDB with SqlDbBase {
task.status.name,
task.error,
task.filePath,
+ task.sourceFileJson,
task.createdAt,
task.updatedAt,
],
@@ -104,6 +110,7 @@ class GalleryDownloadsDB with SqlDbBase {
$columnStatus as status,
$columnError as error,
$columnFilePath as filePath,
+ $columnSourceFile as sourceFileJson,
$columnCreatedAt as createdAt,
$columnUpdatedAt as updatedAt
FROM $tableName
diff --git a/mobile/apps/photos/lib/module/download/gallery_download_queue_service.dart b/mobile/apps/photos/lib/module/download/gallery_download_queue_service.dart
index 7df95ff04e..6633ea851c 100644
--- a/mobile/apps/photos/lib/module/download/gallery_download_queue_service.dart
+++ b/mobile/apps/photos/lib/module/download/gallery_download_queue_service.dart
@@ -1,4 +1,5 @@
import "dart:async";
+import "dart:convert";
import "dart:io";
import "package:collection/collection.dart";
@@ -11,6 +12,7 @@ import "package:photos/db/gallery_downloads_db.dart";
import "package:photos/events/gallery_downloads_events.dart";
import "package:photos/events/user_logged_out_event.dart";
import "package:photos/models/file/file.dart";
+import "package:photos/models/file/file_type.dart";
import "package:photos/module/download/manager.dart";
import "package:photos/module/download/task.dart";
import "package:photos/service_locator.dart";
@@ -95,7 +97,9 @@ class GalleryDownloadQueueService {
);
bool get isCompletionBannerVisible =>
- _showCompletionBanner && completedCount > 0 && _allTasksAreTerminal;
+ _showCompletionBanner &&
+ _allTasksAreTerminal &&
+ (completedCount > 0 || unavailableCount > 0);
bool get isBannerVisible {
if (_tasks.isEmpty) {
@@ -179,9 +183,17 @@ class GalleryDownloadQueueService {
if (uploadID == null || fileSize == null || fileSize <= 0) {
continue;
}
- _queuedFilesByID[uploadID] =
+ final queuedFile =
file.isRemoteFile ? file.copyWith() : file.copyWith(localID: null);
+ _queuedFilesByID[uploadID] = queuedFile;
+ final sourceFileJson = _serializeQueuedFile(queuedFile);
if (_tasks.containsKey(uploadID)) {
+ final existingTask = _tasks[uploadID];
+ if (existingTask != null && existingTask.sourceFileJson == null) {
+ await _updateTask(
+ existingTask.copyWith(sourceFileJson: sourceFileJson),
+ );
+ }
duplicateCount++;
continue;
}
@@ -192,6 +204,7 @@ class GalleryDownloadQueueService {
status: DownloadStatus.pending,
createdAt: createTimestamp++,
updatedAt: createTimestamp,
+ sourceFileJson: sourceFileJson,
);
_tasks[uploadID] = task;
await _db.upsertTask(task);
@@ -299,6 +312,10 @@ class GalleryDownloadQueueService {
);
}
_tasks[task.id] = task;
+ final queuedFile = _deserializeQueuedFile(task.sourceFileJson);
+ if (queuedFile != null) {
+ _queuedFilesByID[task.id] = queuedFile;
+ }
}
if (staleIDs.isNotEmpty) {
@@ -438,6 +455,15 @@ class GalleryDownloadQueueService {
Future<void> _downloadAndSaveToGallery(int fileID) async {
EnteFile? file = _queuedFilesByID[fileID];
+ if (file == null) {
+ final task = _tasks[fileID];
+ if (task != null) {
+ file = _deserializeQueuedFile(task.sourceFileJson);
+ if (file != null) {
+ _queuedFilesByID[fileID] = file;
+ }
+ }
+ }
if (file == null) {
final filesMap = await FilesDB.instance.getFileIDToFileFromIDs([fileID]);
file = filesMap[fileID];
@@ -566,4 +592,63 @@ class GalleryDownloadQueueService {
void _emitUpdatedEvent() {
Bus.instance.fire(GalleryDownloadsUpdatedEvent());
}
+
+ String _serializeQueuedFile(EnteFile file) {
+ return jsonEncode({
+ "uploadedFileID": file.uploadedFileID,
+ "ownerID": file.ownerID,
+ "collectionID": file.collectionID,
+ "title": file.title,
+ "fileType": file.fileType.index,
+ "encryptedKey": file.encryptedKey,
+ "keyDecryptionNonce": file.keyDecryptionNonce,
+ "fileDecryptionHeader": file.fileDecryptionHeader,
+ "thumbnailDecryptionHeader": file.thumbnailDecryptionHeader,
+ "metadataDecryptionHeader": file.metadataDecryptionHeader,
+ "fileSize": file.fileSize,
+ "pubMmdEncodedJson": file.pubMmdEncodedJson,
+ "pubMmdVersion": file.pubMmdVersion,
+ });
+ }
+
+ EnteFile? _deserializeQueuedFile(String? sourceFileJson) {
+ if (sourceFileJson == null || sourceFileJson.isEmpty) {
+ return null;
+ }
+ try {
+ final Map<String, dynamic> map =
+ jsonDecode(sourceFileJson) as Map<String, dynamic>;
+ final fileTypeIndex = map["fileType"];
+ if (fileTypeIndex is! int) {
+ return null;
+ }
+ final file = EnteFile()
+ ..uploadedFileID = map["uploadedFileID"] as int?
+ ..ownerID = map["ownerID"] as int?
+ ..collectionID = map["collectionID"] as int?
+ ..title = map["title"] as String?
+ ..fileType = getFileType(fileTypeIndex)
+ ..encryptedKey = map["encryptedKey"] as String?
+ ..keyDecryptionNonce = map["keyDecryptionNonce"] as String?
+ ..fileDecryptionHeader = map["fileDecryptionHeader"] as String?
+ ..thumbnailDecryptionHeader =
+ map["thumbnailDecryptionHeader"] as String?
+ ..metadataDecryptionHeader = map["metadataDecryptionHeader"] as String?
+ ..fileSize = map["fileSize"] as int?
+ ..pubMmdEncodedJson = map["pubMmdEncodedJson"] as String?
+ ..pubMmdVersion = map["pubMmdVersion"] as int? ?? 0;
+ if (file.uploadedFileID == null ||
+ file.collectionID == null ||
+ file.fileSize == null ||
+ file.fileDecryptionHeader == null ||
+ file.encryptedKey == null ||
+ file.keyDecryptionNonce == null) {
+ return null;
+ }
+ return file;
+ } catch (e, s) {
+ _logger.warning("Failed to deserialize queued source file", e, s);
+ return null;
+ }
+ }
}
diff --git a/mobile/apps/photos/lib/module/download/task.dart b/mobile/apps/photos/lib/module/download/task.dart
index 3c490370bc..0ad8852c93 100644
--- a/mobile/apps/photos/lib/module/download/task.dart
+++ b/mobile/apps/photos/lib/module/download/task.dart
@@ -13,6 +13,7 @@ class DownloadTask {
final int totalBytes;
final int createdAt;
final int updatedAt;
+ final String? sourceFileJson;
int bytesDownloaded;
DownloadStatus status;
String? error;
@@ -24,6 +25,7 @@ class DownloadTask {
required this.totalBytes,
int? createdAt,
int? updatedAt,
+ this.sourceFileJson,
this.bytesDownloaded = 0,
this.status = DownloadStatus.pending,
this.error,
@@ -46,6 +48,7 @@ class DownloadTask {
'totalBytes': totalBytes,
'createdAt': createdAt,
'updatedAt': updatedAt,
+ 'sourceFileJson': sourceFileJson,
'bytesDownloaded': bytesDownloaded,
'status': status.name,
'error': error,
@@ -58,6 +61,7 @@ class DownloadTask {
totalBytes: map['totalBytes'],
createdAt: map['createdAt'],
updatedAt: map['updatedAt'],
+ sourceFileJson: map['sourceFileJson'],
bytesDownloaded: map['bytesDownloaded'] ?? 0,
status: DownloadStatus.values.byName(map['status']),
error: map['error'],
@@ -67,6 +71,7 @@ class DownloadTask {
DownloadTask copyWith({
int? createdAt,
int? updatedAt,
+ String? sourceFileJson,
int? bytesDownloaded,
DownloadStatus? status,
String? error,
@@ -78,6 +83,7 @@ class DownloadTask {
totalBytes: totalBytes,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? DateTime.now().microsecondsSinceEpoch,
+ sourceFileJson: sourceFileJson ?? this.sourceFileJson,
bytesDownloaded: bytesDownloaded ?? this.bytesDownloaded,
status: status ?? this.status,
error: error ?? this.error,
diff --git a/mobile/apps/photos/scripts/internal_changes.txt b/mobile/apps/photos/scripts/internal_changes.txt
index 83c3153d4f..7e53a57da6 100644
--- a/mobile/apps/photos/scripts/internal_changes.txt
+++ b/mobile/apps/photos/scripts/internal_changes.txt
@@ -1,3 +1,4 @@
+- Neeraj: (I) Add internal-only resumable gallery download queue with persistence/restart fixes and completion banner updates
- Ashil: Enable single file download for locally existing uploaded files.
- Laurens: (i) Use usearch vectorDB for magic search and discover section
- Laurens: Make it possible to ignore already named persons