Skip to content

Commit

Permalink
File provider extension (#2979)
Browse files Browse the repository at this point in the history
* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* fix

Signed-off-by: Marino Faggiana <[email protected]>

* cod

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

---------

Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana authored Jul 11, 2024
1 parent 8577a50 commit a1a06ea
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 275 deletions.
51 changes: 2 additions & 49 deletions File Provider Extension/FileProviderData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ class fileProviderData: NSObject {
var accountUrlBase = ""
var homeServerUrl = ""

// Max item for page
let itemForPage = 100

// Anchor
var currentAnchor: UInt64 = 0

// Rank favorite
var listFavoriteIdentifierRank: [String: NSNumber] = [:]

Expand Down Expand Up @@ -88,6 +82,7 @@ class fileProviderData: NSObject {

NCManageDatabase.shared.setCapabilities(account: account)
NextcloudKit.shared.setup(account: activeAccount.account, user: activeAccount.user, userId: activeAccount.userId, password: NCKeychain().getPassword(account: activeAccount.account), urlBase: activeAccount.urlBase, userAgent: userAgent, nextcloudVersion: NCGlobal.shared.capabilityServerVersionMajor, delegate: NCNetworking.shared)
NCNetworking.shared.delegate = providerExtension as? NCNetworkingDelegate

return tableAccount.init(value: activeAccount)
}
Expand All @@ -111,6 +106,7 @@ class fileProviderData: NSObject {
NCManageDatabase.shared.setCapabilities(account: account)

NextcloudKit.shared.setup(account: activeAccount.account, user: activeAccount.user, userId: activeAccount.userId, password: NCKeychain().getPassword(account: activeAccount.account), urlBase: activeAccount.urlBase, userAgent: userAgent, nextcloudVersion: NCGlobal.shared.capabilityServerVersionMajor, delegate: NCNetworking.shared)
NCNetworking.shared.delegate = providerExtension as? NCNetworkingDelegate

return tableAccount.init(value: activeAccount)
}
Expand All @@ -130,60 +126,17 @@ class fileProviderData: NSObject {
fileProviderData.shared.fileProviderSignalDeleteContainerItemIdentifier[item.itemIdentifier] = item.itemIdentifier
fileProviderData.shared.fileProviderSignalDeleteWorkingSetItemIdentifier[item.itemIdentifier] = item.itemIdentifier
}

if update {
fileProviderData.shared.fileProviderSignalUpdateContainerItem[item.itemIdentifier] = item
fileProviderData.shared.fileProviderSignalUpdateWorkingSetItem[item.itemIdentifier] = item
}

if !update && !delete {
fileProviderData.shared.fileProviderSignalUpdateWorkingSetItem[item.itemIdentifier] = item
}

if update || delete {
currentAnchor += 1
fileProviderManager.signalEnumerator(for: parentItemIdentifier) { _ in }
}

fileProviderManager.signalEnumerator(for: .workingSet) { _ in }

return item
}

/*
func updateFavoriteForWorkingSet() {

var updateWorkingSet = false
let oldListFavoriteIdentifierRank = listFavoriteIdentifierRank
listFavoriteIdentifierRank = NCManageDatabase.shared.getTableMetadatasDirectoryFavoriteIdentifierRank(account: account)

// (ADD)
for (identifier, _) in listFavoriteIdentifierRank {

guard let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "ocId == %@", identifier)) else { continue }
guard let parentItemIdentifier = fileProviderUtility.sharedInstance.getParentItemIdentifier(metadata: metadata, homeServerUrl: homeServerUrl) else { continue }
let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)

fileProviderSignalUpdateWorkingSetItem[item.itemIdentifier] = item
updateWorkingSet = true
}

// (REMOVE)
for (identifier, _) in oldListFavoriteIdentifierRank {

if !listFavoriteIdentifierRank.keys.contains(identifier) {

guard let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "ocId == %@", identifier)) else { continue }
let itemIdentifier = fileProviderUtility.sharedInstance.getItemIdentifier(metadata: metadata)

fileProviderSignalDeleteWorkingSetItemIdentifier[itemIdentifier] = itemIdentifier
updateWorkingSet = true
}
}

if updateWorkingSet {
signalEnumerator(for: [.workingSet])
}
}
*/
}
4 changes: 0 additions & 4 deletions File Provider Extension/FileProviderDomain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,4 @@ class FileProviderDomain: NSObject {
}
}
}

func removeAllDomains() {
NSFileProviderManager.removeAllDomains { _ in }
}
}
107 changes: 51 additions & 56 deletions File Provider Extension/FileProviderEnumerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@

import UIKit
import FileProvider
import RealmSwift
import NextcloudKit

