Files
localsend--localsend/app/macos/Runner/SecurityScopedResourceManager.swift
wehub-resource-sync bb087aad19
CI / format (push) Failing after 2s
CI / packaging (push) Failing after 0s
CI / test (push) Failing after 3s
chore: import upstream snapshot with attribution
2026-07-13 11:58:56 +08:00

35 lines
1.3 KiB
Swift

import Foundation
class SecurityScopedResourceManager {
static let shared = SecurityScopedResourceManager()
private var openResources: [URL: Data] = [:]
private init() {}
func startAccessing(bookmark: Data) -> URL? {
do {
var isStale = false
let url = try URL(resolvingBookmarkData: bookmark, options: .withoutUI, relativeTo: nil, bookmarkDataIsStale: &isStale)
if url.startAccessingSecurityScopedResource() {
openResources[url] = bookmark
return url
} else {
print("Failed to start accessing security-scoped resource: \(url)")
return nil
}
} catch {
print("Failed to resolve security-scoped bookmark: \(error)")
return nil
}
}
func stopAccessing(url: URL) {
// TODO: stop accessing when file is not needed anymore! See: https://developer.apple.com/documentation/foundation/url/1779698-startaccessingsecurityscopedreso#:~:text=resource%20in%20question.-,Warning,-If%20you%20fail
if openResources.keys.contains(url) {
url.stopAccessingSecurityScopedResource()
openResources.removeValue(forKey: url)
}
}
}