Skip to content

Commit

Permalink
refactor(DownloadArchiveOperation): Factorised download request
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-coye committed Dec 30, 2024
1 parent 3ff747c commit 277b933
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,7 @@ public class DownloadArchiveOperation: Operation {
if let token {
var request = URLRequest(url: url)
request.setValue("Bearer \(token.accessToken)", forHTTPHeaderField: "Authorization")
task = urlSession.downloadTask(with: request, completionHandler: downloadCompletion)
progressObservation = task?.progress.observe(\.fractionCompleted, options: .new) { _, value in
guard let newValue = value.newValue else {
return
}
DownloadQueue.instance.publishProgress(newValue, for: archiveId)
}
task?.resume()
downloadRequest(request)
} else {
error = .localError // Other error?
end(sessionUrl: url)
Expand All @@ -142,6 +135,17 @@ public class DownloadArchiveOperation: Operation {
}
}

func downloadRequest(_ request: URLRequest) {
task = urlSession.downloadTask(with: request, completionHandler: downloadCompletion)
progressObservation = task?.progress.observe(\.fractionCompleted, options: .new) { _, value in
guard let newValue = value.newValue else {
return
}
DownloadQueue.instance.publishProgress(newValue, for: self.archiveId)
}
task?.resume()
}

func downloadCompletion(url: URL?, response: URLResponse?, error: Error?) {
let statusCode = (response as? HTTPURLResponse)?.statusCode ?? -1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,8 @@ public final class DownloadPublicShareArchiveOperation: DownloadArchiveOperation
linkUuid: publicShareProxy.shareLinkUid,
archiveUuid: archiveId
).url
let request = URLRequest(url: url)

task = urlSession.downloadTask(with: request, completionHandler: downloadCompletion)
progressObservation = task?.progress.observe(\.fractionCompleted, options: .new) { _, value in
guard let newValue = value.newValue else {
return
}
DownloadQueue.instance.publishProgress(newValue, for: self.archiveId)
}
task?.resume()
let request = URLRequest(url: url)
downloadRequest(request)
}
}

0 comments on commit 277b933

Please sign in to comment.