Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Sources/NextcloudKit/Models/NKDataFileXML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
}
Expand Down
95 changes: 95 additions & 0 deletions Tests/NextcloudKitUnitTests/FilesRootResponseTests.swift
Original file line number Diff line number Diff line change
@@ -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 = """
<d:multistatus xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
<d:response>
<d:href>\(rootHref)</d:href>
<d:propstat>
<d:prop>
<d:resourcetype><d:collection/></d:resourcetype>
<oc:id>00000042instance</oc:id>
<oc:fileid>42</oc:fileid>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>
<d:response>
<d:href>\(rootHref)\(user.hrefComponent)/</d:href>
<d:propstat>
<d:prop>
<d:resourcetype><d:collection/></d:resourcetype>
<oc:id>00000043instance</oc:id>
<oc:fileid>43</oc:fileid>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>
<d:response>
<d:href>\(webroot.hrefPrefix)/remote.php/dav%2Ffiles/\(user.hrefComponent)/</d:href>
<d:propstat>
<d:prop>
<d:resourcetype><d:collection/></d:resourcetype>
<oc:id>00000044instance</oc:id>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>
</d:multistatus>
"""

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)
}
}
Loading