Add URLSession to FileProviderExtension

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2023-01-04 21:09:48 +01:00
parent 4efb234f68
commit e319516b89
No known key found for this signature in database
GPG key ID: C839200C384636B0

View file

@ -15,12 +15,29 @@
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;
}