π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Add support for resetting location data to original location data from exif or removing location data for files without location data in exif
ashilkn3c00672aefaf
diff --git a/lib/models/file/file.dart b/lib/models/file/file.dartindex a0481cd390..208cfa7901 100644--- a/lib/models/file/file.dart+++ b/lib/models/file/file.dart@@ -14,6 +14,7 @@ import 'package:photos/utils/date_time_util.dart';import 'package:photos/utils/exif_util.dart';import 'package:photos/utils/file_uploader_util.dart';+//Todo: files with no location data have lat and long set to 0.0. This should ideally be null.class EnteFile {int? generatedID;int? uploadedFileID;@@ -271,6 +272,7 @@ class EnteFile {int get width {return pubMagicMetadata?.w ?? 0;}+bool get hasDimensions {return height != 0 && width != 0;}diff --git a/lib/services/files_service.dart b/lib/services/files_service.dartindex 8f3869d925..4263348324 100644--- a/lib/services/files_service.dart+++ b/lib/services/files_service.dart@@ -1,4 +1,5 @@import 'package:dio/dio.dart';+import "package:flutter/material.dart";import "package:latlong2/latlong.dart";import 'package:logging/logging.dart';import 'package:path/path.dart';@@ -6,12 +7,17 @@ import 'package:photos/core/configuration.dart';import 'package:photos/core/network/network.dart';import 'package:photos/db/files_db.dart';import 'package:photos/extensions/list.dart';+import "package:photos/generated/l10n.dart";import 'package:photos/models/file/file.dart';import "package:photos/models/file_load_result.dart";import "package:photos/models/metadata/file_magic.dart";import 'package:photos/services/file_magic_service.dart';import "package:photos/services/ignored_files_service.dart";+import "package:photos/ui/components/action_sheet_widget.dart";+import "package:photos/ui/components/buttons/button_widget.dart";+import "package:photos/ui/components/models/button_type.dart";import 'package:photos/utils/date_time_util.dart';+import "package:photos/utils/exif_util.dart";class FilesService {late Dio _enteDio;@@ -88,27 +94,39 @@ class FilesService {Future<void> bulkEditLocationData(List<EnteFile> files,- LatLng location,+ LatLng? location,+ BuildContext context,) async {final List<EnteFile> uploadedFiles =files.where((element) => element.uploadedFileID != null).toList();final List<EnteFile> remoteFilesToUpdate = [];final Map<int, Map<String, dynamic>> fileIDToUpdateMetadata = {};- for (EnteFile remoteFile in uploadedFiles) {- // discard files not owned by user and also dedupe already processed- // files- if (remoteFile.ownerID != _config.getUserID()! ||- fileIDToUpdateMetadata.containsKey(remoteFile.uploadedFileID!)) {- continue;- }- remoteFilesToUpdate.add(remoteFile);- fileIDToUpdateMetadata[remoteFile.uploadedFileID!] = {- latKey: location.latitude,- longKey: location.longitude,- };+ if (location == null) {+ await _processFilesForBulkRemoveLocation(+ remoteFilesToUpdate,+ uploadedFiles,+ fileIDToUpdateMetadata,+ context,+ );+ } else {+ for (EnteFile remoteFile in uploadedFiles) {+ // discard files not owned by user and also dedupe already processed+ // files+ if (remoteFile.ownerID != _config.getUserID()! ||+ fileIDToUpdateMetadata.containsKey(remoteFile.uploadedFileID!)) {+ continue;+ }++ remoteFilesToUpdate.add(remoteFile);+ fileIDToUpdateMetadata[remoteFile.uploadedFileID!] = {+ latKey: location.latitude,+ longKey: location.longitude,+ };+ }}+if (remoteFilesToUpdate.isNotEmpty) {await FileMagicService.instance.updatePublicMagicMetadata(remoteFilesToUpdate,@@ -118,6 +136,94 @@ class FilesService {}}+ Future<void> _processFilesForBulkRemoveLocation(+ List<EnteFile> remoteFilesToUpdate,+ List<EnteFile> uploadedFiles,+ Map<int, Map<String, dynamic>> fileIDToUpdateMetadata,+ BuildContext context,+ ) async {+ final filesWithOgLocation = <EnteFile>[];+ final filesWithoutOgLocation = <EnteFile>[];+ for (EnteFile remoteFile in uploadedFiles) {+ // discard files not owned by user and also dedupe files+ if (remoteFile.ownerID != _config.getUserID()! ||+ filesWithoutOgLocation.any(+ (file) => file.uploadedFileID == remoteFile.uploadedFileID,+ ) ||+ filesWithOgLocation.any(+ (file) => file.uploadedFileID == remoteFile.uploadedFileID,+ )) {+ continue;+ }+ final exif = await getExif(remoteFile);+ final locationFromExif = gpsDataFromExif(exif).toLocationObj();+ locationFromExif == null+ ? filesWithoutOgLocation.add(remoteFile)+ : filesWithOgLocation.add(remoteFile);+ }+ if (filesWithOgLocation.isEmpty && filesWithoutOgLocation.isEmpty) {+ _logger.info(+ "Skipping bulkRemoveLocation, no owned files to remove location data from",+ );+ return;+ }+ if (filesWithoutOgLocation.isEmpty) {+ remoteFilesToUpdate.addAll(filesWithOgLocation);+ } else {+ final buttons = <ButtonWidget>[+ ButtonWidget(+ labelText: "All items",+ buttonType: ButtonType.neutral,+ buttonSize: ButtonSize.large,+ shouldStickToDarkTheme: true,+ buttonAction: ButtonAction.first,+ shouldSurfaceExecutionStates: true,+ isInAlert: true,+ onTap: () async {+ remoteFilesToUpdate.addAll(+ [...filesWithOgLocation, ...filesWithoutOgLocation],+ );+ },+ ),+ ButtonWidget(+ labelText: "Items with location data",+ buttonType: ButtonType.neutral,+ buttonSize: ButtonSize.large,+ shouldStickToDarkTheme: true,+ buttonAction: ButtonAction.first,+ shouldSurfaceExecutionStates: true,+ isInAlert: true,+ onTap: () async {+ remoteFilesToUpdate.addAll(filesWithOgLocation);+ },+ ),+ ButtonWidget(+ labelText: S.of(context).cancel,+ buttonType: ButtonType.secondary,+ buttonSize: ButtonSize.large,+ shouldStickToDarkTheme: true,+ buttonAction: ButtonAction.fourth,+ isInAlert: true,+ ),+ ];++ await showActionSheet(+ context: context,+ buttons: buttons,+ title: "Choose items to reset",+ body:+ "Some items selected never had location data in the first place.\n\nResetting location data for such items will delete its custom location data",+ );+ }++ for (var element in remoteFilesToUpdate) {+ fileIDToUpdateMetadata[element.uploadedFileID!] = {+ latKey: null,+ longKey: null,+ };+ }+ }+// Note: this method is not used anywhere, but it is kept for future// reference when we add bulk EditTime featureFuture<void> bulkEditTime(diff --git a/lib/ui/viewer/location/update_location_data_widget.dart b/lib/ui/viewer/location/update_location_data_widget.dartindex 7d53674855..c675856676 100644--- a/lib/ui/viewer/location/update_location_data_widget.dart+++ b/lib/ui/viewer/location/update_location_data_widget.dart@@ -68,13 +68,25 @@ class _UpdateLocationDataWidgetState extends State<UpdateLocationDataWidget> {right: 10,child: Column(children: [+ MapButton(+ icon: Icons.undo,+ onPressed: () async {+ await FilesService.instance+ .bulkEditLocationData(widget.files, null, context);+ Navigator.of(context).pop();+ },+ heroTag: "revert-to-og-location",+ ),MapButton(// icon: Icons.add_location_alt_outlined,icon: Icons.check,onPressed: () async {- await FilesService.instance- .bulkEditLocationData(widget.files, selectedLocation!);+ await FilesService.instance.bulkEditLocationData(+ widget.files,+ selectedLocation!,+ context,+ );Navigator.of(context).pop();},diff --git a/lib/utils/exif_util.dart b/lib/utils/exif_util.dartindex 7e8a99fbde..aa7ed48873 100644--- a/lib/utils/exif_util.dart+++ b/lib/utils/exif_util.dart@@ -66,7 +66,7 @@ Future<DateTime?> getCreationTimeFromEXIF(Location? locationFromExif(Map<String, IfdTag> exif) {try {- return _gpsDataFromExif(exif).toLocationObj();+ return gpsDataFromExif(exif).toLocationObj();} catch (e, s) {_logger.severe("failed to get location from exif", e, s);return null;@@ -85,7 +85,7 @@ Future<Map<String, IfdTag>> readExifAsync(File file) async {);}-GPSData _gpsDataFromExif(Map<String, IfdTag> exif) {+GPSData gpsDataFromExif(Map<String, IfdTag> exif) {final Map<String, dynamic> exifLocationData = {"lat": null,"long": null,