class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
var enumeratedItemIdentifier: NSFileProviderItemIdentifier
var serverUrl: String?
let fpUtility = fileProviderUtility()
let providerUtility = fileProviderUtility()
var recordsPerPage: Int = 20
var anchor: UInt64 = 0

init(enumeratedItemIdentifier: NSFileProviderItemIdentifier) {
self.enumeratedItemIdentifier = enumeratedItemIdentifier
// Select ServerUrl
if enumeratedItemIdentifier == .rootContainer {
serverUrl = fileProviderData.shared.homeServerUrl
} else {
if let metadata = fpUtility.getTableMetadataFromItemIdentifier(enumeratedItemIdentifier),
if let metadata = providerUtility.getTableMetadataFromItemIdentifier(enumeratedItemIdentifier),
let directorySource = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, metadata.serverUrl)) {
serverUrl = directorySource.serverUrl + "/" + metadata.fileName

Expand All @@ -45,9 +47,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
super.init()
}

func invalidate() {

}
func invalidate() { }

func enumerateItems(for observer: NSFileProviderEnumerationObserver, startingAt page: NSFileProviderPage) {
var items: [NSFileProviderItemProtocol] = []
Expand All @@ -58,18 +58,17 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
let tags = NCManageDatabase.shared.getTags(predicate: NSPredicate(format: "account == %@", fileProviderData.shared.account))
for tag in tags {
guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(tag.ocId) else { continue }
itemIdentifierMetadata[fpUtility.getItemIdentifier(metadata: metadata)] = metadata
itemIdentifierMetadata[providerUtility.getItemIdentifier(metadata: metadata)] = metadata
}
/// Favorite
fileProviderData.shared.listFavoriteIdentifierRank = NCManageDatabase.shared.getTableMetadatasDirectoryFavoriteIdentifierRank(account: fileProviderData.shared.account)
for (identifier, _) in fileProviderData.shared.listFavoriteIdentifierRank {

guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(identifier) else { continue }
itemIdentifierMetadata[fpUtility.getItemIdentifier(metadata: metadata)] = metadata
itemIdentifierMetadata[providerUtility.getItemIdentifier(metadata: metadata)] = metadata
}
/// Create items
for (_, metadata) in itemIdentifierMetadata {
if let parentItemIdentifier = fpUtility.getParentItemIdentifier(metadata: metadata) {
if let parentItemIdentifier = providerUtility.getParentItemIdentifier(metadata: metadata) {
let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
items.append(item)
}
Expand All @@ -82,13 +81,35 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
observer.finishEnumerating(upTo: nil)
return
}
if page == NSFileProviderPage.initialPageSortedByDate as NSFileProviderPage || page == NSFileProviderPage.initialPageSortedByName as NSFileProviderPage {
self.readFileOrFolder(serverUrl: serverUrl) { metadatas in
self.completeObserver(observer, numPage: 1, metadatas: metadatas)
var pageNumber = 1
if let stringPage = String(data: page.rawValue, encoding: .utf8),

Check warning on line 85 in File Provider Extension/FileProviderEnumerator.swift

View workflow job for this annotation

GitHub Actions / Lint

Non-Optional String <-> Data Conversion Violation: Prefer using UTF-8 encoded strings when converting between `String` and `Data` (non_optional_string_data_conversion)
let intPage = Int(stringPage) {
pageNumber = intPage
}

self.fetchItemsForPage(serverUrl: serverUrl, pageNumber: pageNumber) { metadatas in
if let metadatas {
for metadata in metadatas {
if metadata.e2eEncrypted || (!metadata.session.isEmpty && metadata.session != NCNetworking.shared.sessionUploadBackgroundExtension) {
continue
}
if let parentItemIdentifier = self.providerUtility.getParentItemIdentifier(metadata: metadata) {
let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
items.append(item)
}
}
}

observer.didEnumerate(items)

if let metadatas,
metadatas.count == self.recordsPerPage {
pageNumber += 1
let providerPage = NSFileProviderPage("\(pageNumber)".data(using: .utf8)!)

Check warning on line 108 in File Provider Extension/FileProviderEnumerator.swift

View workflow job for this annotation

GitHub Actions / Lint

Non-Optional String <-> Data Conversion Violation: Prefer using UTF-8 encoded strings when converting between `String` and `Data` (non_optional_string_data_conversion)
observer.finishEnumerating(upTo: providerPage)
} else {
observer.finishEnumerating(upTo: nil)
}
} else {
let numPage = Int(String(data: page.rawValue, encoding: .utf8)!)!
completeObserver(observer, numPage: numPage, metadatas: nil)
}
}
}
Expand Down Expand Up @@ -126,62 +147,36 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
observer.didDeleteItems(withIdentifiers: itemsDelete)
observer.didUpdate(itemsUpdate)

let data = "\(fileProviderData.shared.currentAnchor)".data(using: .utf8)
let data = "\(self.anchor)".data(using: .utf8)

Check warning on line 150 in File Provider Extension/FileProviderEnumerator.swift

View workflow job for this annotation

GitHub Actions / Lint

Non-Optional String <-> Data Conversion Violation: Prefer using UTF-8 encoded strings when converting between `String` and `Data` (non_optional_string_data_conversion)
observer.finishEnumeratingChanges(upTo: NSFileProviderSyncAnchor(data!), moreComing: false)
}

func currentSyncAnchor(completionHandler: @escaping (NSFileProviderSyncAnchor?) -> Void) {
let data = "\(fileProviderData.shared.currentAnchor)".data(using: .utf8)
let data = "\(self.anchor)".data(using: .utf8)

Check warning on line 155 in File Provider Extension/FileProviderEnumerator.swift

View workflow job for this annotation

GitHub Actions / Lint

Non-Optional String <-> Data Conversion Violation: Prefer using UTF-8 encoded strings when converting between `String` and `Data` (non_optional_string_data_conversion)
completionHandler(NSFileProviderSyncAnchor(data!))
}

// --------------------------------------------------------------------------------------------
// MARK: - User Function + Network
// --------------------------------------------------------------------------------------------

func completeObserver(_ observer: NSFileProviderEnumerationObserver, numPage: Int, metadatas: [tableMetadata]?) {
var numPage = numPage
var items: [NSFileProviderItemProtocol] = []

if let metadatas {
for metadata in metadatas {
if metadata.e2eEncrypted || (!metadata.session.isEmpty && metadata.session != NCNetworking.shared.sessionUploadBackgroundExtension) { continue }
if let parentItemIdentifier = fpUtility.getParentItemIdentifier(metadata: metadata) {
let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
items.append(item)
}
}
observer.didEnumerate(items)
}
func fetchItemsForPage(serverUrl: String, pageNumber: Int, completionHandler: @escaping (_ metadatas: Results<tableMetadata>?) -> Void) {
let predicate = NSPredicate(format: "account == %@ AND serverUrl == %@", fileProviderData.shared.account, serverUrl)

if items.count == fileProviderData.shared.itemForPage {
numPage += 1
let providerPage = NSFileProviderPage("\(numPage)".data(using: .utf8)!)
observer.finishEnumerating(upTo: providerPage)
} else {
observer.finishEnumerating(upTo: nil)
}
}

func readFileOrFolder(serverUrl: String, completionHandler: @escaping (_ metadatas: [tableMetadata]?) -> Void) {
NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: NCKeychain().showHiddenFiles) { account, files, _, error in
if error == .success {
autoreleasepool {
if pageNumber == 1 {
NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: NCKeychain().showHiddenFiles) { account, files, _, error in
if error == .success {
NCManageDatabase.shared.convertFilesToMetadatas(files, useFirstAsMetadataFolder: true) { metadataFolder, metadatas in
/// FOLDER
NCManageDatabase.shared.addMetadata(metadataFolder)
NCManageDatabase.shared.addDirectory(e2eEncrypted: metadataFolder.e2eEncrypted, favorite: metadataFolder.favorite, ocId: metadataFolder.ocId, fileId: metadataFolder.fileId, etag: metadataFolder.etag, permissions: metadataFolder.permissions, richWorkspace: metadataFolder.richWorkspace, serverUrl: serverUrl, account: metadataFolder.account)
/// FILES
let predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND status == %d", account, serverUrl, NCGlobal.shared.metadataStatusNormal)
NCManageDatabase.shared.updateMetadatas(metadatas, predicate: predicate)
let metadatas = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", fileProviderData.shared.account, serverUrl), sorted: "fileName", ascending: true)
completionHandler(metadatas)
let updatePredicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND status == %d", account, serverUrl, NCGlobal.shared.metadataStatusNormal)
NCManageDatabase.shared.updateMetadatas(metadatas, predicate: updatePredicate)
}
}
} else {
let metadatas = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", fileProviderData.shared.account, serverUrl), sorted: "fileName", ascending: true)
completionHandler(metadatas)
let resultsMetadata = NCManageDatabase.shared.fetchPagedResults(ofType: tableMetadata.self, primaryKey: "ocId", recordsPerPage: self.recordsPerPage, pageNumber: pageNumber, filter: predicate, sortedByKeyPath: "fileName")
completionHandler(resultsMetadata)
}
} else {
let resultsMetadata = NCManageDatabase.shared.fetchPagedResults(ofType: tableMetadata.self, primaryKey: "ocId", recordsPerPage: recordsPerPage, pageNumber: pageNumber, filter: predicate, sortedByKeyPath: "fileName")
completionHandler(resultsMetadata)
}
}
}
Loading

0 comments on commit a1a06ea

Please sign in to comment.