Fix scan path to retrieve item metadata in share table view data source
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Tue, 19 Mar 2024 11:38:41 +0000 (19:38 +0800)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Wed, 17 Apr 2024 08:39:02 +0000 (16:39 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareTableViewDataSource.swift

index db7bf883b1846ad99932c4bb7250b9f1e080bc58..c55306093e4ced8f04c7ce6f6528aab14b0cd805 100644 (file)
@@ -173,8 +173,33 @@ class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDele
     }
 
     private func fetchItemMetadata(itemRelativePath: String) async -> NKFile? {
+        guard let kit = kit else {
+            let errorString = "Could not fetch item metadata as nckit unavailable"
+            Logger.sharesDataSource.error("\(errorString, privacy: .public)")
+            Task { @MainActor in self.uiDelegate?.showError(errorString) }
+            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: itemRelativePath, depth: "0") {
+            kit.readFileOrFolder(serverUrlFileName: itemFullServerPath, depth: "0") {
                 account, files, data, error in
                 guard error == .success else {
                     let errorString = "Error getting item metadata: \(error.errorDescription)"