Skip to content

Commit 9d42790

Browse files
add
Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
1 parent 1ee9ac1 commit 9d42790

3 files changed

Lines changed: 46 additions & 13 deletions

File tree

Sources/NextcloudKit/NKCommon.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public struct NKCommon: Sendable {
5858
public let identifierSessionUpload: String = "com.nextcloud.nextcloudkit.session.upload"
5959
// Background
6060
public let identifierSessionDownloadBackground: String = "com.nextcloud.session.downloadbackground"
61+
public let identifierSessionDownloadBackgroundExt: String = "com.nextcloud.session.downloadextension"
62+
6163
public let identifierSessionUploadBackground: String = "com.nextcloud.session.uploadbackground"
6264
public let identifierSessionUploadBackgroundWWan: String = "com.nextcloud.session.uploadbackgroundWWan"
6365
public let identifierSessionUploadBackgroundExt: String = "com.nextcloud.session.uploadextension"

Sources/NextcloudKit/NKSession.swift

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public struct NKSession: Sendable {
2020
public let sessionData: Alamofire.Session
2121
public let sessionDataNoCache: Alamofire.Session
2222
public let sessionDownloadBackground: URLSession
23+
public let sessionDownloadBackgroundExt: URLSession
2324
public let sessionUploadBackground: URLSession
2425
public let sessionUploadBackgroundWWan: URLSession
2526
public let sessionUploadBackgroundExt: URLSession
@@ -47,10 +48,10 @@ public struct NKSession: Sendable {
4748
self.httpMaximumConnectionsPerHostInUpload = httpMaximumConnectionsPerHostInUpload
4849

4950
let backgroundSessionDelegate = NKBackground(nkCommonInstance: nkCommonInstance)
50-
/// Strange but works ?!?!
51+
// Strange but works ?!?!
5152
let sharedCookieStorage = user + "@" + urlBase
5253

53-
/// SessionData Alamofire
54+
// SessionData Alamofire
5455
let configurationSessionData = URLSessionConfiguration.af.default
5556
configurationSessionData.requestCachePolicy = .useProtocolCachePolicy
5657
configurationSessionData.httpMaximumConnectionsPerHost = httpMaximumConnectionsPerHost
@@ -67,7 +68,7 @@ public struct NKSession: Sendable {
6768
serializationQueue: nkCommonInstance.serializationQueue,
6869
eventMonitors: [NKMonitor(nkCommonInstance: nkCommonInstance)])
6970

70-
/// SessionDataNoCache Alamofire
71+
// SessionDataNoCache Alamofire
7172
let configurationSessionDataNoCache = URLSessionConfiguration.af.default
7273
configurationSessionDataNoCache.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData
7374
configurationSessionDataNoCache.httpMaximumConnectionsPerHost = httpMaximumConnectionsPerHost
@@ -80,7 +81,7 @@ public struct NKSession: Sendable {
8081
serializationQueue: nkCommonInstance.serializationQueue,
8182
eventMonitors: [NKMonitor(nkCommonInstance: nkCommonInstance)])
8283

83-
/// Session Download Background
84+
// Session Download Background
8485
let configurationDownloadBackground = URLSessionConfiguration.background(withIdentifier: NKCommon().getSessionConfigurationIdentifier(NKCommon().identifierSessionDownloadBackground, account: account))
8586
configurationDownloadBackground.allowsCellularAccess = true
8687

@@ -99,7 +100,27 @@ public struct NKSession: Sendable {
99100
configurationDownloadBackground.httpCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: sharedCookieStorage)
100101
sessionDownloadBackground = URLSession(configuration: configurationDownloadBackground, delegate: backgroundSessionDelegate, delegateQueue: OperationQueue.main)
101102

102-
/// Session Upload Background
103+
// Session Download Background Extension
104+
let configurationDownloadBackgroundExt = URLSessionConfiguration.background(withIdentifier: NKCommon().identifierSessionDownloadBackgroundExt + UUID().uuidString)
105+
configurationDownloadBackgroundExt.allowsCellularAccess = true
106+
107+
if #available(macOS 11, *) {
108+
configurationDownloadBackgroundExt.sessionSendsLaunchEvents = true
109+
}
110+
111+
configurationDownloadBackgroundExt.isDiscretionary = false
112+
configurationDownloadBackgroundExt.httpMaximumConnectionsPerHost = self.httpMaximumConnectionsPerHostInDownload
113+
configurationDownloadBackgroundExt.requestCachePolicy = .useProtocolCachePolicy
114+
configurationDownloadBackgroundExt.sharedContainerIdentifier = groupIdentifier
115+
116+
#if os(iOS) || targetEnvironment(macCatalyst)
117+
configurationDownloadBackgroundExt.multipathServiceType = .handover
118+
#endif
119+
120+
configurationDownloadBackgroundExt.httpCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: sharedCookieStorage)
121+
sessionDownloadBackgroundExt = URLSession(configuration: configurationDownloadBackgroundExt, delegate: backgroundSessionDelegate, delegateQueue: OperationQueue.main)
122+
123+
// Session Upload Background
103124
let configurationUploadBackground = URLSessionConfiguration.background(withIdentifier: NKCommon().getSessionConfigurationIdentifier(NKCommon().identifierSessionUploadBackground, account: account))
104125
configurationUploadBackground.allowsCellularAccess = true
105126

@@ -118,7 +139,7 @@ public struct NKSession: Sendable {
118139
configurationUploadBackground.httpCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: sharedCookieStorage)
119140
sessionUploadBackground = URLSession(configuration: configurationUploadBackground, delegate: backgroundSessionDelegate, delegateQueue: OperationQueue.main)
120141

121-
/// Session Upload Background WWan
142+
// Session Upload Background WWan
122143
let configurationUploadBackgroundWWan = URLSessionConfiguration.background(withIdentifier: NKCommon().getSessionConfigurationIdentifier(NKCommon().identifierSessionUploadBackgroundWWan, account: account))
123144
configurationUploadBackgroundWWan.allowsCellularAccess = false
124145

@@ -132,7 +153,7 @@ public struct NKSession: Sendable {
132153
configurationUploadBackgroundWWan.httpCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: sharedCookieStorage)
133154
sessionUploadBackgroundWWan = URLSession(configuration: configurationUploadBackgroundWWan, delegate: backgroundSessionDelegate, delegateQueue: OperationQueue.main)
134155

135-
/// Session Upload Background Extension
156+
// Session Upload Background Extension
136157
let configurationUploadBackgroundExt = URLSessionConfiguration.background(withIdentifier: NKCommon().identifierSessionUploadBackgroundExt + UUID().uuidString)
137158
configurationUploadBackgroundExt.allowsCellularAccess = true
138159

Sources/NextcloudKit/NextcloudKitBackground.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
2828
public func download(serverUrlFileName: Any,
2929
fileNameLocalPath: String,
3030
taskDescription: String? = nil,
31+
account: String,
3132
automaticResume: Bool = true,
32-
account: String) -> (URLSessionDownloadTask?, error: NKError) {
33+
sessionIdentifier: String) -> (URLSessionDownloadTask?, error: NKError) {
3334
var url: URL?
35+
var downloadSession: URLSession?
3436
let groupDefaults = UserDefaults(suiteName: NextcloudKit.shared.nkCommonInstance.groupIdentifier)
3537

3638
/// Check if error is in groupDefaults
@@ -68,11 +70,17 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
6870
request.setValue(nkSession.userAgent, forHTTPHeaderField: "User-Agent")
6971
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
7072

71-
let task = nkSession.sessionDownloadBackground.downloadTask(with: request)
72-
task.taskDescription = taskDescription
73+
if sessionIdentifier == nkCommonInstance.identifierSessionDownloadBackground {
74+
downloadSession = nkSession.sessionDownloadBackground
75+
} else if sessionIdentifier == nkCommonInstance.identifierSessionDownloadBackgroundExt {
76+
downloadSession = nkSession.sessionDownloadBackgroundExt
77+
}
78+
79+
let task = downloadSession?.downloadTask(with: request)
80+
task?.taskDescription = taskDescription
7381

7482
if automaticResume {
75-
task.resume()
83+
task?.resume()
7684
}
7785

7886
return (task, .success)
@@ -88,17 +96,19 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
8896
public func downloadAsync(serverUrlFileName: Any,
8997
fileNameLocalPath: String,
9098
taskDescription: String? = nil,
99+
account: String,
91100
automaticResume: Bool = true,
92-
account: String) async -> (
101+
sessionIdentifier: String) async -> (
93102
downloadTask: URLSessionDownloadTask?,
94103
error: NKError
95104
) {
96105
await withCheckedContinuation { continuation in
97106
let (task, error) = download(serverUrlFileName: serverUrlFileName,
98107
fileNameLocalPath: fileNameLocalPath,
99108
taskDescription: taskDescription,
109+
account: account,
100110
automaticResume: automaticResume,
101-
account: account)
111+
sessionIdentifier: sessionIdentifier)
102112
continuation.resume(returning: (downloadTask: task, error: error))
103113
}
104114
}

0 commit comments

Comments
 (0)