Stop using NextcloudKit.shared and NKCommon.shared, use specific NextcloudKit instance instead to prevent clashing between different FileProvider domains

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2023-02-02 20:13:35 +01:00
parent f0524d44e1
commit 230b55d7e9
No known key found for this signature in database
GPG key ID: C839200C384636B0
3 changed files with 36 additions and 27 deletions

View file

@ -20,12 +20,14 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
private let enumeratedItemIdentifier: NSFileProviderItemIdentifier
private let anchor = NSFileProviderSyncAnchor("an anchor".data(using: .utf8)!)
private static let maxItemsPerFileProviderPage = 100
var ncAccount: NextcloudAccount
let ncAccount: NextcloudAccount
let ncKit: NextcloudKit
var serverUrl: String = ""
init(enumeratedItemIdentifier: NSFileProviderItemIdentifier, ncAccount: NextcloudAccount) {
init(enumeratedItemIdentifier: NSFileProviderItemIdentifier, ncAccount: NextcloudAccount, ncKit: NextcloudKit) {
self.enumeratedItemIdentifier = enumeratedItemIdentifier
self.ncAccount = ncAccount
self.ncKit = ncKit
if enumeratedItemIdentifier == .rootContainer {
NSLog("Providing enumerator for root container")
@ -76,8 +78,8 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
page == NSFileProviderPage.initialPageSortedByName as NSFileProviderPage {
NSLog("Enumerating initial page for user: %@ with serverUrl: %@", ncAccount.username, serverUrl)
FileProviderEnumerator.readServerUrl(serverUrl, ncAccount: ncAccount) { metadatas, readError in
FileProviderEnumerator.completeObserver(observer, numPage: 1, itemMetadatas: metadatas, error: readError)
FileProviderEnumerator.readServerUrl(serverUrl, ncAccount: ncAccount, ncKit: ncKit) { metadatas, readError in
FileProviderEnumerator.completeObserver(observer, ncKit: self.ncKit, numPage: 1, itemMetadatas: metadatas, error: readError)
}
return;
@ -85,7 +87,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
let numPage = Int(String(data: page.rawValue, encoding: .utf8)!)!
NSLog("Enumerating page %d for user: %@ with serverUrl: %@", numPage, ncAccount.username, serverUrl)
FileProviderEnumerator.completeObserver(observer, numPage: numPage, itemMetadatas: nil, error: nil)
FileProviderEnumerator.completeObserver(observer, ncKit: ncKit, numPage: numPage, itemMetadatas: nil, error: nil)
}
func enumerateChanges(for observer: NSFileProviderChangeObserver, from anchor: NSFileProviderSyncAnchor) {
@ -108,7 +110,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
// MARK: - Helper methods
private static func completeObserver(_ observer: NSFileProviderEnumerationObserver, numPage: Int, itemMetadatas: [NextcloudItemMetadataTable]?, error: Error?) {
private static func completeObserver(_ observer: NSFileProviderEnumerationObserver, ncKit: NextcloudKit, numPage: Int, itemMetadatas: [NextcloudItemMetadataTable]?, error: Error?) {
guard error == nil else {
NSLog("Finishing enumeration with error")
observer.finishEnumeratingWithError(error!)
@ -132,7 +134,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
createFileOrDirectoryLocally(metadata: itemMetadata)
if let parentItemIdentifier = parentItemIdentifierFromMetadata(itemMetadata) {
let item = FileProviderItem(metadata: itemMetadata, parentItemIdentifier: parentItemIdentifier)
let item = FileProviderItem(metadata: itemMetadata, parentItemIdentifier: parentItemIdentifier, ncKit: ncKit)
NSLog("Will enumerate item with ocId: %@ and name: %@", itemMetadata.ocId, itemMetadata.fileName)
items.append(item)
} else {
@ -159,7 +161,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
completionHandler(metadatas, readError)
}
private static func readServerUrl(_ serverUrl: String, ncAccount: NextcloudAccount, completionHandler: @escaping (_ metadatas: [NextcloudItemMetadataTable]?, _ readError: Error?) -> Void) {
private static func readServerUrl(_ serverUrl: String, ncAccount: NextcloudAccount, ncKit: NextcloudKit, completionHandler: @escaping (_ metadatas: [NextcloudItemMetadataTable]?, _ readError: Error?) -> Void) {
let dbManager = NextcloudFilesDatabaseManager.shared
let ncKitAccount = ncAccount.ncKitAccount
var directoryEtag: String?
@ -168,9 +170,9 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
directoryEtag = directoryMetadata.etag
}
NSLog("Starting to read serverUrl: %@ for user: %@ at depth 0", serverUrl, ncKitAccount)
NSLog("Starting to read serverUrl: %@ for user: %@ at depth 0. NCKit info: user: %@, userId: %@, password: %@, urlBase: %@, ncVersion: %d", serverUrl, ncKitAccount, ncKit.nkCommonInstance.user, ncKit.nkCommonInstance.userId, ncKit.nkCommonInstance.password, ncKit.nkCommonInstance.urlBase, ncKit.nkCommonInstance.nextcloudVersion)
NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "0", showHiddenFiles: true) { account, files, _, error in
ncKit.readFileOrFolder(serverUrlFileName: serverUrl, depth: "0", showHiddenFiles: true) { account, files, _, error in
guard error == .success else {
NSLog("0 depth readFileOrFolder of url: %@ did not complete successfully, received error: %@", serverUrl, error.errorDescription)
finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, readError: error.error, completionHandler: completionHandler)
@ -186,7 +188,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
NSLog("Starting to read serverUrl: %@ for user: %@ at depth 1", serverUrl, ncKitAccount)
NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: true) { account, files, _, error in
ncKit.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: true) { account, files, _, error in
guard error == .success else {
NSLog("1 depth readFileOrFolder of url: %@ did not complete successfully, received error: %@", serverUrl, error.errorDescription)
finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, readError: error.error, completionHandler: completionHandler)

View file

@ -19,6 +19,11 @@ import NextcloudKit
class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension, NKCommonDelegate {
let domain: NSFileProviderDomain
let ncKit = NextcloudKit()
lazy var ncKitBackground: NKBackground = {
let nckb = NKBackground(nkCommonInstance: ncKit.nkCommonInstance)
return nckb
}()
let appGroupIdentifier: String? = Bundle.main.object(forInfoDictionaryKey: "SocketApiPrefix") as? String
var ncAccount: NextcloudAccount?
@ -44,7 +49,8 @@ class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension, NKComm
configuration.httpMaximumConnectionsPerHost = urlSessionMaximumConnectionsPerHost
configuration.requestCachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData
configuration.sharedContainerIdentifier = appGroupIdentifier
let session = URLSession(configuration: configuration, delegate: NKBackground.shared, delegateQueue: OperationQueue.main)
let session = URLSession(configuration: configuration, delegate: ncKitBackground, delegateQueue: OperationQueue.main)
return session
}()
@ -85,7 +91,7 @@ class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension, NKComm
metadata.serverUrl = ncAccount.serverUrl
metadata.classFile = NKCommon.typeClassFile.directory.rawValue
completionHandler(FileProviderItem(metadata: metadata, parentItemIdentifier: NSFileProviderItemIdentifier.rootContainer), nil)
completionHandler(FileProviderItem(metadata: metadata, parentItemIdentifier: NSFileProviderItemIdentifier.rootContainer, ncKit: ncKit), nil)
return Progress()
}
@ -96,7 +102,7 @@ class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension, NKComm
return Progress()
}
completionHandler(FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier), nil)
completionHandler(FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier, ncKit: ncKit), nil)
return Progress()
}
@ -136,7 +142,7 @@ class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension, NKComm
throw NSFileProviderError(.notAuthenticated)
}
return FileProviderEnumerator(enumeratedItemIdentifier: containerItemIdentifier, ncAccount: ncAccount)
return FileProviderEnumerator(enumeratedItemIdentifier: containerItemIdentifier, ncAccount: ncAccount, ncKit: ncKit)
}
// MARK: Nextcloud desktop client communication
@ -175,14 +181,13 @@ class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension, NKComm
func setupDomainAccount(user: String, serverUrl: String, password: String) {
ncAccount = NextcloudAccount(user: user, serverUrl: serverUrl, password: password)
NextcloudKit.shared.setup(user: ncAccount!.username,
userId: ncAccount!.username,
password: ncAccount!.password,
urlBase: ncAccount!.serverUrl,
userAgent: "Nextcloud-macOS/FileProviderExt",
nextcloudVersion: 25,
delegate: nil) // TODO: add delegate methods for self
ncKit.setup(user: ncAccount!.username,
userId: ncAccount!.username,
password: ncAccount!.password,
urlBase: ncAccount!.serverUrl,
userAgent: "Nextcloud-macOS/FileProviderExt",
nextcloudVersion: 25,
delegate: nil) // TODO: add delegate methods for self
NSLog("Nextcloud account set up in File Provider extension for user: %@ at server: %@", user, serverUrl)

View file

@ -25,6 +25,7 @@ class FileProviderItem: NSObject, NSFileProviderItem {
let metadata: NextcloudItemMetadataTable
let parentItemIdentifier: NSFileProviderItemIdentifier
let ncKit: NextcloudKit
var itemIdentifier: NSFileProviderItemIdentifier {
return NSFileProviderItemIdentifier(metadata.ocId)
@ -62,9 +63,9 @@ class FileProviderItem: NSObject, NSFileProviderItem {
return .folder
}
let internalType = NKCommon.shared.getInternalType(fileName: metadata.fileNameView,
mimeType: "",
directory: metadata.directory)
let internalType = ncKit.nkCommonInstance.getInternalType(fileName: metadata.fileNameView,
mimeType: "",
directory: metadata.directory)
return UTType(filenameExtension: internalType.ext) ?? .content
}
@ -111,9 +112,10 @@ class FileProviderItem: NSObject, NSFileProviderItem {
}
}
init(metadata: NextcloudItemMetadataTable, parentItemIdentifier: NSFileProviderItemIdentifier) {
init(metadata: NextcloudItemMetadataTable, parentItemIdentifier: NSFileProviderItemIdentifier, ncKit: NextcloudKit) {
self.metadata = metadata
self.parentItemIdentifier = parentItemIdentifier
self.ncKit = ncKit
super.init()
}
}