Move item metadata fetch into util file in FileProviderUIExt
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Tue, 30 Jul 2024 10:47:38 +0000 (18:47 +0800)
committerMatthieu Gallien <matthieu_gallien@yahoo.fr>
Thu, 12 Sep 2024 07:50:50 +0000 (09:50 +0200)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/Extensions/Logger+Extensions.swift
shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/MetadataProvider.swift [new file with mode: 0644]
shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/Sharing/ShareTableViewDataSource.swift
shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj

index 960171034da09beca320cb942ae9090428eaa6e8..64836328b5ebd022b9c62d9dfc7f6f3ab808c339 100644 (file)
@@ -11,6 +11,7 @@ extension Logger {
     private static var subsystem = Bundle.main.bundleIdentifier!
 
     static let actionViewController = Logger(subsystem: subsystem, category: "actionViewController")
+    static let metadataProvider = Logger(subsystem: subsystem, category: "metadataProvider")
     static let shareCapabilities = Logger(subsystem: subsystem, category: "shareCapabilities")
     static let shareController = Logger(subsystem: subsystem, category: "shareController")
     static let shareeDataSource = Logger(subsystem: subsystem, category: "shareeDataSource")
diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/MetadataProvider.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/MetadataProvider.swift
new file mode 100644 (file)
index 0000000..890c320
--- /dev/null
@@ -0,0 +1,45 @@
+//
+//  MetadataProvider.swift
+//  FileProviderUIExt
+//
+//  Created by Claudio Cambra on 30/7/24.
+//
+
+import Foundation
+import NextcloudKit
+import OSLog
+
+func fetchItemMetadata(itemRelativePath: String, kit: NextcloudKit) async -> NKFile? {
+    func slashlessPath(_ string: String) -> String {
+        var strCopy = string
+        if strCopy.hasPrefix("/") {
+            strCopy.removeFirst()
+        }
+        if strCopy.hasSuffix("/") {
+            strCopy.removeLast()
+        }
+        return strCopy
+    }
+
+    let nkCommon = kit.nkCommonInstance
+    let urlBase = slashlessPath(nkCommon.urlBase)
+    let davSuffix = slashlessPath(nkCommon.dav)
+    let userId = nkCommon.userId
+    let itemRelPath = slashlessPath(itemRelativePath)
+
+    let itemFullServerPath = "\(urlBase)/\(davSuffix)/files/\(userId)/\(itemRelPath)"
+    return await withCheckedContinuation { continuation in
+        kit.readFileOrFolder(serverUrlFileName: itemFullServerPath, depth: "0") {
+            account, files, data, error in
+            guard error == .success else {
+                Logger.metadataProvider.error(
+                    "Error getting item metadata: \(error.errorDescription)"
+                )
+                continuation.resume(returning: nil)
+                return
+            }
+            Logger.metadataProvider.info("Successfully retrieved item metadata")
+            continuation.resume(returning: files.first)
+        }
+    }
+}
index f0d0baf99c85269607f260d407a7cf05097c9f5c..826711f0c626027f6411478ffa007d972aa2daae 100644 (file)
@@ -66,7 +66,14 @@ class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDele
     }
 
     func reload() async {
-        guard let itemURL = itemURL else { return }
+        guard let itemURL else {
+            presentError("No item URL, cannot reload data!")
+            return
+        }
+        guard let kit else {
+            presentError("NextcloudKit instance is unavailable, cannot reload data!")
+            return
+        }
         guard let itemIdentifier = await withCheckedContinuation({
             (continuation: CheckedContinuation<NSFileProviderItemIdentifier?, Never>) -> Void in
             NSFileProviderManager.getIdentifierForUserVisibleFile(
@@ -106,7 +113,7 @@ class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDele
                 presentError("Server does not support shares.")
                 return
             }
-            itemMetadata = await fetchItemMetadata(itemRelativePath: serverPathString)
+            itemMetadata = await fetchItemMetadata(itemRelativePath: serverPathString, kit: kit)
             guard itemMetadata?.permissions.contains("R") == true else {
                 presentError("This file cannot be shared.")
                 return
@@ -163,44 +170,6 @@ class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDele
         }
     }
 
-    private func fetchItemMetadata(itemRelativePath: String) async -> NKFile? {
-        guard let kit = kit else {
-            presentError("Could not fetch item metadata as NextcloudKit instance is unavailable")
-            return nil
-        }
-
-        func slashlessPath(_ string: String) -> String {
-            var strCopy = string
-            if strCopy.hasPrefix("/") {
-                strCopy.removeFirst()
-            }
-            if strCopy.hasSuffix("/") {
-                strCopy.removeLast()
-            }
-            return strCopy
-        }
-
-        let nkCommon = kit.nkCommonInstance
-        let urlBase = slashlessPath(nkCommon.urlBase)
-        let davSuffix = slashlessPath(nkCommon.dav)
-        let userId = nkCommon.userId
-        let itemRelPath = slashlessPath(itemRelativePath)
-
-        let itemFullServerPath = "\(urlBase)/\(davSuffix)/files/\(userId)/\(itemRelPath)"
-        return await withCheckedContinuation { continuation in
-            kit.readFileOrFolder(serverUrlFileName: itemFullServerPath, depth: "0") {
-                account, files, data, error in
-                guard error == .success else {
-                    self.presentError("Error getting item metadata: \(error.errorDescription)")
-                    continuation.resume(returning: nil)
-                    return
-                }
-                Logger.sharesDataSource.info("Successfully retrieved item metadata")
-                continuation.resume(returning: files.first)
-            }
-        }
-    }
-
     private func presentError(_ errorString: String) {
         Logger.sharesDataSource.error("\(errorString, privacy: .public)")
         Task { @MainActor in self.uiDelegate?.showError(errorString) }
index ff6aa0624e36866e08099050bcbe990043e04856..b2613dddd6c04b5e003ef524e00054cd5000f607 100644 (file)
@@ -28,6 +28,7 @@
                537BD67A2C58D67800446ED0 /* LockViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537BD6792C58D67800446ED0 /* LockViewController.swift */; };
                537BD67C2C58D7B700446ED0 /* LockViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 537BD67B2C58D7B700446ED0 /* LockViewController.xib */; };
                537BD6802C58F01B00446ED0 /* FileProviderCommunication.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537BD67F2C58F01B00446ED0 /* FileProviderCommunication.swift */; };
