Execute deletion on remote

This commit is contained in:
vishnukvmd 2021-09-16 02:20:13 +05:30
parent fbf337c93c
commit 82aeff80cc
2 changed files with 45 additions and 10 deletions

View file

@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
import 'package:photos/core/constants.dart';
import 'package:photos/models/duplicate_files.dart';
import 'package:photos/models/file.dart';
@ -9,6 +8,7 @@ import 'package:photos/ui/common_elements.dart';
import 'package:photos/ui/detail_page.dart';
import 'package:photos/ui/thumbnail_widget.dart';
import 'package:photos/utils/data_util.dart';
import 'package:photos/utils/delete_file_util.dart';
import 'package:photos/utils/navigation_util.dart';
class DeduplicatePage extends StatefulWidget {
@ -145,7 +145,8 @@ class _DeduplicatePageState extends State<DeduplicatePage> {
color: Colors.red[700],
onPressed: _selectedFiles.isEmpty
? null
: () {
: () async {
await deleteFilesFromRemoteOnly(context, _selectedFiles.toList());
Navigator.of(context).pop(_selectedFiles.length);
},
);

View file

@ -35,11 +35,11 @@ Future<void> deleteFilesFromEverywhere(
final List<String> localSharedMediaIDs = [];
final List<String> alreadyDeletedIDs = []; // to ignore already deleted files
for (final file in files) {
if (file.localID != null) {
if (file.localID != null) {
if (!(await _localFileExist(file))) {
_logger.warning("Already deleted " + file.toString());
alreadyDeletedIDs.add(file.localID);
} else if(file.isSharedMediaToAppSandbox()) {
} else if (file.isSharedMediaToAppSandbox()) {
localSharedMediaIDs.add(file.localID);
} else {
localAssetIDs.add(file.localID);
@ -48,7 +48,8 @@ Future<void> deleteFilesFromEverywhere(
}
Set<String> deletedIDs = <String>{};
try {
deletedIDs = (await PhotoManager.editor.deleteWithIds(localAssetIDs)).toSet();
deletedIDs =
(await PhotoManager.editor.deleteWithIds(localAssetIDs)).toSet();
} catch (e, s) {
_logger.severe("Could not delete file", e, s);
}
@ -108,6 +109,37 @@ Future<void> deleteFilesFromEverywhere(
}
}
Future<void> deleteFilesFromRemoteOnly(
BuildContext context, List<File> files) async {
final dialog = createProgressDialog(context, "deleting...");
await dialog.show();
_logger.info("Trying to delete files " + files.toString());
final updatedCollectionIDs = <int>{};
final List<int> ids = [];
for (final file in files) {
updatedCollectionIDs.add(file.collectionID);
ids.add(file.uploadedFileID);
}
try {
await SyncService.instance.deleteFilesOnServer(ids);
await FilesDB.instance.deleteMultipleUploadedFiles(ids);
} catch (e) {
_logger.severe(e);
await dialog.hide();
showGenericErrorDialog(context);
rethrow;
}
for (final collectionID in updatedCollectionIDs) {
Bus.instance.fire(CollectionUpdatedEvent(
collectionID,
files.where((file) => file.collectionID == collectionID).toList(),
type: EventType.deleted,
));
}
await dialog.hide();
RemoteSyncService.instance.sync(silently: true);
}
Future<void> deleteFilesOnDeviceOnly(
BuildContext context, List<File> files) async {
final dialog = createProgressDialog(context, "deleting...");
@ -121,7 +153,7 @@ Future<void> deleteFilesOnDeviceOnly(
if (!(await _localFileExist(file))) {
_logger.warning("Already deleted " + file.toString());
alreadyDeletedIDs.add(file.localID);
} else if(file.isSharedMediaToAppSandbox()) {
} else if (file.isSharedMediaToAppSandbox()) {
localSharedMediaIDs.add(file.localID);
} else {
localAssetIDs.add(file.localID);
@ -130,7 +162,8 @@ Future<void> deleteFilesOnDeviceOnly(
}
Set<String> deletedIDs = <String>{};
try {
deletedIDs = (await PhotoManager.editor.deleteWithIds(localAssetIDs)).toSet();
deletedIDs =
(await PhotoManager.editor.deleteWithIds(localAssetIDs)).toSet();
} catch (e, s) {
_logger.severe("Could not delete file", e, s);
}
@ -169,9 +202,11 @@ Future<bool> deleteLocalFiles(
if (Platform.isAndroid) {
final androidInfo = await DeviceInfoPlugin().androidInfo;
if (androidInfo.version.sdkInt < kAndroid11SDKINT) {
deletedIDs.addAll(await _deleteLocalFilesInBatches(context, localAssetIDs));
deletedIDs
.addAll(await _deleteLocalFilesInBatches(context, localAssetIDs));
} else {
deletedIDs.addAll(await _deleteLocalFilesInOneShot(context, localAssetIDs));
deletedIDs
.addAll(await _deleteLocalFilesInOneShot(context, localAssetIDs));
}
} else {
deletedIDs.addAll(await _deleteLocalFilesInOneShot(context, localAssetIDs));
@ -251,7 +286,6 @@ Future<List<String>> _deleteLocalFilesInBatches(
return deletedIDs;
}
Future<bool> _localFileExist(File file) {
if (file.isSharedMediaToAppSandbox()) {
var localFile = io.File(getSharedMediaFilePath(file));