2023-03-13 15:28:08 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2023 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
|
2024-01-22 09:40:47 +03:00
|
|
|
import Foundation
|
2023-03-14 23:07:16 +03:00
|
|
|
import OSLog
|
2023-03-13 15:28:08 +03:00
|
|
|
|
2024-01-22 09:40:47 +03:00
|
|
|
class FileProviderMaterialisedEnumerationObserver: NSObject, NSFileProviderEnumerationObserver {
|
2023-03-13 15:28:08 +03:00
|
|
|
let ncKitAccount: String
|
2023-03-13 15:39:24 +03:00
|
|
|
let completionHandler: (_ deletedOcIds: Set<String>) -> Void
|
2024-01-22 09:40:47 +03:00
|
|
|
var allEnumeratedItemIds: Set<String> = .init()
|
2023-03-13 15:28:08 +03:00
|
|
|
|
2024-01-22 09:40:47 +03:00
|
|
|
required init(
|
|
|
|
ncKitAccount: String, completionHandler: @escaping (_ deletedOcIds: Set<String>) -> Void
|
|
|
|
) {
|
2023-03-13 15:28:08 +03:00
|
|
|
self.ncKitAccount = ncKitAccount
|
2023-03-13 15:39:24 +03:00
|
|
|
self.completionHandler = completionHandler
|
2023-03-13 15:28:08 +03:00
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func didEnumerate(_ updatedItems: [NSFileProviderItemProtocol]) {
|
2024-01-22 09:40:47 +03:00
|
|
|
let updatedItemsIds = Array(updatedItems.map(\.itemIdentifier.rawValue))
|
2023-03-13 15:28:08 +03:00
|
|
|
|
|
|
|
for updatedItemsId in updatedItemsIds {
|
|
|
|
allEnumeratedItemIds.insert(updatedItemsId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 09:40:47 +03:00
|
|
|
func finishEnumerating(upTo _: NSFileProviderPage?) {
|
2023-03-14 23:07:16 +03:00
|
|
|
Logger.materialisedFileHandling.debug("Handling enumerated materialised items.")
|
2024-01-22 09:40:47 +03:00
|
|
|
FileProviderMaterialisedEnumerationObserver.handleEnumeratedItems(
|
|
|
|
allEnumeratedItemIds,
|
|
|
|
account: ncKitAccount,
|
|
|
|
completionHandler: completionHandler)
|
2023-03-13 15:28:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func finishEnumeratingWithError(_ error: Error) {
|
2024-01-22 09:40:47 +03:00
|
|
|
Logger.materialisedFileHandling.error(
|
|
|
|
"Ran into error when enumerating materialised items: \(error.localizedDescription, privacy: .public). Handling items enumerated so far"
|
|
|
|
)
|
|
|
|
FileProviderMaterialisedEnumerationObserver.handleEnumeratedItems(
|
|
|
|
allEnumeratedItemIds,
|
|
|
|
account: ncKitAccount,
|
|
|
|
completionHandler: completionHandler)
|
2023-03-13 15:28:08 +03:00
|
|
|
}
|
|
|
|
|
2024-01-22 09:40:47 +03:00
|
|
|
static func handleEnumeratedItems(
|
|
|
|
_ itemIds: Set<String>, account: String,
|
|
|
|
completionHandler: @escaping (_ deletedOcIds: Set<String>) -> Void
|
|
|
|
) {
|
2023-03-13 15:28:08 +03:00
|
|
|
let dbManager = NextcloudFilesDatabaseManager.shared
|
|
|
|
let databaseLocalFileMetadatas = dbManager.localFileMetadatas(account: account)
|
|
|
|
var noLongerMaterialisedIds = Set<String>()
|
|
|
|
|
|
|
|
DispatchQueue.global(qos: .background).async {
|
|
|
|
for localFile in databaseLocalFileMetadatas {
|
|
|
|
let localFileOcId = localFile.ocId
|
|
|
|
|
|
|
|
guard itemIds.contains(localFileOcId) else {
|
|
|
|
noLongerMaterialisedIds.insert(localFileOcId)
|
2024-01-22 09:40:47 +03:00
|
|
|
continue
|
2023-03-13 15:28:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
2024-01-22 09:40:47 +03:00
|
|
|
Logger.materialisedFileHandling.info(
|
|
|
|
"Cleaning up local file metadatas for unmaterialised items")
|
2023-03-13 15:28:08 +03:00
|
|
|
for itemId in noLongerMaterialisedIds {
|
|
|
|
dbManager.deleteLocalFileMetadata(ocId: itemId)
|
|
|
|
}
|
2023-03-13 15:39:24 +03:00
|
|
|
|
|
|
|
completionHandler(noLongerMaterialisedIds)
|
2023-03-13 15:28:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|