Branchwell

PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / SecurityScopedAccess.swift

Raw 3.6 KB on 23ffa4e102ea595ec83f2b18683b75332fc815c3
1import Foundation
2
3extension Notification.Name {
4 static let pulseCanvasUserDataChanged = Notification.Name("PulseCanvas.userDataChanged")
5 static let pulseCanvasCloudDataApplied = Notification.Name("PulseCanvas.cloudDataApplied")
6}
7
8/// Restores use an immutable storage generation. The pointer and restored settings
9/// are activated together before any playback or library models are created.
10enum UserDataStorage {
11 // Local UI builds can use a private Info.plist key to keep QA out of real files.
12 // The shipping plists never contain this key, and Release ignores it.
13 private static var testRoot: URL? {
14 #if DEBUG
15 if let path = Bundle.main.object(forInfoDictionaryKey: "PulseCanvasUserDataTestRoot") as? String {
16 return URL(fileURLWithPath: path, isDirectory: true)
17 }
18 #endif
19 return nil
20 }
21 static var defaultSupport: URL {
22 if let testRoot { return testRoot.appendingPathComponent("Support", isDirectory: true) }
23 return FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
24 .appendingPathComponent("PulseCanvas", isDirectory: true)
25 }
26 static var backupRoot: URL { defaultSupport.deletingLastPathComponent().appendingPathComponent("PulseCanvasBackups", isDirectory: true) }
27 static var defaultDocuments: URL {
28 if let testRoot { return testRoot.appendingPathComponent("Documents", isDirectory: true) }
29 #if os(macOS)
30 return defaultSupport.appendingPathComponent("Documents", isDirectory: true)
31 #else
32 return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
33 #endif
34 }
35 static func root(_ name: String) -> URL {
36 if let value = UserDefaults.standard.string(forKey: "PulseCanvas.backup.active.v1"), let id = UUID(uuidString: value) {
37 return backupRoot.appendingPathComponent("Restores/\(id.uuidString)/\(name)", isDirectory: true)
38 }
39 return name == "Documents" ? defaultDocuments
40 : defaultSupport
41 }
42 static var documents: URL { root("Documents") }
43 static var support: URL { root("Support") }
44}
45
46/// Retain a balanced sandbox lease for the complete lifetime of an audio read
47/// or asynchronous export. Non-sandboxed development URLs need no lease.
48final class SecurityScopedAccess {
49 let url: URL
50 private let didStart: Bool
51
52 init(url: URL) {
53 self.url = url
54 didStart = url.startAccessingSecurityScopedResource()
55 }
56
57 deinit {
58 if didStart { url.stopAccessingSecurityScopedResource() }
59 }
60
61 static func resolve(bookmark: Data?, fallback: URL) -> URL {
62 guard let bookmark else { return fallback }
63 var stale = false
64 // A stale bookmark can still resolve. The audio controller refreshes
65 // the bookmark while the resolved resource is accessible.
66 #if os(macOS)
67 let options: URL.BookmarkResolutionOptions = [.withSecurityScope, .withoutUI]
68 #else
69 let options: URL.BookmarkResolutionOptions = [.withoutUI]
70 #endif
71 return (try? URL(resolvingBookmarkData: bookmark, options: options,
72 relativeTo: nil, bookmarkDataIsStale: &stale)) ?? fallback
73 }
74
75 func readOnlyBookmark() throws -> Data {
76 #if os(macOS)
77 let options: URL.BookmarkCreationOptions = [.withSecurityScope, .securityScopeAllowOnlyReadAccess]
78 #else
79 let options: URL.BookmarkCreationOptions = []
80 #endif
81 return try url.bookmarkData(options: options,
82 includingResourceValuesForKeys: nil, relativeTo: nil)
83 }
84}