AddToCollection: Upload to uncategorized if files are not already uploaded

This commit is contained in:
Neeraj Gupta 2023-06-28 11:56:43 +05:30
parent c8d315c554
commit 39800de742
2 changed files with 45 additions and 16 deletions

View file

@ -10,6 +10,7 @@ import 'package:photos/models/file.dart';
import 'package:photos/models/selected_files.dart'; import 'package:photos/models/selected_files.dart';
import "package:photos/services/collections_service.dart"; import "package:photos/services/collections_service.dart";
import 'package:photos/services/favorites_service.dart'; import 'package:photos/services/favorites_service.dart';
import "package:photos/services/hidden_service.dart";
import "package:photos/services/ignored_files_service.dart"; import "package:photos/services/ignored_files_service.dart";
import "package:photos/services/remote_sync_service.dart"; import "package:photos/services/remote_sync_service.dart";
import 'package:photos/ui/actions/collection/collection_sharing_actions.dart'; import 'package:photos/ui/actions/collection/collection_sharing_actions.dart';
@ -18,6 +19,7 @@ import 'package:photos/ui/components/action_sheet_widget.dart';
import 'package:photos/ui/components/buttons/button_widget.dart'; import 'package:photos/ui/components/buttons/button_widget.dart';
import 'package:photos/ui/components/models/button_type.dart'; import 'package:photos/ui/components/models/button_type.dart';
import 'package:photos/utils/dialog_util.dart'; import 'package:photos/utils/dialog_util.dart';
import "package:photos/utils/file_uploader.dart";
import "package:photos/utils/share_util.dart"; import "package:photos/utils/share_util.dart";
import 'package:photos/utils/toast_util.dart'; import 'package:photos/utils/toast_util.dart';
import "package:receive_sharing_intent/receive_sharing_intent.dart"; import "package:receive_sharing_intent/receive_sharing_intent.dart";
@ -83,7 +85,7 @@ extension CollectionFileActions on CollectionActions {
List<SharedMediaFile>? sharedFiles, List<SharedMediaFile>? sharedFiles,
List<XFile>? pickedFiles, List<XFile>? pickedFiles,
}) async { }) async {
final dialog = showProgressDialog ProgressDialog? dialog = showProgressDialog
? createProgressDialog( ? createProgressDialog(
context, context,
S.of(context).uploadingFilesToAlbum, S.of(context).uploadingFilesToAlbum,
@ -139,9 +141,23 @@ extension CollectionFileActions on CollectionActions {
final Collection? c = final Collection? c =
CollectionsService.instance.getCollectionByID(collectionID); CollectionsService.instance.getCollectionByID(collectionID);
if (c != null && c.owner!.id != currentUserID) { if (c != null && c.owner!.id != currentUserID) {
showToast(context, S.of(context).canNotUploadToAlbumsOwnedByOthers); if (!showProgressDialog) {
await dialog?.hide(); dialog = createProgressDialog(
return false; context,
S.of(context).uploadingFilesToAlbum,
isDismissible: true,
);
await dialog.show();
}
final Collection uncat =
await CollectionsService.instance.getUncategorizedCollection();
for (File unuploadedFile in filesPendingUpload) {
final uploadedFile = await FileUploader.instance.forceUpload(
unuploadedFile,
uncat.id,
);
files.add(uploadedFile);
}
} else { } else {
// filesPendingUpload might be getting ignored during auto-upload // filesPendingUpload might be getting ignored during auto-upload
// because the user deleted these files from ente in the past. // because the user deleted these files from ente in the past.

View file

@ -24,7 +24,6 @@ import 'package:photos/models/encryption_result.dart';
import 'package:photos/models/file.dart'; import 'package:photos/models/file.dart';
import 'package:photos/models/file_type.dart'; import 'package:photos/models/file_type.dart';
import "package:photos/models/metadata/file_magic.dart"; import "package:photos/models/metadata/file_magic.dart";
import 'package:photos/models/upload_url.dart'; import 'package:photos/models/upload_url.dart';
import 'package:photos/services/collections_service.dart'; import 'package:photos/services/collections_service.dart';
import "package:photos/services/file_magic_service.dart"; import "package:photos/services/file_magic_service.dart";
@ -296,12 +295,17 @@ class FileUploader {
} }
} }
Future<File> forceUpload(File file, int collectionID) async {
return _tryToUpload(file, collectionID, true);
}
Future<File> _tryToUpload( Future<File> _tryToUpload(
File file, File file,
int collectionID, int collectionID,
bool forcedUpload, bool forcedUpload,
) async { ) async {
await checkNetworkForUpload(isForceUpload: forcedUpload); await checkNetworkForUpload(isForceUpload: forcedUpload);
if (!forcedUpload) {
final fileOnDisk = await FilesDB.instance.getFile(file.generatedID!); final fileOnDisk = await FilesDB.instance.getFile(file.generatedID!);
final wasAlreadyUploaded = fileOnDisk != null && final wasAlreadyUploaded = fileOnDisk != null &&
fileOnDisk.uploadedFileID != null && fileOnDisk.uploadedFileID != null &&
@ -311,6 +315,7 @@ class FileUploader {
debugPrint("File is already uploaded ${fileOnDisk.tag}"); debugPrint("File is already uploaded ${fileOnDisk.tag}");
return fileOnDisk; return fileOnDisk;
} }
}
if ((file.localID ?? '') == '') { if ((file.localID ?? '') == '') {
_logger.severe('Trying to upload file with missing localID'); _logger.severe('Trying to upload file with missing localID');
return file; return file;
@ -590,7 +595,9 @@ class FileUploader {
"\n existing: ${sameLocalSameCollection.tag}", "\n existing: ${sameLocalSameCollection.tag}",
); );
// should delete the fileToUploadEntry // should delete the fileToUploadEntry
if (fileToUpload.generatedID != null) {
await FilesDB.instance.deleteByGeneratedID(fileToUpload.generatedID!); await FilesDB.instance.deleteByGeneratedID(fileToUpload.generatedID!);
}
Bus.instance.fire( Bus.instance.fire(
LocalPhotosUpdatedEvent( LocalPhotosUpdatedEvent(
@ -619,7 +626,11 @@ class FileUploader {
fileMissingLocal.uploadedFileID!, fileMissingLocal.uploadedFileID!,
fileToUpload.localID!, fileToUpload.localID!,
); );
// For files selected from device, during collaborative upload, we don't
// insert entries in the FilesDB. So, we don't need to delete the entry
if (fileToUpload.generatedID != null) {
await FilesDB.instance.deleteByGeneratedID(fileToUpload.generatedID!); await FilesDB.instance.deleteByGeneratedID(fileToUpload.generatedID!);
}
Bus.instance.fire( Bus.instance.fire(
LocalPhotosUpdatedEvent( LocalPhotosUpdatedEvent(
[fileToUpload], [fileToUpload],
@ -848,7 +859,9 @@ class FileUploader {
Future<UploadURL> _getUploadURL() async { Future<UploadURL> _getUploadURL() async {
if (_uploadURLs.isEmpty) { if (_uploadURLs.isEmpty) {
await fetchUploadURLs(_queue.length); // the queue is empty, fetch at least for one file to handle force uploads
// that are not in the queue. This is to also avoid
await fetchUploadURLs(max(_queue.length, 1));
} }
try { try {
return _uploadURLs.removeFirst(); return _uploadURLs.removeFirst();