Skip to content

Commit 9e7b197

Browse files
Merge pull request #208 from nextcloud/X-NC-WebDAV-Auto-Mkcol
mkcol - upload
2 parents 21be08a + dcb6a33 commit 9e7b197

3 files changed

Lines changed: 24 additions & 18 deletions

File tree

Sources/NextcloudKit/NextcloudKit+Login.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Alamofire
77
import SwiftyJSON
88

99
public extension NextcloudKit {
10-
// MARK: - App Password
10+
// MARK: - App Password / Onetime
1111

1212
/// Retrieves an app password (token) for the given user credentials and server URL.
1313
///
@@ -106,12 +106,12 @@ public extension NextcloudKit {
106106
/// - taskHandler: Callback for observing the underlying URLSessionTask.
107107
/// - completion: Returns the token string (if any), raw response data, and NKError result.
108108
func getAppPasswordOnetime(url: String,
109-
user: String,
110-
onetimeToken: String,
111-
userAgent: String? = nil,
112-
options: NKRequestOptions = NKRequestOptions(),
113-
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
114-
completion: @escaping (_ token: String?, _ responseData: AFDataResponse<Data>?, _ error: NKError) -> Void) {
109+
user: String,
110+
onetimeToken: String,
111+
userAgent: String? = nil,
112+
options: NKRequestOptions = NKRequestOptions(),
113+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
114+
completion: @escaping (_ token: String?, _ responseData: AFDataResponse<Data>?, _ error: NKError) -> Void) {
115115
let endpoint = "ocs/v2.php/core/getapppassword-onetime"
116116
guard let url = self.nkCommonInstance.createStandardUrl(serverUrl: url, endpoint: endpoint) else {
117117
return options.queue.async { completion(nil, nil, .urlError) }

Sources/NextcloudKit/NextcloudKit+Upload.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public extension NextcloudKit {
1515
/// - dateCreationFile: Optional creation date to include in headers (X-OC-CTime).
1616
/// - dateModificationFile: Optional modification date to include in headers (X-OC-MTime).
1717
/// - overwrite: If true, the remote file will be overwritten if it already exists.
18+
/// - autoMkcol: When set to 1, instructs the server to automatically create any missing parent directories when uploading a file.
1819
/// - account: The account associated with the upload session.
1920
/// - options: Optional configuration for the request (headers, queue, timeout, etc.).
2021
/// - requestHandler: Called with the created UploadRequest.
@@ -33,6 +34,7 @@ public extension NextcloudKit {
3334
dateCreationFile: Date? = nil,
3435
dateModificationFile: Date? = nil,
3536
overwrite: Bool = false,
37+
autoMkcol: Bool = false,
3638
account: String,
3739
options: NKRequestOptions = NKRequestOptions(),
3840
requestHandler: @escaping (_ request: UploadRequest) -> Void = { _ in },
@@ -64,8 +66,13 @@ public extension NextcloudKit {
6466
if overwrite {
6567
headers.update(name: "Overwrite", value: "true")
6668
}
69+
if autoMkcol {
70+
headers.update(name: "X-NC-WebDAV-Auto-Mkcol", value: "1")
71+
}
6772
headers.update(.contentType("application/octet-stream"))
6873

74+
// X-NC-WebDAV-Auto-Mkcol
75+
6976
let request = nkSession.sessionData.upload(fileNameLocalPathUrl, to: url, method: .put, headers: headers, interceptor: NKInterceptor(nkCommonInstance: nkCommonInstance), fileManager: .default).validate(statusCode: 200..<300).onURLSessionTaskCreation(perform: { task in
7077
task.taskDescription = options.taskDescription
7178
options.queue.async { taskHandler(task) }
@@ -104,17 +111,7 @@ public extension NextcloudKit {
104111

105112
/// Asynchronously uploads a file to the Nextcloud server.
106113
///
107-
/// - Parameters:
108-
/// - serverUrlFileName: The remote server URL or path where the file will be uploaded.
109-
/// - fileNameLocalPath: The local file path to be uploaded.
110-
/// - dateCreationFile: Optional creation date to include in headers (X-OC-CTime).
111-
/// - dateModificationFile: Optional modification date to include in headers (X-OC-MTime).
112-
/// - overwrite: If true, the remote file will be overwritten if it already exists.
113-
/// - account: The account associated with the upload session.
114-
/// - options: Optional configuration for the request (headers, queue, timeout, etc.).
115-
/// - requestHandler: Called with the created UploadRequest.
116-
/// - taskHandler: Called with the underlying URLSessionTask when it's created.
117-
/// - progressHandler: Called periodically with upload progress.
114+
/// - Parameters: Same as the synchronous version.
118115
///
119116
/// - Returns: A tuple containing:
120117
/// - account: The account used for the upload.
@@ -129,6 +126,7 @@ public extension NextcloudKit {
129126
dateCreationFile: Date? = nil,
130127
dateModificationFile: Date? = nil,
131128
overwrite: Bool = false,
129+
autoMkcol: Bool = false,
132130
account: String,
133131
options: NKRequestOptions = NKRequestOptions(),
134132
requestHandler: @escaping (_ request: UploadRequest) -> Void = { _ in },
@@ -149,6 +147,7 @@ public extension NextcloudKit {
149147
dateCreationFile: dateCreationFile,
150148
dateModificationFile: dateModificationFile,
151149
overwrite: overwrite,
150+
autoMkcol: autoMkcol,
152151
account: account,
153152
options: options,
154153
requestHandler: requestHandler,

Sources/NextcloudKit/NextcloudKitBackground.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
124124
/// - dateModificationFile: Optional modification date metadata for the file.
125125
/// - taskDescription: Optional description to set on the URLSession task.
126126
/// - overwrite: Boolean indicating whether to overwrite existing files on the server.
127+
/// - autoMkcol: When set to 1, instructs the server to automatically create any missing parent directories when uploading a file.
127128
/// - account: The Nextcloud account associated with the upload.
128129
/// - sessionIdentifier: A string identifier for the upload session.
129130
///
@@ -136,6 +137,7 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
136137
dateModificationFile: Date?,
137138
taskDescription: String? = nil,
138139
overwrite: Bool = false,
140+
autoMkcol: Bool = false,
139141
account: String,
140142
automaticResume: Bool = true,
141143
sessionIdentifier: String) -> (URLSessionUploadTask?, error: NKError) {
@@ -185,6 +187,9 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
185187
if overwrite {
186188
request.setValue("true", forHTTPHeaderField: "Overwrite")
187189
}
190+
if autoMkcol {
191+
request.setValue("1", forHTTPHeaderField: "X-NC-WebDAV-Auto-Mkcol")
192+
}
188193
// Epoch of linux do not permitted negativ value
189194
if let dateCreationFile, dateCreationFile.timeIntervalSince1970 > 0 {
190195
request.setValue("\(dateCreationFile.timeIntervalSince1970)", forHTTPHeaderField: "X-OC-CTime")
@@ -225,6 +230,7 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
225230
dateModificationFile: Date?,
226231
taskDescription: String? = nil,
227232
overwrite: Bool = false,
233+
autoMkcol: Bool = false,
228234
account: String,
229235
automaticResume: Bool = true,
230236
sessionIdentifier: String) async -> (
@@ -238,6 +244,7 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
238244
dateModificationFile: dateModificationFile,
239245
taskDescription: taskDescription,
240246
overwrite: overwrite,
247+
autoMkcol: autoMkcol,
241248
account: account,
242249
automaticResume: automaticResume,
243250
sessionIdentifier: sessionIdentifier)

0 commit comments

Comments
 (0)