Refactoring

This commit is contained in:
Leon Böttger committed 2025-11-10 21:22:46 +01:00
1 parent 39ea48fc80
commit 9536854fa2
8 files changed
+176 -208

No files matched your search

@@ -39,6 +39,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
private var qrCodeNameEncryptionKey: SymmetricKey?
private let hasConnectionMonitor = NWPathMonitor()
private let connectionMonitorQueue = DispatchQueue(label: "NetworkConnectionMonitorQueue")
private var securityScopeUrl: URL?
public let deviceInfo: EndpointInfo
public let endpointID: [UInt8] = generateEndpointID()
@@ -92,6 +93,28 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
}
hasConnectionMonitor.start(queue: connectionMonitorQueue)
// remove old temp directory
let tempPath = FileManager.default.temporaryDirectory
let fileManager = FileManager.default
do {
let contents = try fileManager.contentsOfDirectory(at: tempPath, includingPropertiesForKeys: nil)
var didSomething = false
for item in contents {
didSomething = true
try fileManager.removeItem(at: item)
}
if didSomething {
log("[SaveFilesManager] Temporary directory cleared.")
}
} catch {
log("[SaveFilesManager] Failed to list contents of temp directory: \(error)")
}
}
@@ -208,6 +231,11 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
func connectionWasTerminated(connection: InboundNearbyConnection, error: Error?) {
incomingConnections.removeValue(forKey: connection.id)
// If there are no more incoming connections, we can stop accessing the save directory
if incomingConnections.isEmpty {
self.stopAccessingSaveDirectory()
}
if !connection.wasUserRejected {
inboundAppDelegates.forEach { delegate in
delegate.connectionWasTerminated(connectionID: connection.id, from: connection.remoteDeviceInfo ?? RemoteDeviceInfo(name: "UnknownDevice".localized(), type: .unknown), wasPlainTextTransfer: connection.isPlainTextTransfer, error: error)
@@ -595,6 +623,68 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
}
// -- MARK: - Inbound Connection Data Store
public func getSaveDirectory() -> URL {
// Not supported on iOS
#if os(macOS)
if let securityScopeUrl = securityScopeUrl {
log("[SaveFilesManager] Using existing security scope URL: \(securityScopeUrl)")
return securityScopeUrl
}
if let bookmarkData = Settings.shared.saveFolderBookmark {
var isStale = false
do {
let url = try URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
if !isStale {
if url.startAccessingSecurityScopedResource() {
log("[SaveFilesManager] Successfully accessed security scoped resource: \(url)")
securityScopeUrl = url
return url
}
} else {
log("[SaveFilesManager] Bookmark is stale, using default downloads folder.")
}
} catch {
log("[SaveFilesManager] Failed to resolve bookmark: \(error), using default downloads folder.")
}
}
do {
return try FileManager.default.url(for: .downloadsDirectory, in: .userDomainMask, appropriateFor: nil, create: true).resolvingSymlinksInPath()
} catch {
fatalError("[SaveFilesManager] Failed to get downloads directory: \(error)")
}
#else
// Return the documents directory for iOS
do {
return try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true).resolvingSymlinksInPath()
} catch {
fatalError("Failed to get documents directory: \(error)")
}
#endif
}
private func stopAccessingSaveDirectory() {
// Clean up security scoped resource access
guard let url = securityScopeUrl else {
return
}
log("[SaveFilesManager] Stopping access to security scoped resource: \(url)")
url.stopAccessingSecurityScopedResource()
securityScopeUrl = nil
}
// -- MARK: - Internal Data Model
private struct FoundServiceInfo {