Skip to content

Commit 8bee098

Browse files
async
Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
1 parent 6afa808 commit 8bee098

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Sources/NextcloudKit/NextcloudKitBackground.swift

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
1414

1515
// MARK: - Download
1616

17+
/// Starts a download task for a file from the server to a local path.
18+
///
19+
/// - Parameters:
20+
/// - serverUrlFileName: The URL or URL string of the file to download.
21+
/// - fileNameLocalPath: The local file path where the downloaded file will be saved.
22+
/// - taskDescription: Optional description to set on the URLSession task.
23+
/// - account: The Nextcloud account associated with the download.
24+
///
25+
/// - Returns: A tuple containing:
26+
/// - URLSessionDownloadTask?: The download task if created successfully.
27+
/// - error: An `NKError` indicating success or failure in starting the download.
1728
public func download(serverUrlFileName: Any,
1829
fileNameLocalPath: String,
1930
taskDescription: String? = nil,
@@ -63,8 +74,46 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
6374
return (task, .success)
6475
}
6576

77+
/// Asynchronously starts a download task for a file.
78+
///
79+
/// - Parameters: Same as the synchronous version.
80+
///
81+
/// - Returns: A tuple containing:
82+
/// - downloadTask: The `URLSessionDownloadTask?` if successfully created.
83+
/// - error: The `NKError` result.
84+
public func downloadAsync(serverUrlFileName: Any,
85+
fileNameLocalPath: String,
86+
taskDescription: String? = nil,
87+
account: String) async -> (
88+
downloadTask: URLSessionDownloadTask?,
89+
error: NKError
90+
) {
91+
await withCheckedContinuation { continuation in
92+
let (task, error) = download(serverUrlFileName: serverUrlFileName,
93+
fileNameLocalPath: fileNameLocalPath,
94+
taskDescription: taskDescription,
95+
account: account)
96+
continuation.resume(returning: (downloadTask: task, error: error))
97+
}
98+
}
99+
66100
// MARK: - Upload
67101

102+
/// Starts an upload task to send a local file to the server.
103+
///
104+
/// - Parameters:
105+
/// - serverUrlFileName: The server URL or URL string where the file will be uploaded.
106+
/// - fileNameLocalPath: The local file path of the file to upload.
107+
/// - dateCreationFile: Optional creation date metadata for the file.
108+
/// - dateModificationFile: Optional modification date metadata for the file.
109+
/// - taskDescription: Optional description to set on the URLSession task.
110+
/// - overwrite: Boolean indicating whether to overwrite existing files on the server.
111+
/// - account: The Nextcloud account associated with the upload.
112+
/// - sessionIdentifier: A string identifier for the upload session.
113+
///
114+
/// - Returns: A tuple containing:
115+
/// - URLSessionUploadTask?: The upload task if created successfully.
116+
/// - error: An `NKError` indicating success or failure in starting the upload.
68117
public func upload(serverUrlFileName: Any,
69118
fileNameLocalPath: String,
70119
dateCreationFile: Date?,
@@ -138,6 +187,37 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
138187
return (task, .success)
139188
}
140189

190+
/// Asynchronously starts an upload task to send a local file.
191+
///
192+
/// - Parameters: Same as the synchronous version.
193+
///
194+
/// - Returns: A tuple containing:
195+
/// - uploadTask: The `URLSessionUploadTask?` if successfully created.
196+
/// - error: The `NKError` result.
197+
public func uploadAsync(serverUrlFileName: Any,
198+
fileNameLocalPath: String,
199+
dateCreationFile: Date?,
200+
dateModificationFile: Date?,
201+
taskDescription: String? = nil,
202+
overwrite: Bool = false,
203+
account: String,
204+
sessionIdentifier: String) async -> (
205+
uploadTask: URLSessionUploadTask?,
206+
error: NKError
207+
) {
208+
await withCheckedContinuation { continuation in
209+
let (task, error) = upload(serverUrlFileName: serverUrlFileName,
210+
fileNameLocalPath: fileNameLocalPath,
211+
dateCreationFile: dateCreationFile,
212+
dateModificationFile: dateModificationFile,
213+
taskDescription: taskDescription,
214+
overwrite: overwrite,
215+
account: account,
216+
sessionIdentifier: sessionIdentifier)
217+
continuation.resume(returning: (uploadTask: task, error: error))
218+
}
219+
}
220+
141221
// MARK: - SessionDelegate
142222

143223
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

0 commit comments

Comments
 (0)