Skip to content
Merged
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: 7 additions & 7 deletions Sources/NextcloudKit/NextcloudKit+Login.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Alamofire
import SwiftyJSON

public extension NextcloudKit {
// MARK: - App Password
// MARK: - App Password / Onetime

/// Retrieves an app password (token) for the given user credentials and server URL.
///
Expand Down Expand Up @@ -106,12 +106,12 @@ public extension NextcloudKit {
/// - taskHandler: Callback for observing the underlying URLSessionTask.
/// - completion: Returns the token string (if any), raw response data, and NKError result.
func getAppPasswordOnetime(url: String,
user: String,
onetimeToken: String,
userAgent: String? = nil,
options: NKRequestOptions = NKRequestOptions(),
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
completion: @escaping (_ token: String?, _ responseData: AFDataResponse<Data>?, _ error: NKError) -> Void) {
user: String,
onetimeToken: String,
userAgent: String? = nil,
options: NKRequestOptions = NKRequestOptions(),
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
completion: @escaping (_ token: String?, _ responseData: AFDataResponse<Data>?, _ error: NKError) -> Void) {
let endpoint = "ocs/v2.php/core/getapppassword-onetime"
guard let url = self.nkCommonInstance.createStandardUrl(serverUrl: url, endpoint: endpoint) else {
return options.queue.async { completion(nil, nil, .urlError) }
Expand Down
21 changes: 10 additions & 11 deletions Sources/NextcloudKit/NextcloudKit+Upload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public extension NextcloudKit {
/// - dateCreationFile: Optional creation date to include in headers (X-OC-CTime).
/// - dateModificationFile: Optional modification date to include in headers (X-OC-MTime).
/// - overwrite: If true, the remote file will be overwritten if it already exists.
/// - autoMkcol: When set to 1, instructs the server to automatically create any missing parent directories when uploading a file.
/// - account: The account associated with the upload session.
/// - options: Optional configuration for the request (headers, queue, timeout, etc.).
/// - requestHandler: Called with the created UploadRequest.
Expand All @@ -33,6 +34,7 @@ public extension NextcloudKit {
dateCreationFile: Date? = nil,
dateModificationFile: Date? = nil,
overwrite: Bool = false,
autoMkcol: Bool = false,
account: String,
options: NKRequestOptions = NKRequestOptions(),
requestHandler: @escaping (_ request: UploadRequest) -> Void = { _ in },
Expand Down Expand Up @@ -64,8 +66,13 @@ public extension NextcloudKit {
if overwrite {
headers.update(name: "Overwrite", value: "true")
}
if autoMkcol {
headers.update(name: "X-NC-WebDAV-Auto-Mkcol", value: "1")
}
headers.update(.contentType("application/octet-stream"))

// X-NC-WebDAV-Auto-Mkcol

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
task.taskDescription = options.taskDescription
options.queue.async { taskHandler(task) }
Expand Down Expand Up @@ -104,17 +111,7 @@ public extension NextcloudKit {

/// Asynchronously uploads a file to the Nextcloud server.
///
/// - Parameters:
/// - serverUrlFileName: The remote server URL or path where the file will be uploaded.
/// - fileNameLocalPath: The local file path to be uploaded.
/// - dateCreationFile: Optional creation date to include in headers (X-OC-CTime).
/// - dateModificationFile: Optional modification date to include in headers (X-OC-MTime).
/// - overwrite: If true, the remote file will be overwritten if it already exists.
/// - account: The account associated with the upload session.
/// - options: Optional configuration for the request (headers, queue, timeout, etc.).
/// - requestHandler: Called with the created UploadRequest.
/// - taskHandler: Called with the underlying URLSessionTask when it's created.
/// - progressHandler: Called periodically with upload progress.
/// - Parameters: Same as the synchronous version.
///
/// - Returns: A tuple containing:
/// - account: The account used for the upload.
Expand All @@ -129,6 +126,7 @@ public extension NextcloudKit {
dateCreationFile: Date? = nil,
dateModificationFile: Date? = nil,
overwrite: Bool = false,
autoMkcol: Bool = false,
account: String,
options: NKRequestOptions = NKRequestOptions(),
requestHandler: @escaping (_ request: UploadRequest) -> Void = { _ in },
Expand All @@ -149,6 +147,7 @@ public extension NextcloudKit {
dateCreationFile: dateCreationFile,
dateModificationFile: dateModificationFile,
overwrite: overwrite,
autoMkcol: autoMkcol,
account: account,
options: options,
requestHandler: requestHandler,
Expand Down
7 changes: 7 additions & 0 deletions Sources/NextcloudKit/NextcloudKitBackground.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
/// - dateModificationFile: Optional modification date metadata for the file.
/// - taskDescription: Optional description to set on the URLSession task.
/// - overwrite: Boolean indicating whether to overwrite existing files on the server.
/// - autoMkcol: When set to 1, instructs the server to automatically create any missing parent directories when uploading a file.
/// - account: The Nextcloud account associated with the upload.
/// - sessionIdentifier: A string identifier for the upload session.
///
Expand All @@ -136,6 +137,7 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
dateModificationFile: Date?,
taskDescription: String? = nil,
overwrite: Bool = false,
autoMkcol: Bool = false,
account: String,
automaticResume: Bool = true,
sessionIdentifier: String) -> (URLSessionUploadTask?, error: NKError) {
Expand Down Expand Up @@ -185,6 +187,9 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
if overwrite {
request.setValue("true", forHTTPHeaderField: "Overwrite")
}
if autoMkcol {
request.setValue("1", forHTTPHeaderField: "X-NC-WebDAV-Auto-Mkcol")
}
// Epoch of linux do not permitted negativ value
if let dateCreationFile, dateCreationFile.timeIntervalSince1970 > 0 {
request.setValue("\(dateCreationFile.timeIntervalSince1970)", forHTTPHeaderField: "X-OC-CTime")
Expand Down Expand Up @@ -225,6 +230,7 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
dateModificationFile: Date?,
taskDescription: String? = nil,
overwrite: Bool = false,
autoMkcol: Bool = false,
account: String,
automaticResume: Bool = true,
sessionIdentifier: String) async -> (
Expand All @@ -238,6 +244,7 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
dateModificationFile: dateModificationFile,
taskDescription: taskDescription,
overwrite: overwrite,
autoMkcol: autoMkcol,
account: account,
automaticResume: automaticResume,
sessionIdentifier: sessionIdentifier)
Expand Down
Loading