π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
add platform channel and util to exclude paths from iOS backup
victor003dcc2202cf
diff --git a/mobile/apps/photos/ios/Runner/AppDelegate.swift b/mobile/apps/photos/ios/Runner/AppDelegate.swiftindex 16ead278f0..60579b3ca4 100644--- a/mobile/apps/photos/ios/Runner/AppDelegate.swift+++ b/mobile/apps/photos/ios/Runner/AppDelegate.swift@@ -32,6 +32,7 @@ import workmanager_apple}GeneratedPluginRegistrant.register(with: self)+ registerBackupExclusionChannel()WorkmanagerPlugin.setPluginRegistrantCallback { registry inGeneratedPluginRegistrant.register(with: registry)}@@ -84,6 +85,38 @@ import workmanager_applereturn json["internalUser"] as? Bool ?? false}+ private func registerBackupExclusionChannel() {+ guard let controller = window?.rootViewController as? FlutterViewController else {+ NSLog("[ente] registerBackupExclusionChannel: no FlutterViewController found, backup exclusion will not be applied")+ return+ }+ let channel = FlutterMethodChannel(+ name: "io.ente.photos/backup",+ binaryMessenger: controller.binaryMessenger)+ channel.setMethodCallHandler { call, result in+ guard call.method == "excludeFromBackup" else {+ result(FlutterMethodNotImplemented)+ return+ }+ guard let args = call.arguments as? [String: Any],+ let path = args["path"] as? String+ else {+ result(FlutterError(code: "INVALID_ARGS", message: "Missing 'path'", details: nil))+ return+ }+ var url = URL(fileURLWithPath: path)+ do {+ try url.setResourceValue(true, forKey: .isExcludedFromBackupKey)+ result(true)+ } catch {+ result(FlutterError(+ code: "EXCLUDE_BACKUP_ERROR",+ message: error.localizedDescription,+ details: nil))+ }+ }+ }+override func applicationDidBecomeActive(_ application: UIApplication) {signal(SIGPIPE, SIG_IGN)}diff --git a/mobile/apps/photos/lib/utils/ios_backup_util.dart b/mobile/apps/photos/lib/utils/ios_backup_util.dartnew file mode 100644index 0000000000..781b3cc2b8--- /dev/null+++ b/mobile/apps/photos/lib/utils/ios_backup_util.dart@@ -0,0 +1,42 @@+import 'dart:io';++import 'package:flutter/foundation.dart';+import 'package:flutter/services.dart';+import 'package:logging/logging.dart';++final _logger = Logger('IosBackupUtil');+const _channel = MethodChannel('io.ente.photos/backup');++/// Marks [path] as excluded from iCloud and local backups on iOS.+///+/// Called on every app init rather than once, so the flag is re-applied after+/// device restores (where directories are recreated without the attribute).+/// The syscall is idempotent and has negligible overhead.+///+/// No-op on non-iOS platforms.+Future<void> excludeFromBackup(String path) async {+ if (!Platform.isIOS) return;+ await _invokeExcludeFromBackup(path);+}++/// Invokes the platform channel directly, without the iOS platform guard.+///+/// Exposed for unit testing so tests can exercise the channel logic and+/// logging behaviour without needing to fake [Platform.isIOS].+@visibleForTesting+Future<void> invokeExcludeFromBackup(String path) =>+ _invokeExcludeFromBackup(path);++Future<void> _invokeExcludeFromBackup(String path) async {+ try {+ final ok = await _channel.invokeMethod<bool>(+ 'excludeFromBackup',+ {'path': path},+ );+ if (ok != true) {+ _logger.warning('excludeFromBackup returned false for: $path');+ }+ } on PlatformException catch (e) {+ _logger.warning('Failed to exclude path from backup: $path β ${e.message}');+ }+}diff --git a/mobile/apps/photos/test/utils/ios_backup_util_test.dart b/mobile/apps/photos/test/utils/ios_backup_util_test.dartnew file mode 100644index 0000000000..c7733836a7--- /dev/null+++ b/mobile/apps/photos/test/utils/ios_backup_util_test.dart@@ -0,0 +1,91 @@+import 'package:flutter/services.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:logging/logging.dart';+import 'package:photos/utils/ios_backup_util.dart';++void main() {+ TestWidgetsFlutterBinding.ensureInitialized();++ const channel = MethodChannel('io.ente.photos/backup');+ const testPath = '/var/mobile/Containers/Data/Application/test/Documents';++ late List<MethodCall> calls;++ setUp(() {+ calls = [];+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger+ .setMockMethodCallHandler(channel, (call) async {+ calls.add(call);+ return true;+ });+ });++ tearDown(() {+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger+ .setMockMethodCallHandler(channel, null);+ });++ group('invokeExcludeFromBackup', () {+ test('invokes excludeFromBackup on the correct channel with the given path',+ () async {+ await invokeExcludeFromBackup(testPath);++ expect(calls, hasLength(1));+ expect(calls.first.method, 'excludeFromBackup');+ expect(calls.first.arguments, {'path': testPath});+ });++ test('logs a warning when the channel returns false', () async {+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger+ .setMockMethodCallHandler(channel, (_) async => false);++ final logs = <LogRecord>[];+ final sub = Logger('IosBackupUtil').onRecord.listen(logs.add);++ await invokeExcludeFromBackup(testPath);++ await sub.cancel();+ expect(+ logs.any(+ (r) =>+ r.level == Level.WARNING &&+ r.message.contains('excludeFromBackup returned false'),+ ),+ isTrue,+ );+ });++ test('logs a warning when the channel throws PlatformException', () async {+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger+ .setMockMethodCallHandler(channel, (_) async {+ throw PlatformException(+ code: 'EXCLUDE_BACKUP_ERROR',+ message: 'simulated native failure',+ );+ });++ final logs = <LogRecord>[];+ final sub = Logger('IosBackupUtil').onRecord.listen(logs.add);++ await invokeExcludeFromBackup(testPath);++ await sub.cancel();+ expect(+ logs.any(+ (r) =>+ r.level == Level.WARNING &&+ r.message.contains('Failed to exclude path from backup'),+ ),+ isTrue,+ );+ });+ });++ group('excludeFromBackup (platform guard)', () {+ test('does not invoke the channel on non-iOS platforms', () async {+ // Test host is Linux/macOS β Platform.isIOS is always false here.+ await excludeFromBackup(testPath);+ expect(calls, isEmpty);+ });+ });+}