π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[auth] unify directory picker to use dir_utils and cleanup logs
Prateek Sunalbaa2ecaabc5d
diff --git a/mobile/apps/auth/lib/services/local_backup_service.dart b/mobile/apps/auth/lib/services/local_backup_service.dartindex e410a9d17d..a2881b094e 100644--- a/mobile/apps/auth/lib/services/local_backup_service.dart+++ b/mobile/apps/auth/lib/services/local_backup_service.dart@@ -87,10 +87,8 @@ class LocalBackupService {final fileName = _buildFileName(now, isManual: true);final filePath = '$directoryPath/$fileName';- _logger.info('Writing backup to: $filePath');final backupFile = File(filePath);await backupFile.writeAsString(encryptedJson);- _logger.info('Backup written successfully');await _manageOldBackups(directoryPath);@@ -123,38 +121,32 @@ class LocalBackupService {return success;}- // iOS with bookmark or other platforms- final subPath = Platform.isIOS ? 'EnteAuthBackups' : null;- final dir = PickedDirectory(- path: target.path!,- bookmark: target.iosBookmark,- );-- // On iOS, use withAccess for scoped access+ // iOS with bookmarkif (Platform.isIOS && target.iosBookmark != null) {+ final dir = PickedDirectory(+ path: target.path!,+ bookmark: target.iosBookmark,+ );final result = await dirUtils.withAccess(dir, (path) async {- final fullPath = subPath != null ? '$path/$subPath' : path;- await dirUtils.createDirectory(dir, subPath!);final success = await dirUtils.writeFile(- PickedDirectory(path: fullPath),+ PickedDirectory(path: path),fileName,contentBytes,);if (success) {- await _manageOldBackups(fullPath);+ await _manageOldBackups(path);}return success;});return result ?? false;}- // Non-iOS: direct file write- final basePath = subPath != null ? '${target.path}/$subPath' : target.path!;+ // Other platforms: direct file write+ final basePath = target.path!;await Directory(basePath).create(recursive: true);final filePath = '$basePath/$fileName';await File(filePath).writeAsBytes(contentBytes);await _manageOldBackups(basePath);- _logger.info('Backup written to: $filePath');return true;} catch (e, s) {_logger.severe('Failed to write backup: $e', e, s);@@ -179,7 +171,6 @@ class LocalBackupService {while (backupFiles.length > limit) {final file = backupFiles.removeAt(0);await dirUtils.deleteFile(dir, file);- _logger.info('Deleted old backup: ${file.name}');}} catch (e, s) {_logger.severe('Error pruning backups', e, s);@@ -210,9 +201,7 @@ class LocalBackupService {while (files.length > _maxBackups) {final fileToDelete = files.removeAt(0);await fileToDelete.delete();- _logger.info('Deleted old backup: ${fileToDelete.path}');}- _logger.info('Backup count is now ${files.length}. Cleanup complete.');} catch (e, s) {_logger.severe('Error during old backup cleanup', e, s);}@@ -221,8 +210,7 @@ class LocalBackupService {Future<void> deleteAllBackupsIn(String path, {String? iosBookmark}) async {try {final dirUtils = DirUtils.instance;- final subPath = Platform.isIOS ? 'EnteAuthBackups' : null;- final backupPath = subPath != null ? '$path/$subPath' : path;+ final backupPath = path;Future<void> doDelete() async {final backupDir = Directory(backupPath);@@ -247,9 +235,7 @@ class LocalBackupService {for (final file in files) {await file.delete();- _logger.info('Deleted: ${file.path}');}- _logger.info('Successfully cleaned up old backup location.');}// On iOS, use scoped access via bookmarkdiff --git a/mobile/apps/auth/lib/ui/settings/data/local_backup/local_backup_experience.dart b/mobile/apps/auth/lib/ui/settings/data/local_backup/local_backup_experience.dartindex a9fccbfaf8..eb8b97249f 100644--- a/mobile/apps/auth/lib/ui/settings/data/local_backup/local_backup_experience.dart+++ b/mobile/apps/auth/lib/ui/settings/data/local_backup/local_backup_experience.dart@@ -1,5 +1,6 @@import 'dart:io';+import 'package:dir_utils/dir_utils.dart';import 'package:ente_auth/l10n/l10n.dart';import 'package:ente_auth/services/local_backup_service.dart';import 'package:ente_auth/services/secure_storage_service.dart';@@ -9,11 +10,9 @@ import 'package:ente_auth/ui/components/buttons/button_widget.dart';import 'package:ente_auth/ui/components/dialog_widget.dart';import 'package:ente_auth/ui/components/models/button_type.dart';import 'package:ente_lock_screen/local_authentication_service.dart';-import 'package:file_picker/file_picker.dart';import 'package:flutter/material.dart';import 'package:logging/logging.dart';import 'package:path_provider/path_provider.dart';-import 'package:saf_util/saf_util.dart';import 'package:shared_preferences/shared_preferences.dart';typedef LocalBackupVariantBuilder = Widget Function(@@ -213,10 +212,8 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {}try {- _logger.info('Starting manual backup...');final success = await LocalBackupService.instance.triggerAutomaticBackup(isManual: true);- _logger.info('Manual backup result: $success');if (showSnackBar) {_showSnackBar(success@@ -374,9 +371,7 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {Future<bool> _hasStoredPassword() async {final stored = await _readStoredPassword();- final hasPassword = stored != null && stored.isNotEmpty;- _logger.info('_hasStoredPassword check: $hasPassword');- return hasPassword;+ return stored != null && stored.isNotEmpty;}Future<bool> _clearBackupPassword() async {@@ -391,14 +386,10 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {Future<String?> _readStoredPassword() async {try {- final password = await SecureStorageService.instance+ return SecureStorageService.instance.read(SecureStorageService.autoBackupPasswordKey);- _logger.info(- '_readStoredPassword: ${password != null ? "found (length: ${password.length})" : "null"}',- );- return password;} catch (e) {- _logger.severe('_readStoredPassword error: $e');+ _logger.severe('Failed to read backup password: $e');return null;}}@@ -594,10 +585,10 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {return false;}- final pickedPath = await FilePicker.platform.getDirectoryPath();- if (pickedPath != null) {+ final picked = await DirUtils.instance.pickDirectory();+ if (picked != null && picked.path.isNotEmpty) {return _persistLocation(- pickedPath,+ picked.path,successMessage: context.l10n.initialBackupCreated,);}@@ -731,11 +722,11 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {return false;} else {// Other platforms (macOS, etc.)- String? directoryPath = await FilePicker.platform.getDirectoryPath();+ final picked = await DirUtils.instance.pickDirectory();- if (directoryPath != null) {+ if (picked != null && picked.path.isNotEmpty) {final saved = await _persistLocation(- directoryPath,+ picked.path,successMessage:successMessage ?? context.l10n.locationUpdatedAndBackupCreated,);@@ -763,32 +754,19 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {}) async {final prefs = await SharedPreferences.getInstance();try {- _logger.info('iOS: Using pre-created bookmark for: $path');-// Start accessing using the bookmarkfinal accessResult = await SecurityBookmarkService.instance.startAccessingBookmark(bookmark);if (accessResult == null || !accessResult.success) {- _logger.severe('iOS: Failed to start accessing bookmark');+ _logger.severe('iOS: Failed to start accessing bookmark for: $path');return false;}- _logger.info('iOS: Scoped access granted via bookmark');try {- final backupDir = Directory('$path/EnteAuthBackups');- _logger.info('iOS: Creating backup dir');- await backupDir.create(recursive: true);- _logger.info('iOS: Backup dir created, writing initial backup');- await LocalBackupService.instance.writeBackupToDirectory(- backupDir.path,- );- _logger.info('iOS: Initial backup written successfully');+ await LocalBackupService.instance.writeBackupToDirectory(path);} finally {await SecurityBookmarkService.instance.stopAccessingBookmark(bookmark);}-- // Save both the path (for display) and bookmark (for access)- _logger.info('iOS: Saving backup path and bookmark to preferences');await prefs.setString('autoBackupPath', path);await prefs.setString(_iosBookmarkKey, bookmark);await prefs.remove(_treeUriKey);@@ -847,7 +825,6 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {}}- _logger.info('Attempting to save backup path: $path');if (await savePath(path)) {return true;}@@ -874,12 +851,8 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {}Future<bool> _pickAndPersistAndroidLocation() async {- final saf = SafUtil();- final picked = await saf.pickDirectory(- writePermission: true,- persistablePermission: true,- );- final treeUri = picked?.uri;+ final picked = await DirUtils.instance.pickDirectory();+ final treeUri = picked?.treeUri;if (treeUri == null || treeUri.isEmpty) {return false;}diff --git a/mobile/packages/dir_utils/lib/dir_utils.dart b/mobile/packages/dir_utils/lib/dir_utils.dartindex cf90fcd6f5..4be31b3538 100644--- a/mobile/packages/dir_utils/lib/dir_utils.dart+++ b/mobile/packages/dir_utils/lib/dir_utils.dart@@ -63,6 +63,7 @@ class FileInfo {final String path;final bool isDirectory;final DateTime lastModified;+/// Android SAF URI (for deletion). Null on other platforms.final String? uri;}@@ -103,10 +104,7 @@ class DirUtils {final result = await _channel.invokeMethod<Map<dynamic, dynamic>>('pickDirectory',);- if (result == null) {- _logger.info('iOS: Directory picker cancelled');- return null;- }+ if (result == null) return null;final path = result['path'] as String?;final bookmark = result['bookmark'] as String?;@@ -116,7 +114,6 @@ class DirUtils {return null;}- _logger.info('iOS: Picked directory: $path');return PickedDirectory(path: path, bookmark: bookmark);} on PlatformException catch (e) {_logger.severe('iOS: Failed to pick directory: ${e.message}');@@ -131,12 +128,8 @@ class DirUtils {writePermission: true,persistablePermission: true,);- if (picked == null) {- _logger.info('Android: Directory picker cancelled');- return null;- }+ if (picked == null) return null;- _logger.info('Android: Picked directory: ${picked.uri}');return PickedDirectory(path: picked.name,treeUri: picked.uri,@@ -150,12 +143,8 @@ class DirUtils {Future<PickedDirectory?> _pickDirectoryOther() async {try {final path = await FilePicker.platform.getDirectoryPath();- if (path == null) {- _logger.info('Other: Directory picker cancelled');- return null;- }+ if (path == null) return null;- _logger.info('Other: Picked directory: $path');return PickedDirectory(path: path);} catch (e) {_logger.severe('Other: Failed to pick directory: $e');@@ -188,15 +177,11 @@ class DirUtils {);if (result == null) return null;- final accessResult = AccessResult(+ return AccessResult(success: result['success'] as bool,path: result['path'] as String,isStale: result['isStale'] as bool,);- _logger.info(- 'iOS: Start access: success=${accessResult.success}, isStale=${accessResult.isStale}',- );- return accessResult;} on PlatformException catch (e) {_logger.severe('iOS: Failed to start access: ${e.message}');return null;@@ -219,7 +204,6 @@ class DirUtils {'stopAccess',{'bookmark': dir.bookmark},);- _logger.info('iOS: Stopped access');return result ?? false;} on PlatformException catch (e) {_logger.severe('iOS: Failed to stop access: ${e.message}');@@ -287,7 +271,6 @@ class DirUtils {content,overwrite: true,);- _logger.info('Android: Wrote file: $fileName');return true;} catch (e) {_logger.severe('Android: Failed to write file: $e');@@ -309,7 +292,6 @@ class DirUtils {'writeFile',{'path': filePath, 'content': content},);- _logger.info('iOS: Wrote file: $filePath');return result ?? false;} on PlatformException catch (e) {_logger.severe('iOS: Failed to write file: ${e.message}');@@ -326,9 +308,7 @@ class DirUtils {try {final dirPath = subPath != null ? '$basePath/$subPath' : basePath;final filePath = '$dirPath/$fileName';- final file = File(filePath);- await file.writeAsBytes(content);- _logger.info('Other: Wrote file: $filePath');+ await File(filePath).writeAsBytes(content);return true;} catch (e) {_logger.severe('Other: Failed to write file: $e');@@ -337,7 +317,10 @@ class DirUtils {}/// List files in a directory.- Future<List<FileInfo>> listFiles(PickedDirectory dir, {String? subPath}) async {+ Future<List<FileInfo>> listFiles(+ PickedDirectory dir, {+ String? subPath,+ }) async {if (Platform.isAndroid && dir.treeUri != null) {return _listFilesAndroid(dir.treeUri!);} else if (Platform.isIOS && dir.bookmark != null) {@@ -438,9 +421,7 @@ class DirUtils {Future<bool> _deleteFileAndroid(String uri, bool isDir) async {try {- final safUtil = SafUtil();- await safUtil.delete(uri, isDir);- _logger.info('Android: Deleted: $uri');+ await SafUtil().delete(uri, isDir);return true;} catch (e) {_logger.severe('Android: Failed to delete: $e');@@ -455,12 +436,9 @@ class DirUtils {'deleteFile',{'path': path},);- _logger.info('iOS: Deleted: $path');return result ?? false;} else {- final file = File(path);- await file.delete();- _logger.info('Other: Deleted: $path');+ await File(path).delete();return true;}} catch (e) {@@ -491,7 +469,6 @@ class DirUtils {'createDirectory',{'path': fullPath},);- _logger.info('iOS: Created directory: $fullPath');return result ?? false;} on PlatformException catch (e) {_logger.severe('iOS: Failed to create directory: ${e.message}');@@ -501,9 +478,7 @@ class DirUtils {Future<bool> _createDirectoryOther(String basePath, String subPath) async {try {- final fullPath = '$basePath/$subPath';- await Directory(fullPath).create(recursive: true);- _logger.info('Other: Created directory: $fullPath');+ await Directory('$basePath/$subPath').create(recursive: true);return true;} catch (e) {_logger.severe('Other: Failed to create directory: $e');