Skip to content

Commit 2777583

Browse files
Merge pull request #216 from nextcloud/NCDav
NKDAV
2 parents 8c308aa + 95c98cd commit 2777583

3 files changed

Lines changed: 42 additions & 8 deletions

File tree

Sources/NextcloudKit/NKCommon.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,6 @@ public struct NKCommon: Sendable {
407407
return 0
408408
}
409409

410-
public func returnPathfromServerUrl(_ serverUrl: String, urlBase: String, userId: String) -> String {
411-
let home = urlBase + "/remote.php/dav/files/" + userId
412-
return serverUrl.replacingOccurrences(of: home, with: "")
413-
}
414-
415410
public func getSessionErrorFromAFError(_ afError: AFError?) -> NSError? {
416411
if let afError = afError?.asAFError {
417412
switch afError {

Sources/NextcloudKit/NKDav.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-FileCopyrightText: Nextcloud GmbH
2+
// SPDX-FileCopyrightText: 2026 Marino Faggiana
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
/// Utility namespace for constructing Nextcloud WebDAV (DAV) endpoint paths.
6+
///
7+
/// The standard DAV structure in Nextcloud is:
8+
/// `<baseURL>/remote.php/dav/files/<userId>/`
9+
public enum NKDav {
10+
11+
// Base DAV endpoint used by Nextcloud
12+
public static let basePath = "/remote.php/dav/"
13+
14+
// Files namespace within DAV
15+
public static let filesPath = "files/"
16+
17+
/// Builds the user DAV path with trailing slash
18+
/// Example: /remote.php/dav/files/<userId>/
19+
public static func userPath(userId: String) -> String {
20+
basePath + filesPath + userId + "/"
21+
}
22+
23+
/// Builds the user DAV path without trailing slash
24+
/// Example: /remote.php/dav/files/<userId>
25+
public static func userPathNoSlash(userId: String) -> String {
26+
basePath + filesPath + userId
27+
}
28+
29+
/// Full DAV URL (with trailing slash)
30+
public static func homeURLString(urlBase: String, userId: String) -> String {
31+
urlBase + userPath(userId: userId)
32+
}
33+
34+
/// Full DAV URL (without trailing slash)
35+
public static func homeURLStringNoSlash(urlBase: String, userId: String) -> String {
36+
urlBase + userPathNoSlash(userId: userId)
37+
}
38+
}

Sources/NextcloudKit/NextcloudKit+Upload.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,10 @@ public extension NextcloudKit {
250250
// Build endpoints and headers
251251
let totalFileSize = self.nkCommonInstance.getFileSize(filePath: directory + "/" + fileName)
252252
let serverUrlChunkFolder = nkSession.urlBase + "/" + nkSession.dav + "/uploads/" + nkSession.userId + "/" + chunkFolder
253-
let serverUrlFileName = nkSession.urlBase + "/" + nkSession.dav + "/files/" + nkSession.userId
254-
+ self.nkCommonInstance.returnPathfromServerUrl(serverUrl, urlBase: nkSession.urlBase, userId: nkSession.userId)
255-
+ "/" + (destinationFileName ?? fileName)
253+
let serverUrlFileName = NKDav.homeURLString(urlBase: nkSession.urlBase, userId: nkSession.userId)
254+
+ serverUrl.replacingOccurrences(of: NKDav.homeURLStringNoSlash(urlBase: nkSession.urlBase, userId: nkSession.userId), with: "")
255+
+ "/"
256+
+ (destinationFileName ?? fileName)
256257

257258
if options.customHeader == nil { options.customHeader = [:] }
258259
options.customHeader?["Destination"] = serverUrlFileName.urlEncoded

0 commit comments

Comments
 (0)