Skip to content

Commit

Permalink
https://github.com/nextcloud/ios/issues/2937
Browse files Browse the repository at this point in the history
Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana committed Jun 10, 2024
1 parent e626763 commit 0fd93dc
Showing 1 changed file with 10 additions and 37 deletions.
47 changes: 10 additions & 37 deletions File Provider Extension/FileProviderEnumerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@ import FileProvider
import NextcloudKit

class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {

var enumeratedItemIdentifier: NSFileProviderItemIdentifier
var serverUrl: String?
let fpUtility = fileProviderUtility()

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)) {
Expand All @@ -56,12 +53,10 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
}

func enumerateItems(for observer: NSFileProviderEnumerationObserver, startingAt page: NSFileProviderPage) {

var items: [NSFileProviderItemProtocol] = []

/*** WorkingSet ***/
if enumeratedItemIdentifier == .workingSet {

var itemIdentifierMetadata: [NSFileProviderItemIdentifier: tableMetadata] = [:]

// ***** Tags *****
Expand Down Expand Up @@ -103,21 +98,17 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
}

if page == NSFileProviderPage.initialPageSortedByDate as NSFileProviderPage || page == NSFileProviderPage.initialPageSortedByName as NSFileProviderPage {

self.readFileOrFolder(serverUrl: serverUrl) { metadatas in
self.completeObserver(observer, numPage: 1, metadatas: metadatas)
}

} else {

let numPage = Int(String(data: page.rawValue, encoding: .utf8)!)!

Check warning on line 105 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)
completeObserver(observer, numPage: numPage, metadatas: nil)
}
}
}

func enumerateChanges(for observer: NSFileProviderChangeObserver, from anchor: NSFileProviderSyncAnchor) {

var itemsDelete: [NSFileProviderItemIdentifier] = []
var itemsUpdate: [FileProviderItem] = []

Expand Down Expand Up @@ -166,18 +157,13 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
// --------------------------------------------------------------------------------------------

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

var numPage = numPage
var items: [NSFileProviderItemProtocol] = []

if metadatas != nil {

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!)
Expand All @@ -197,29 +183,16 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
}

func readFileOrFolder(serverUrl: String, completionHandler: @escaping (_ metadatas: [tableMetadata]?) -> Void) {

var directoryEtag: String?

if let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", fileProviderData.shared.account, serverUrl)) {
directoryEtag = tableDirectory.etag
}

NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "0", showHiddenFiles: NCKeychain().showHiddenFiles) { account, files, _, error in

if directoryEtag != files.first?.etag {

NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: NCKeychain().showHiddenFiles) { account, files, _, error in

if error == .success {
DispatchQueue.global().async {
NCManageDatabase.shared.convertFilesToMetadatas(files, useFirstAsMetadataFolder: true) { _, metadatas in
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)
}
}
} else {
NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: NCKeychain().showHiddenFiles) { account, files, _, error in
if error == .success {
DispatchQueue.global().async {
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)
}
Expand Down

0 comments on commit 0fd93dc

Please sign in to comment.