Skip to content

Commit

Permalink
File provider extension (fix & improvements) (#2978)
Browse files Browse the repository at this point in the history
* lint

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 10, 2024
1 parent 0e975a9 commit 1c8854a
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 381 deletions.
13 changes: 2 additions & 11 deletions File Provider Extension/FileProviderData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,9 @@ class fileProviderData: NSObject {
// MARK: -

func setupAccount(domain: NSFileProviderDomain?, providerExtension: NSFileProviderExtension) -> tableAccount? {

self.domain = domain
if domain != nil {
if let fileProviderManager = NSFileProviderManager(for: domain!) {
self.fileProviderManager = fileProviderManager
}
if let domain, let fileProviderManager = NSFileProviderManager(for: domain) {
self.fileProviderManager = fileProviderManager
}

// LOG
Expand All @@ -81,7 +78,6 @@ class fileProviderData: NSObject {

// NO DOMAIN -> Set default account
if domain == nil {

guard let activeAccount = NCManageDatabase.shared.getActiveAccount() else { return nil }

account = activeAccount.account
Expand All @@ -91,7 +87,6 @@ class fileProviderData: NSObject {
homeServerUrl = utilityFileSystem.getHomeServer(urlBase: activeAccount.urlBase, userId: activeAccount.userId)

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)

return tableAccount.init(value: activeAccount)
Expand Down Expand Up @@ -120,19 +115,15 @@ class fileProviderData: NSObject {
return tableAccount.init(value: activeAccount)
}
}

return nil
}

// MARK: -

@discardableResult
func signalEnumerator(ocId: String, delete: Bool = false, update: Bool = false) -> FileProviderItem? {

guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return nil }

guard let parentItemIdentifier = fileProviderUtility().getParentItemIdentifier(metadata: metadata) else { return nil }

let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)

if delete {
Expand Down
20 changes: 8 additions & 12 deletions File Provider Extension/FileProviderDomain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@
import UIKit

class FileProviderDomain: NSObject {

func registerDomains() {

NSFileProviderManager.getDomainsWithCompletionHandler { fileProviderDomain, error in

var domains: [String] = []
let pathRelativeToDocumentStorage = NSFileProviderManager.default.documentStorageURL.absoluteString
let accounts = NCManageDatabase.shared.getAllAccount()
Expand All @@ -50,10 +47,10 @@ class FileProviderDomain: NSObject {
}
}
if !domainFound {
let domainRawValue = NSFileProviderDomain(identifier: NSFileProviderDomainIdentifier(rawValue: domain), displayName: domain, pathRelativeToDocumentStorage: pathRelativeToDocumentStorage)
NSFileProviderManager.remove(domainRawValue, completionHandler: { error in
if error != nil {
print("Error domain: \(domainRawValue) error: \(String(describing: error))")
let fileProviderDomain = NSFileProviderDomain(identifier: NSFileProviderDomainIdentifier(rawValue: domain), displayName: domain, pathRelativeToDocumentStorage: pathRelativeToDocumentStorage)
NSFileProviderManager.remove(fileProviderDomain, completionHandler: { error in
if let error {
print("Error domain: \(fileProviderDomain) error: \(String(describing: error))")
}
})
}
Expand All @@ -72,10 +69,10 @@ class FileProviderDomain: NSObject {
}
}
if !domainFound {
let domainRawValue = NSFileProviderDomain(identifier: NSFileProviderDomainIdentifier(rawValue: accountDomain), displayName: accountDomain, pathRelativeToDocumentStorage: pathRelativeToDocumentStorage)
NSFileProviderManager.add(domainRawValue, completionHandler: { error in
if error != nil {
print("Error domain: \(domainRawValue) error: \(String(describing: error))")
let fileProviderDomain = NSFileProviderDomain(identifier: NSFileProviderDomainIdentifier(rawValue: accountDomain), displayName: accountDomain, pathRelativeToDocumentStorage: pathRelativeToDocumentStorage)
NSFileProviderManager.add(fileProviderDomain, completionHandler: { error in
if let error {
print("Error domain: \(fileProviderDomain) error: \(String(describing: error))")
}
})
}
Expand All @@ -84,7 +81,6 @@ class FileProviderDomain: NSObject {
}

func removeAllDomains() {

NSFileProviderManager.removeAllDomains { _ in }
}
}
51 changes: 16 additions & 35 deletions File Provider Extension/FileProviderEnumerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,16 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {

init(enumeratedItemIdentifier: NSFileProviderItemIdentifier) {
self.enumeratedItemIdentifier = enumeratedItemIdentifier

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

}
}

super.init()
}

Expand All @@ -54,49 +51,37 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {

func enumerateItems(for observer: NSFileProviderEnumerationObserver, startingAt page: NSFileProviderPage) {
var items: [NSFileProviderItemProtocol] = []

/*** WorkingSet ***/
/// WorkingSet
if enumeratedItemIdentifier == .workingSet {
var itemIdentifierMetadata: [NSFileProviderItemIdentifier: tableMetadata] = [:]

// ***** Tags *****
/// Tags
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 }
fpUtility.createocIdentifierOnFileSystem(metadata: metadata)
itemIdentifierMetadata[fpUtility.getItemIdentifier(metadata: metadata)] = metadata
}

// ***** Favorite *****
/// 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
}

// create items
/// Create items
for (_, metadata) in itemIdentifierMetadata {
let parentItemIdentifier = fpUtility.getParentItemIdentifier(metadata: metadata)
if parentItemIdentifier != nil {
let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier!)
if let parentItemIdentifier = fpUtility.getParentItemIdentifier(metadata: metadata) {
let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
items.append(item)
}
}

observer.didEnumerate(items)
observer.finishEnumerating(upTo: nil)

} else {

/*** ServerUrl ***/

/// ServerUrl
guard let serverUrl = serverUrl else {
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)
Expand All @@ -111,7 +96,6 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
func enumerateChanges(for observer: NSFileProviderChangeObserver, from anchor: NSFileProviderSyncAnchor) {
var itemsDelete: [NSFileProviderItemIdentifier] = []
var itemsUpdate: [FileProviderItem] = []

// Report the deleted items
//
if self.enumeratedItemIdentifier == .workingSet {
Expand All @@ -125,7 +109,6 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
}
fileProviderData.shared.fileProviderSignalDeleteContainerItemIdentifier.removeAll()
}

// Report the updated items
//
if self.enumeratedItemIdentifier == .workingSet {
Expand Down Expand Up @@ -160,13 +143,11 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
var numPage = numPage
var items: [NSFileProviderItemProtocol] = []

if metadatas != nil {
for metadata in metadatas! {
if let metadatas {
for metadata in metadatas {
if metadata.e2eEncrypted || (!metadata.session.isEmpty && metadata.session != NCNetworking.shared.sessionUploadBackgroundExtension) { continue }
fpUtility.createocIdentifierOnFileSystem(metadata: metadata)
let parentItemIdentifier = fpUtility.getParentItemIdentifier(metadata: metadata)
if parentItemIdentifier != nil {
let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier!)
if let parentItemIdentifier = fpUtility.getParentItemIdentifier(metadata: metadata) {
let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
items.append(item)
}
}
Expand All @@ -185,7 +166,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
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 {
DispatchQueue.global().async {
autoreleasepool {
NCManageDatabase.shared.convertFilesToMetadatas(files, useFirstAsMetadataFolder: true) { metadataFolder, metadatas in
/// FOLDER
NCManageDatabase.shared.addMetadata(metadataFolder)
Expand Down
Loading

0 comments on commit 1c8854a

Please sign in to comment.