2022-03-29 16:00:59 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import FileProvider
|
2022-12-27 02:18:00 +03:00
|
|
|
import OSLog
|
|
|
|
import NCDesktopClientSocketKit
|
2023-01-04 23:09:48 +03:00
|
|
|
import NextcloudKit
|
2022-03-29 16:00:59 +03:00
|
|
|
|
2023-01-28 19:07:13 +03:00
|
|
|
class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension, NKCommonDelegate {
|
2022-05-13 14:50:17 +03:00
|
|
|
let domain: NSFileProviderDomain
|
2023-02-02 22:13:35 +03:00
|
|
|
let ncKit = NextcloudKit()
|
|
|
|
lazy var ncKitBackground: NKBackground = {
|
|
|
|
let nckb = NKBackground(nkCommonInstance: ncKit.nkCommonInstance)
|
|
|
|
return nckb
|
|
|
|
}()
|
2023-01-04 23:09:48 +03:00
|
|
|
|
|
|
|
let appGroupIdentifier: String? = Bundle.main.object(forInfoDictionaryKey: "SocketApiPrefix") as? String
|
2023-01-04 23:40:30 +03:00
|
|
|
var ncAccount: NextcloudAccount?
|
2023-01-04 23:18:21 +03:00
|
|
|
lazy var socketClient: LocalSocketClient? = {
|
2023-01-12 18:38:57 +03:00
|
|
|
guard let containerUrl = pathForAppGroupContainer() else {
|
|
|
|
NSLog("Could not start file provider socket client properly as could not get container url")
|
2023-01-04 23:18:21 +03:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2023-01-12 18:38:57 +03:00
|
|
|
let socketPath = containerUrl.appendingPathComponent(".fileprovidersocket", conformingTo: .archive)
|
2023-01-04 23:18:21 +03:00
|
|
|
let lineProcessor = FileProviderSocketLineProcessor(delegate: self)
|
|
|
|
|
2023-01-12 18:38:57 +03:00
|
|
|
return LocalSocketClient(socketPath: socketPath.path, lineProcessor: lineProcessor)
|
2023-01-04 23:18:21 +03:00
|
|
|
}()
|
2022-12-29 02:21:33 +03:00
|
|
|
|
2023-01-04 23:09:48 +03:00
|
|
|
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
|
2023-02-02 22:13:35 +03:00
|
|
|
|
|
|
|
let session = URLSession(configuration: configuration, delegate: ncKitBackground, delegateQueue: OperationQueue.main)
|
2023-01-04 23:09:48 +03:00
|
|
|
return session
|
|
|
|
}()
|
2023-02-16 22:38:41 +03:00
|
|
|
var outstandingSessionTasks: [String: URLSessionTask] = [:]
|
|
|
|
var outstandingOcIdTemp: [String: String] = [:]
|
2023-01-04 23:09:48 +03:00
|
|
|
|
2023-01-27 04:26:27 +03:00
|
|
|
private var itemIdsForEnumeratorsNeedingSignalling: NSMutableSet = NSMutableSet()
|
|
|
|
|
2022-03-29 16:00:59 +03:00
|
|
|
required init(domain: NSFileProviderDomain) {
|
2022-05-13 14:50:17 +03:00
|
|
|
self.domain = domain
|
2022-05-11 19:52:14 +03:00
|
|
|
// 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.
|
2022-12-27 02:18:00 +03:00
|
|
|
|
2022-03-29 16:00:59 +03:00
|
|
|
super.init()
|
2023-01-04 23:18:21 +03:00
|
|
|
self.socketClient?.start()
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func invalidate() {
|
|
|
|
// TODO: cleanup any resources
|
|
|
|
}
|
2022-12-29 02:21:33 +03:00
|
|
|
|
|
|
|
// MARK: NSFileProviderReplicatedExtension protocol methods
|
2022-03-29 16:00:59 +03:00
|
|
|
|
|
|
|
func item(for identifier: NSFileProviderItemIdentifier, request: NSFileProviderRequest, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) -> Progress {
|
|
|
|
// resolve the given identifier to a record in the model
|
2023-01-27 03:54:32 +03:00
|
|
|
|
|
|
|
NSLog("Received item request for item with identifier: %@", identifier.rawValue)
|
2023-01-26 22:51:23 +03:00
|
|
|
if identifier == .rootContainer {
|
2023-01-27 01:01:31 +03:00
|
|
|
guard let ncAccount = ncAccount else {
|
2023-01-28 19:07:13 +03:00
|
|
|
NSLog("Not providing item: %@ as account not set up yet", identifier.rawValue)
|
2023-01-26 22:51:23 +03:00
|
|
|
completionHandler(nil, NSFileProviderError(.notAuthenticated))
|
|
|
|
return Progress()
|
|
|
|
}
|
2022-03-29 16:00:59 +03:00
|
|
|
|
2023-01-26 22:51:23 +03:00
|
|
|
let metadata = NextcloudItemMetadataTable()
|
|
|
|
|
2023-01-27 01:01:31 +03:00
|
|
|
metadata.account = ncAccount.ncKitAccount
|
2023-01-26 22:51:23 +03:00
|
|
|
metadata.directory = true
|
|
|
|
metadata.ocId = NSFileProviderItemIdentifier.rootContainer.rawValue
|
|
|
|
metadata.fileName = "root"
|
|
|
|
metadata.fileNameView = "root"
|
2023-01-27 01:01:31 +03:00
|
|
|
metadata.serverUrl = ncAccount.serverUrl
|
2023-01-26 22:51:23 +03:00
|
|
|
metadata.classFile = NKCommon.typeClassFile.directory.rawValue
|
|
|
|
|
2023-02-02 22:13:35 +03:00
|
|
|
completionHandler(FileProviderItem(metadata: metadata, parentItemIdentifier: NSFileProviderItemIdentifier.rootContainer, ncKit: ncKit), nil)
|
2023-01-26 22:51:23 +03:00
|
|
|
return Progress()
|
|
|
|
}
|
|
|
|
|
|
|
|
let dbManager = NextcloudFilesDatabaseManager.shared
|
|
|
|
guard let metadata = dbManager.itemMetadataFromFileProviderItemIdentifier(identifier),
|
|
|
|
let parentItemIdentifier = parentItemIdentifierFromMetadata(metadata) else {
|
|
|
|
completionHandler(nil, NSFileProviderError(.noSuchItem))
|
|
|
|
return Progress()
|
|
|
|
}
|
|
|
|
|
2023-02-02 22:13:35 +03:00
|
|
|
completionHandler(FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier, ncKit: ncKit), nil)
|
2022-03-29 16:00:59 +03:00
|
|
|
return Progress()
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchContents(for itemIdentifier: NSFileProviderItemIdentifier, version requestedVersion: NSFileProviderItemVersion?, request: NSFileProviderRequest, completionHandler: @escaping (URL?, NSFileProviderItem?, Error?) -> Void) -> Progress {
|
2023-02-16 22:38:41 +03:00
|
|
|
|
|
|
|
NSLog("Received request to fetch contents of item with identifier: %@", itemIdentifier.rawValue)
|
|
|
|
|
|
|
|
let dbManager = NextcloudFilesDatabaseManager.shared
|
|
|
|
let ocId = itemIdentifier.rawValue
|
|
|
|
guard let metadata = dbManager.itemMetadataFromOcId(ocId) else {
|
|
|
|
NSLog("Could not acquire metadata of item with identifier: %@", itemIdentifier.rawValue)
|
|
|
|
completionHandler(nil, nil, NSFileProviderError(.noSuchItem))
|
|
|
|
return Progress()
|
|
|
|
}
|
|
|
|
|
|
|
|
guard !metadata.isDocumentViewableOnly else {
|
|
|
|
NSLog("Could not get contents of item as is readonly: %@ %@", itemIdentifier.rawValue, metadata.fileName)
|
|
|
|
completionHandler(nil, nil, NSFileProviderError(.cannotSynchronize))
|
|
|
|
return Progress()
|
|
|
|
}
|
|
|
|
|
|
|
|
let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
|
|
|
|
|
|
|
|
NSLog("Fetching file with name %@ at URL: %@", metadata.fileName, serverUrlFileName)
|
|
|
|
|
|
|
|
do {
|
|
|
|
let fileNameLocalPath = try localPathForNCFile(ocId: metadata.ocId, fileNameView: metadata.fileNameView)
|
|
|
|
|
2023-02-20 15:42:28 +03:00
|
|
|
guard let updatedMetadata = dbManager.setStatusForItemMetadata(metadata, status: NextcloudItemMetadataTable.Status.downloading) else {
|
|
|
|
NSLog("Could not acquire updated metadata of item with identifier: %@", itemIdentifier.rawValue)
|
|
|
|
completionHandler(nil, nil, NSFileProviderError(.noSuchItem))
|
|
|
|
return Progress()
|
|
|
|
}
|
2023-02-16 22:38:41 +03:00
|
|
|
|
|
|
|
self.ncKit.download(serverUrlFileName: serverUrlFileName,
|
|
|
|
fileNameLocalPath: fileNameLocalPath.path,
|
|
|
|
requestHandler: { _ in
|
|
|
|
|
|
|
|
}, taskHandler: { task in
|
|
|
|
self.outstandingSessionTasks[serverUrlFileName] = task
|
|
|
|
NSFileProviderManager(for: self.domain)?.register(task, forItemWithIdentifier: itemIdentifier, completionHandler: { _ in })
|
|
|
|
}, progressHandler: { _ in
|
|
|
|
|
|
|
|
}) { _, etag, date, _, _, _, error in
|
|
|
|
self.outstandingSessionTasks.removeValue(forKey: serverUrlFileName)
|
|
|
|
|
|
|
|
if error == .success {
|
|
|
|
NSLog("Acquired contents of item with identifier: %@ and filename: %@", itemIdentifier.rawValue, updatedMetadata.fileName)
|
|
|
|
updatedMetadata.status = NextcloudItemMetadataTable.Status.normal.rawValue
|
|
|
|
updatedMetadata.date = (date ?? NSDate()) as Date
|
|
|
|
updatedMetadata.etag = etag ?? ""
|
|
|
|
|
|
|
|
dbManager.addLocalFileMetadataFromItemMetadata(updatedMetadata)
|
|
|
|
dbManager.addItemMetadata(updatedMetadata)
|
|
|
|
|
|
|
|
guard let parentItemIdentifier = parentItemIdentifierFromMetadata(updatedMetadata) else {
|
|
|
|
completionHandler(nil, nil, NSFileProviderError(.noSuchItem))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let fpItem = FileProviderItem(metadata: updatedMetadata, parentItemIdentifier: parentItemIdentifier, ncKit: self.ncKit)
|
|
|
|
|
|
|
|
completionHandler(fileNameLocalPath, fpItem, nil)
|
|
|
|
} else {
|
|
|
|
NSLog("Could not acquire contents of item with identifier: %@ and fileName: %@", itemIdentifier.rawValue, updatedMetadata.fileName)
|
|
|
|
|
|
|
|
updatedMetadata.status = NextcloudItemMetadataTable.Status.downloadError.rawValue
|
|
|
|
updatedMetadata.sessionError = error.errorDescription
|
|
|
|
|
|
|
|
dbManager.addItemMetadata(updatedMetadata)
|
|
|
|
|
|
|
|
completionHandler(nil, nil, NSFileProviderError(.cannotSynchronize))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch let error {
|
|
|
|
NSLog("Could not find local path for file %@, received error: %@", metadata.fileNameView, error.localizedDescription)
|
|
|
|
}
|
|
|
|
|
2022-03-29 16:00:59 +03:00
|
|
|
return Progress()
|
|
|
|
}
|
|
|
|
|
|
|
|
func createItem(basedOn itemTemplate: NSFileProviderItem, fields: NSFileProviderItemFields, contents url: URL?, options: NSFileProviderCreateItemOptions = [], request: NSFileProviderRequest, completionHandler: @escaping (NSFileProviderItem?, NSFileProviderItemFields, Bool, Error?) -> Void) -> Progress {
|
|
|
|
// TODO: a new item was created on disk, process the item's creation
|
2023-02-20 15:42:28 +03:00
|
|
|
|
2022-03-29 16:00:59 +03:00
|
|
|
completionHandler(itemTemplate, [], false, nil)
|
|
|
|
return Progress()
|
|
|
|
}
|
|
|
|
|
|
|
|
func modifyItem(_ item: NSFileProviderItem, baseVersion version: NSFileProviderItemVersion, changedFields: NSFileProviderItemFields, contents newContents: URL?, options: NSFileProviderModifyItemOptions = [], request: NSFileProviderRequest, completionHandler: @escaping (NSFileProviderItem?, NSFileProviderItemFields, Bool, Error?) -> Void) -> Progress {
|
|
|
|
// TODO: an item was modified on disk, process the item's modification
|
|
|
|
|
|
|
|
completionHandler(nil, [], false, NSError(domain: NSCocoaErrorDomain, code: NSFeatureUnsupportedError, userInfo:[:]))
|
|
|
|
return Progress()
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteItem(identifier: NSFileProviderItemIdentifier, baseVersion version: NSFileProviderItemVersion, options: NSFileProviderDeleteItemOptions = [], request: NSFileProviderRequest, completionHandler: @escaping (Error?) -> Void) -> Progress {
|
|
|
|
// TODO: an item was deleted on disk, process the item's deletion
|
|
|
|
|
|
|
|
completionHandler(NSError(domain: NSCocoaErrorDomain, code: NSFeatureUnsupportedError, userInfo:[:]))
|
|
|
|
return Progress()
|
|
|
|
}
|
|
|
|
|
|
|
|
func enumerator(for containerItemIdentifier: NSFileProviderItemIdentifier, request: NSFileProviderRequest) throws -> NSFileProviderEnumerator {
|
2023-01-27 04:26:27 +03:00
|
|
|
|
|
|
|
guard let ncAccount = ncAccount else {
|
|
|
|
NSLog("Not providing enumerator for container with identifier %@ yet as account not set up")
|
|
|
|
itemIdsForEnumeratorsNeedingSignalling.add(containerItemIdentifier)
|
|
|
|
throw NSFileProviderError(.notAuthenticated)
|
|
|
|
}
|
|
|
|
|
2023-02-02 22:13:35 +03:00
|
|
|
return FileProviderEnumerator(enumeratedItemIdentifier: containerItemIdentifier, ncAccount: ncAccount, ncKit: ncKit)
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
2022-12-29 02:21:33 +03:00
|
|
|
|
|
|
|
// MARK: Nextcloud desktop client communication
|
2022-12-30 21:52:51 +03:00
|
|
|
func sendFileProviderDomainIdentifier() {
|
2022-12-29 02:21:33 +03:00
|
|
|
let command = "FILE_PROVIDER_DOMAIN_IDENTIFIER_REQUEST_REPLY"
|
|
|
|
let argument = domain.identifier.rawValue
|
|
|
|
let message = command + ":" + argument + "\n"
|
2023-01-04 23:18:21 +03:00
|
|
|
socketClient?.sendMessage(message)
|
2022-12-29 02:21:33 +03:00
|
|
|
}
|
2022-12-30 21:52:51 +03:00
|
|
|
|
2023-01-30 22:32:35 +03:00
|
|
|
private func signalEnumeratorAfterAccountSetup() {
|
|
|
|
guard let fpManager = NSFileProviderManager(for: domain) else {
|
|
|
|
NSLog("Could not get file provider manager for domain %@, cannot notify after account setup", domain)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ncAccount != nil)
|
|
|
|
|
2023-02-01 20:20:44 +03:00
|
|
|
if !NextcloudFilesDatabaseManager.shared.anyItemMetadatasForAccount(ncAccount!.ncKitAccount) {
|
|
|
|
// This refreshes the entire structure of the FileProvider and calls
|
|
|
|
// enumerateItems rather than enumerateChanges in the enumerator
|
2023-01-30 22:32:35 +03:00
|
|
|
NSLog("Signalling manager for user %@ at server %@ to reimport everything", ncAccount!.username, ncAccount!.serverUrl)
|
|
|
|
fpManager.reimportItems(below: .rootContainer, completionHandler: {_ in })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
NSLog("Signalling enumerator for user %@ at server %@", ncAccount!.username, ncAccount!.serverUrl)
|
|
|
|
// System will only respond to workingSet when using and NSFileProviderReplicatedExtension
|
|
|
|
// https://developer.apple.com/documentation/fileprovider/nonreplicated_file_provider_extension/content_and_change_tracking/tracking_your_file_provider_s_changes/using_push_notifications_to_signal_changes
|
|
|
|
fpManager.signalEnumerator(for: .workingSet) { error in
|
|
|
|
if error != nil {
|
|
|
|
NSLog("Error signalling enumerator for workingSet, received error: %@", error!.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:01:31 +03:00
|
|
|
func setupDomainAccount(user: String, serverUrl: String, password: String) {
|
|
|
|
ncAccount = NextcloudAccount(user: user, serverUrl: serverUrl, password: password)
|
2023-02-02 22:13:35 +03:00
|
|
|
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
|
2023-01-28 19:07:13 +03:00
|
|
|
|
2023-01-27 04:26:27 +03:00
|
|
|
NSLog("Nextcloud account set up in File Provider extension for user: %@ at server: %@", user, serverUrl)
|
|
|
|
|
2023-01-30 22:32:35 +03:00
|
|
|
if itemIdsForEnumeratorsNeedingSignalling.count > 0 {
|
|
|
|
signalEnumeratorAfterAccountSetup()
|
|
|
|
itemIdsForEnumeratorsNeedingSignalling = NSMutableSet()
|
2023-01-27 04:26:27 +03:00
|
|
|
}
|
2022-12-30 21:52:51 +03:00
|
|
|
}
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|