diff --git a/Sources/NextcloudKit/Models/NKDataFileXML.swift b/Sources/NextcloudKit/Models/NKDataFileXML.swift index c723efec..0e1cbc61 100644 --- a/Sources/NextcloudKit/Models/NKDataFileXML.swift +++ b/Sources/NextcloudKit/Models/NKDataFileXML.swift @@ -279,7 +279,9 @@ public class NKDataFileXML: NSObject { func convertDataFile(xmlData: Data, nkSession: NKSession, rootFileName: String, showHiddenFiles: Bool, includeHiddenFiles: [String]) async -> [NKFile] { var files: [NKFile] = [] - let rootFiles = "/" + nkSession.dav + "/files/" + let webroot = URLComponents(string: nkSession.urlBase)?.percentEncodedPath ?? "" + let rootFiles = NSString.path(withComponents: ["/", webroot, nkSession.dav, "files"]) + "/" + let davUserId = nkSession.userId.isEmpty ? nkSession.user : nkSession.userId guard let baseUrl = self.nkCommonInstance.getHostName(urlString: nkSession.urlBase) else { return files } @@ -314,17 +316,17 @@ public class NKDataFileXML: NSObject { file.account = nkSession.account // path - file.path = (fileNamePath as NSString).deletingLastPathComponent + "/" - file.path = file.path.removingPercentEncoding ?? "" + let parentPath = (fileNamePath as NSString).deletingLastPathComponent + "/" + file.path = parentPath.removingPercentEncoding ?? "" // fileName file.fileName = (fileNamePath as NSString).lastPathComponent file.fileName = file.fileName.removingPercentEncoding ?? "" - // ServerUrl - if href == rootFiles + nkSession.user + "/" { + // An encoded separator in the parent must not match the DAV Files collection. + if parentPath == rootFiles, file.fileName.utf8.elementsEqual(davUserId.utf8) { file.fileName = rootFileName - file.serverUrl = baseUrl + rootFiles + nkSession.user + file.serverUrl = baseUrl + file.path + davUserId } else { file.serverUrl = baseUrl + file.path.dropLast() } diff --git a/Tests/NextcloudKitUnitTests/FilesRootResponseTests.swift b/Tests/NextcloudKitUnitTests/FilesRootResponseTests.swift new file mode 100644 index 00000000..1faabb86 --- /dev/null +++ b/Tests/NextcloudKitUnitTests/FilesRootResponseTests.swift @@ -0,0 +1,95 @@ +// SPDX-FileCopyrightText: Nextcloud GmbH +// SPDX-FileCopyrightText: 2026 Claudio Cambra +// SPDX-License-Identifier: GPL-3.0-or-later + +import Foundation +import Testing +@testable import NextcloudKit + +struct FilesRootResponseTests { + @Test( + arguments: [("backend-user", "backend-user"), ("backend user", "backend%20user"), ("", "login%40example.invalid")], + [("", ""), ("/nextcloud", "/nextcloud"), ("/nextcloud%20install/", "/nextcloud%20install")] + ) + func identifiesFilesRoot( + user: (id: String, hrefComponent: String), + webroot: (baseURLPath: String, hrefPrefix: String) + ) async throws { + let common = NKCommon() + let origin = "https://example.invalid" + let session = NKSession( + nkCommonInstance: common, + urlBase: origin + webroot.baseURLPath, + user: "login@example.invalid", + userId: user.id, + password: "", + account: UUID().uuidString, + userAgent: "NextcloudKitTests", + groupIdentifier: "", + httpMaximumConnectionsPerHost: 1, + httpMaximumConnectionsPerHostInDownload: 1, + httpMaximumConnectionsPerHostInUpload: 1 + ) + let rootHref = "\(webroot.hrefPrefix)/remote.php/dav/files/\(user.hrefComponent)/" + let xml = """ + + + \(rootHref) + + + + 00000042instance + 42 + + HTTP/1.1 200 OK + + + + \(rootHref)\(user.hrefComponent)/ + + + + 00000043instance + 43 + + HTTP/1.1 200 OK + + + + \(webroot.hrefPrefix)/remote.php/dav%2Ffiles/\(user.hrefComponent)/ + + + + 00000044instance + + HTTP/1.1 200 OK + + + + """ + + let files = await NKDataFileXML(nkCommonInstance: common).convertDataFile( + xmlData: Data(xml.utf8), + nkSession: session, + rootFileName: common.rootFileName, + showHiddenFiles: true, + includeHiddenFiles: [] + ) + + #expect(files.count == 3) + let root = try #require(files.first { $0.ocId == "00000042instance" }) + let child = try #require(files.first { $0.ocId == "00000043instance" }) + let rootURL = try #require((origin + rootHref.dropLast()).removingPercentEncoding) + let expectedUserID = try #require(user.hrefComponent.removingPercentEncoding) + #expect(root.fileName == common.rootFileName) + #expect(root.serverUrl == rootURL) + #expect(root.directory) + #expect(root.fileId == "42") + #expect(child.fileName == expectedUserID) + #expect(child.serverUrl == rootURL) + #expect(child.directory) + #expect(child.fileId == "43") + let differentParent = try #require(files.first { $0.ocId == "00000044instance" }) + #expect(differentParent.fileName == expectedUserID) + } +}