Add URLSession to FileProviderExtension
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Wed, 4 Jan 2023 20:09:48 +0000 (21:09 +0100)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Fri, 12 May 2023 05:21:09 +0000 (13:21 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension.swift

index f31f02b97d31b78e47dd01a761bf30003ddd2735..15455e755da40e166e19d4e4409fc5330f251892 100644 (file)
 import FileProvider
 import OSLog
 import NCDesktopClientSocketKit
+import NextcloudKit
 
 class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension {
     let domain: NSFileProviderDomain
+
+    let appGroupIdentifier: String? = Bundle.main.object(forInfoDictionaryKey: "SocketApiPrefix") as? String
     var socketClient: LocalSocketClient = LocalSocketClient()
     var ncAccount: FileProviderDomainNextcloudAccountData  = FileProviderDomainNextcloudAccountData()
 
+    let urlSessionIdentifier: String = "com.nextcloud.session.upload.fileproviderext"
+    let urlSessionMaximumConnectionsPerHost = 5
+    lazy var urlSession: URLSession = {
+        let configuration = URLSessionConfiguration.background(withIdentifier: urlSessionIdentifier)
+        configuration.allowsCellularAccess = true
+        configuration.sessionSendsLaunchEvents = true
+        configuration.isDiscretionary = false
+        configuration.httpMaximumConnectionsPerHost = urlSessionMaximumConnectionsPerHost
+        configuration.requestCachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData
+        configuration.sharedContainerIdentifier = appGroupIdentifier
+        let session = URLSession(configuration: configuration, delegate: NKBackground.shared, delegateQueue: OperationQueue.main)
+        return session
+    }()
+
     required init(domain: NSFileProviderDomain) {
         self.domain = domain
         // The containing application must create a domain using `NSFileProviderManager.add(_:, completionHandler:)`. The system will then launch the application extension process, call `FileProviderExtension.init(domain:)` to instantiate the extension for that domain, and call methods on the instance.
@@ -79,10 +96,8 @@ class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension {
     // MARK: Nextcloud desktop client communication
 
     func startLocalSocketClient() {
-        let bundle = Bundle(for: type(of: self))
-        guard let fileProviderSocketApiPrefix = bundle.object(forInfoDictionaryKey: "SocketApiPrefix") as? String else {
+        guard let fileProviderSocketApiPrefix = appGroupIdentifier else {
             NSLog("Could not start file provider socket client properly as SocketApiPrefix is missing")
-
             return;
         }