An open-source cross-platform alternative to AirDrop
fix(macos): persist destination folder access across app launches (#1979)
Shlomof2f3b79bc30e
diff --git a/app/lib/config/init.dart b/app/lib/config/init.dartindex 02da5588..bf2b5b12 100644--- a/app/lib/config/init.dart+++ b/app/lib/config/init.dart@@ -190,12 +190,6 @@ Future<void> postInit(BuildContext context, Ref ref, bool appStart) async {if (appStart) {if (defaultTargetPlatform == TargetPlatform.macOS) {- switch (ref.read(settingsProvider).destination) {- case String path:- await requestFolderAccess(path);- break;- }-// handle dropped filespendingFilesStream.listen((files) {ref.global.dispatchAsync(_HandleAppStartArgumentsAction(diff --git a/app/lib/pages/tabs/settings_tab.dart b/app/lib/pages/tabs/settings_tab.dartindex 2c265924..039a9887 100644--- a/app/lib/pages/tabs/settings_tab.dart+++ b/app/lib/pages/tabs/settings_tab.dart@@ -203,13 +203,16 @@ class SettingsTab extends StatelessWidget {onPressed: () async {if (vm.settings.destination != null) {await ref.notifier(settingsProvider).setDestination(null);+ if (defaultTargetPlatform == TargetPlatform.macOS) {+ await removeExistingDestinationAccess();+ }return;}final directory = await pickDirectoryPath();if (directory != null) {if (defaultTargetPlatform == TargetPlatform.macOS) {- await requestFolderAccess(directory);+ await persistDestinationFolderAccess(directory);}await ref.notifier(settingsProvider).setDestination(directory);}diff --git a/app/lib/util/native/macos_channel.dart b/app/lib/util/native/macos_channel.dartindex b391eb81..962c68e8 100644--- a/app/lib/util/native/macos_channel.dart+++ b/app/lib/util/native/macos_channel.dart@@ -14,8 +14,12 @@ Future<void> setupStatusBar() async {});}-Future<void> requestFolderAccess(String path) async {- await _methodChannel.invokeMethod('requestFolderAccess', path);+Future<void> removeExistingDestinationAccess() async {+ await _methodChannel.invokeMethod('removeExistingDestinationAccess');+}++Future<void> persistDestinationFolderAccess(String path) async {+ await _methodChannel.invokeMethod('persistDestinationFolderAccess', path);}Future<void> updateDockProgress(double progress) async {diff --git a/app/macos/Runner/AppDelegate.swift b/app/macos/Runner/AppDelegate.swiftindex 5da383bd..3d7900bc 100644--- a/app/macos/Runner/AppDelegate.swift+++ b/app/macos/Runner/AppDelegate.swift@@ -48,6 +48,8 @@ class AppDelegate: FlutterAppDelegate {DockProgress.style = .squircle(color: localsendBrandColor)isLaunchedAsLoginItem = LaunchAtLogin.wasLaunchedAtLogin++ restoreDestinationFolderAccess()}override func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {@@ -165,18 +167,13 @@ class AppDelegate: FlutterAppDelegate {let i18n = call.arguments as! [String: String]setupStatusBarItem(i18n: i18n)result(nil)- case "requestFolderAccess":+ case "removeDestinationFolderAccess":+ removeExistingDestinationAccess()+ result(nil)+ case "persistDestinationFolderAccess":+ let folderPath = call.arguments as! Stringdo {- let folderPath = call.arguments as! String- let folderURL = URL(fileURLWithPath: folderPath)-- let bookmarkData = try folderURL.bookmarkData(- options: [.withSecurityScope],- includingResourceValuesForKeys: nil,- relativeTo: nil- )-- let _ = SecurityScopedResourceManager.shared.startAccessing(bookmark: bookmarkData)+ try saveDestinationFolderAccess(folderPath)result(nil)} catch {result(FlutterError(code: "REQUEST_FOLDER_ACCESS_FAILED", message: "An error occurred while requesting folder access", details: nil))@@ -214,6 +211,37 @@ class AppDelegate: FlutterAppDelegate {}}+ private func saveDestinationFolderAccess(_ folderPath: String) throws {+ let folderURL = URL(fileURLWithPath: folderPath)+ let bookmarkData = try folderURL.bookmarkData(+ options: [.withSecurityScope],+ includingResourceValuesForKeys: nil,+ relativeTo: nil+ )+ Defaults[.destinationFolderBookmark] = bookmarkData+ }++ private func removeExistingDestinationAccess() {+ guard let existingBookmarkData = Defaults[.destinationFolderBookmark] else { return }+ if let url = SecurityScopedResourceManager.shared.startAccessing(bookmark: existingBookmarkData) {+ SecurityScopedResourceManager.shared.stopAccessing(url: url)+ Defaults[.destinationFolderBookmark] = nil+ }+ }++ private func restoreDestinationFolderAccess() {+ guard let bookmarkData = Defaults[.destinationFolderBookmark] else { return }+ do {+ var isStale = false+ let url = try URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, bookmarkDataIsStale: &isStale)+ if !isStale {+ let _ = url.startAccessingSecurityScopedResource()+ }+ } catch {+ print("Failed to restore folder access: \(error)")+ }+ }+override func application(_ sender: NSApplication, openFile filename: String) -> Bool {/**Although file URLs shared via the dock icon or the "open with" file menu item already contain access permission, we pass this through the bookmark mechanism for uniformity and readability of the code with URLs shared from the share extension.diff --git a/app/macos/Runner/Release.entitlements b/app/macos/Runner/Release.entitlementsindex 57f16075..25301d20 100644--- a/app/macos/Runner/Release.entitlements+++ b/app/macos/Runner/Release.entitlements@@ -16,5 +16,7 @@<true/><key>com.apple.security.network.server</key><true/>+ <key>com.apple.security.files.bookmarks.app-scope</key>+ <true/></dict></plist>diff --git a/app/macos/Runner/Shared.swift b/app/macos/Runner/Shared.swiftindex bcf483ce..af2e0014 100644--- a/app/macos/Runner/Shared.swift+++ b/app/macos/Runner/Shared.swift@@ -8,6 +8,7 @@ typealias FileBookmarkData = Dataextension Defaults.Keys {static let pendingFiles = Key<[FileBookmarkData]>("pendingFiles", default: [], suite: sharedDefaults)static let pendingStrings = Key<[String]>("pendingStrings", default: [], suite: sharedDefaults)+ static let destinationFolderBookmark = Key<FileBookmarkData?>("destinationFolderBookmark", default: nil, suite: .standard)}/**