+               537BD6822C58F72E00446ED0 /* MetadataProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537BD6812C58F72E00446ED0 /* MetadataProvider.swift */; };
                538E396A27F4765000FA63D5 /* UniformTypeIdentifiers.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 538E396927F4765000FA63D5 /* UniformTypeIdentifiers.framework */; };
                538E396D27F4765000FA63D5 /* FileProviderExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 538E396C27F4765000FA63D5 /* FileProviderExtension.swift */; };
                538E397627F4765000FA63D5 /* FileProviderExt.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 538E396727F4765000FA63D5 /* FileProviderExt.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
                537BD6792C58D67800446ED0 /* LockViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LockViewController.swift; sourceTree = "<group>"; };
                537BD67B2C58D7B700446ED0 /* LockViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = LockViewController.xib; sourceTree = "<group>"; };
                537BD67F2C58F01B00446ED0 /* FileProviderCommunication.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileProviderCommunication.swift; sourceTree = "<group>"; };
+               537BD6812C58F72E00446ED0 /* MetadataProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetadataProvider.swift; sourceTree = "<group>"; };
                538E396727F4765000FA63D5 /* FileProviderExt.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = FileProviderExt.appex; sourceTree = BUILT_PRODUCTS_DIR; };
                538E396927F4765000FA63D5 /* UniformTypeIdentifiers.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UniformTypeIdentifiers.framework; path = System/Library/Frameworks/UniformTypeIdentifiers.framework; sourceTree = SDKROOT; };
                538E396C27F4765000FA63D5 /* FileProviderExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileProviderExtension.swift; sourceTree = "<group>"; };
                                537BD6772C58D0C400446ED0 /* Sharing */,
                                53B979802B84C81F002DA742 /* DocumentActionViewController.swift */,
                                537BD67F2C58F01B00446ED0 /* FileProviderCommunication.swift */,
+                               537BD6812C58F72E00446ED0 /* MetadataProvider.swift */,
                                53FE14572B8E3A7C006C4193 /* FileProviderUIExt.entitlements */,
                                53B979852B84C81F002DA742 /* Info.plist */,
                        );
                        isa = PBXSourcesBuildPhase;
                        buildActionMask = 2147483647;
                        files = (
+                               537BD6822C58F72E00446ED0 /* MetadataProvider.swift in Sources */,
                                537630932B85F4B00026BFAB /* ShareViewController.swift in Sources */,
                                53FE14672B8F78B6006C4193 /* ShareOptionsView.swift in Sources */,
                                53651E462BBC0D9500ECAC29 /* ShareeSuggestionsDataSource.swift in Sources */,