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
|
2023-01-26 22:50:40 +03:00
|
|
|
import NextcloudKit
|
2022-03-29 16:00:59 +03:00
|
|
|
|
|
|
|
class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
|
|
|
|
|
|
|
|
private let enumeratedItemIdentifier: NSFileProviderItemIdentifier
|
|
|
|
private let anchor = NSFileProviderSyncAnchor("an anchor".data(using: .utf8)!)
|
2023-01-26 22:50:40 +03:00
|
|
|
private static let maxItemsPerFileProviderPage = 100
|
|
|
|
var ncAccount: NextcloudAccount?
|
2023-01-27 01:01:31 +03:00
|
|
|
var serverUrl: String?
|
2022-03-29 16:00:59 +03:00
|
|
|
|
2023-01-10 23:21:28 +03:00
|
|
|
init(enumeratedItemIdentifier: NSFileProviderItemIdentifier, ncAccount: NextcloudAccount?) {
|
2022-03-29 16:00:59 +03:00
|
|
|
self.enumeratedItemIdentifier = enumeratedItemIdentifier
|
2023-01-26 22:50:40 +03:00
|
|
|
self.ncAccount = ncAccount
|
2023-01-10 22:25:26 +03:00
|
|
|
|
|
|
|
if enumeratedItemIdentifier == .rootContainer {
|
2023-01-10 23:21:28 +03:00
|
|
|
self.serverUrl = ncAccount?.davFilesUrl
|
2023-01-10 22:25:26 +03:00
|
|
|
} else {
|
|
|
|
let dbManager = NextcloudFilesDatabaseManager.shared
|
2023-01-12 23:49:28 +03:00
|
|
|
if let itemMetadata = dbManager.itemMetadataFromFileProviderItemIdentifier(enumeratedItemIdentifier),
|
|
|
|
let itemDirectoryMetadata = dbManager.parentDirectoryMetadataForItem(itemMetadata) {
|
2023-01-10 22:25:26 +03:00
|
|
|
|
2023-01-27 01:01:31 +03:00
|
|
|
self.serverUrl = itemDirectoryMetadata.serverUrl + "/" + itemMetadata.fileName
|
2023-01-10 22:25:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-29 16:00:59 +03:00
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func invalidate() {
|
|
|
|
// TODO: perform invalidation of server connection if necessary
|
|
|
|
}
|
|
|
|
|
2023-01-12 23:49:28 +03:00
|
|
|
// MARK: - Protocol methods
|
|
|
|
|
2022-03-29 16:00:59 +03:00
|
|
|
func enumerateItems(for observer: NSFileProviderEnumerationObserver, startingAt page: NSFileProviderPage) {
|
|
|
|
/* TODO:
|
|
|
|
- inspect the page to determine whether this is an initial or a follow-up request
|
|
|
|
|
|
|
|
If this is an enumerator for a directory, the root container or all directories:
|
|
|
|
- perform a server request to fetch directory contents
|
|
|
|
If this is an enumerator for the active set:
|
|
|
|
- perform a server request to update your local database
|
|
|
|
- fetch the active set from your local database
|
|
|
|
|
|
|
|
- inform the observer about the items returned by the server (possibly multiple times)
|
|
|
|
- inform the observer that you are finished with this page
|
|
|
|
*/
|
2023-01-26 22:50:40 +03:00
|
|
|
|
|
|
|
if enumeratedItemIdentifier == .workingSet {
|
|
|
|
// TODO
|
|
|
|
observer.finishEnumerating(upTo: nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let serverUrl = serverUrl, let ncAccount = ncAccount else { observer.finishEnumerating(upTo: nil); return }
|
|
|
|
|
|
|
|
if page == NSFileProviderPage.initialPageSortedByDate as NSFileProviderPage ||
|
|
|
|
page == NSFileProviderPage.initialPageSortedByName as NSFileProviderPage {
|
|
|
|
|
|
|
|
FileProviderEnumerator.readServerUrl(serverUrl, ncAccount: ncAccount) { metadatas in
|
|
|
|
FileProviderEnumerator.completeObserver(observer, numPage: 1, itemMetadatas: metadatas)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let numPage = Int(String(data: page.rawValue, encoding: .utf8)!)!
|
|
|
|
FileProviderEnumerator.completeObserver(observer, numPage: numPage, itemMetadatas: nil)
|
|
|
|
}
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func enumerateChanges(for observer: NSFileProviderChangeObserver, from anchor: NSFileProviderSyncAnchor) {
|
|
|
|
/* TODO:
|
|
|
|
- query the server for updates since the passed-in sync anchor
|
|
|
|
|
|
|
|
If this is an enumerator for the active set:
|
|
|
|
- note the changes in your local database
|
|
|
|
|
|
|
|
- inform the observer about item deletions and updates (modifications + insertions)
|
|
|
|
- inform the observer when you have finished enumerating up to a subsequent sync anchor
|
|
|
|
*/
|
|
|
|
observer.finishEnumeratingChanges(upTo: anchor, moreComing: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func currentSyncAnchor(completionHandler: @escaping (NSFileProviderSyncAnchor?) -> Void) {
|
|
|
|
completionHandler(anchor)
|
|
|
|
}
|
2023-01-12 23:50:35 +03:00
|
|
|
|
|
|
|
// MARK: - Helper methods
|
|
|
|
|
2023-01-26 22:50:40 +03:00
|
|
|
private static func completeObserver(_ observer: NSFileProviderEnumerationObserver, numPage: Int, itemMetadatas: [NextcloudItemMetadataTable]?) {
|
2023-01-12 23:50:35 +03:00
|
|
|
guard itemMetadatas != nil else { observer.finishEnumerating(upTo: nil); return }
|
2023-01-26 22:50:40 +03:00
|
|
|
|
2023-01-12 23:50:35 +03:00
|
|
|
var items: [NSFileProviderItem] = []
|
|
|
|
|
|
|
|
for itemMetadata in itemMetadatas! {
|
|
|
|
if itemMetadata.e2eEncrypted { continue }
|
|
|
|
|
|
|
|
createFileOrDirectoryLocally(metadata: itemMetadata)
|
|
|
|
|
|
|
|
if let parentItemIdentifier = parentItemIdentifierFromMetadata(itemMetadata) {
|
|
|
|
let item = FileProviderItem(metadata: itemMetadata, parentItemIdentifier: parentItemIdentifier)
|
|
|
|
items.append(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
observer.didEnumerate(items)
|
|
|
|
|
|
|
|
if items.count == maxItemsPerFileProviderPage {
|
|
|
|
let nextPage = numPage + 1
|
|
|
|
let providerPage = NSFileProviderPage("\(nextPage)".data(using: .utf8)!)
|
|
|
|
observer.finishEnumerating(upTo: providerPage)
|
|
|
|
} else {
|
|
|
|
observer.finishEnumerating(upTo: nil)
|
|
|
|
}
|
|
|
|
}
|
2023-01-26 22:50:40 +03:00
|
|
|
|
|
|
|
private static func finishReadServerUrl(_ serverUrlPath: String, ncKitAccount: String, completionHandler: @escaping (_ metadatas: [NextcloudItemMetadataTable]?) -> Void) {
|
|
|
|
let metadatas = NextcloudFilesDatabaseManager.shared.itemMetadatas(account: ncKitAccount, serverUrl: serverUrlPath)
|
|
|
|
completionHandler(metadatas)
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:01:31 +03:00
|
|
|
private static func readServerUrl(_ serverUrl: String, ncAccount: NextcloudAccount, completionHandler: @escaping (_ metadatas: [NextcloudItemMetadataTable]?) -> Void) {
|
2023-01-26 22:50:40 +03:00
|
|
|
let dbManager = NextcloudFilesDatabaseManager.shared
|
2023-01-27 01:01:31 +03:00
|
|
|
let ncKitAccount = ncAccount.ncKitAccount
|
2023-01-26 22:50:40 +03:00
|
|
|
var directoryEtag: String?
|
|
|
|
|
2023-01-27 01:01:31 +03:00
|
|
|
if let directoryMetadata = dbManager.directoryMetadata(account: ncKitAccount, serverUrl: serverUrl) {
|
2023-01-26 22:50:40 +03:00
|
|
|
directoryEtag = directoryMetadata.etag
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:01:31 +03:00
|
|
|
NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "0", showHiddenFiles: true) { account, files, _, error in
|
2023-01-26 22:50:40 +03:00
|
|
|
guard directoryEtag != files.first?.etag else {
|
2023-01-27 01:01:31 +03:00
|
|
|
finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, completionHandler: completionHandler)
|
2023-01-26 22:50:40 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:01:31 +03:00
|
|
|
NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: true) { account, files, _, error in
|
2023-01-26 22:50:40 +03:00
|
|
|
guard error == .success else {
|
2023-01-27 01:01:31 +03:00
|
|
|
finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, completionHandler: completionHandler)
|
2023-01-26 22:50:40 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.global().async {
|
|
|
|
dbManager.convertNKFilesToItemMetadatas(files, account: ncKitAccount) { _, childDirectoriesMetadata, metadatas in
|
2023-01-27 01:01:31 +03:00
|
|
|
dbManager.updateItemMetadatas(account: ncKitAccount, serverUrl: serverUrl, updatedMetadatas: metadatas)
|
|
|
|
dbManager.updateDirectoryMetadatasFromItemMetadatas(account: ncKitAccount, parentDirectoryServerUrl: serverUrl, updatedDirectoryItemMetadatas: childDirectoriesMetadata)
|
|
|
|
finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, completionHandler: completionHandler)
|
2023-01-26 22:50:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|