mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-19 21:06:10 +00:00
Refactoring
This commit is contained in:
1 parent
39ea48fc80
commit
9536854fa2
8 files changed
+176
-208
No files matched your search
@@ -22,6 +22,7 @@ import UIKit
|
||||
class InboundNearbyConnection: NearbyConnection {
|
||||
|
||||
private var filesToBeReceived: [Int64: InternalFileInfo] = [:]
|
||||
private var downloadedFiles = [URL]()
|
||||
private var currentState: State = .initial
|
||||
private var cipherCommitment: Data?
|
||||
private var textPayloadID: Int64 = 0
|
||||
@@ -51,7 +52,17 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.connectionWasTerminated(connection: self, error: self.lastError)
|
||||
|
||||
SaveFilesManager.shared.movePendingFilesToTarget()
|
||||
if !self.downloadedFiles.isEmpty {
|
||||
|
||||
#if os(macOS)
|
||||
if Settings.shared.openFinderAfterReceiving {
|
||||
log("[SaveFilesManager] Opening \(self.downloadedFiles.count) file(s) in Finder.")
|
||||
NSWorkspace.shared.activateFileViewerSelecting(self.downloadedFiles)
|
||||
}
|
||||
#else
|
||||
FileManager.default.openDocumentFolder()
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,7 +179,7 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
#if os(macOS)
|
||||
fileInfo.progress?.unpublish()
|
||||
#endif
|
||||
SaveFilesManager.shared.registerFileFinishedDownloading(fileInfo.destinationURL)
|
||||
downloadedFiles.append(fileInfo.destinationURL)
|
||||
EXIFUtils.applyEXIFTimestamps(at: fileInfo.destinationURL)
|
||||
|
||||
filesToBeReceived.removeValue(forKey: id)
|
||||
@@ -222,7 +233,7 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
fileInfo.progress?.unpublish()
|
||||
#endif
|
||||
filesToBeReceived.removeValue(forKey: id)
|
||||
SaveFilesManager.shared.registerFileFinishedDownloading(fileInfo.destinationURL)
|
||||
downloadedFiles.append(fileInfo.destinationURL)
|
||||
EXIFUtils.applyEXIFTimestamps(at: fileInfo.destinationURL)
|
||||
|
||||
if filesToBeReceived.isEmpty {
|
||||
@@ -496,7 +507,7 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
currentState = .waitingForUserConsent
|
||||
|
||||
if frame.v1.introduction.fileMetadata.count > 0 && frame.v1.introduction.textMetadata.isEmpty {
|
||||
let saveDirectory = SaveFilesManager.shared.getSaveDirectory()
|
||||
let saveDirectory = NearbyConnectionManager.shared.getSaveDirectory()
|
||||
var usedDestinations = Set<URL>()
|
||||
|
||||
for file in frame.v1.introduction.fileMetadata {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user