From 277b933a5744e9724b1995250cb6b40d1a17d4a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Coye=20de=20Brune=CC=81lis?= Date: Mon, 30 Dec 2024 11:48:52 +0100 Subject: [PATCH] refactor(DownloadArchiveOperation): Factorised download request --- .../DownloadArchiveOperation.swift | 20 +++++++++++-------- .../DownloadPublicShareArchiveOperation.swift | 11 ++-------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/kDriveCore/Data/DownloadQueue/DownloadOperation/DownloadArchiveOperation.swift b/kDriveCore/Data/DownloadQueue/DownloadOperation/DownloadArchiveOperation.swift index 4ae283bac..ac62eec24 100644 --- a/kDriveCore/Data/DownloadQueue/DownloadOperation/DownloadArchiveOperation.swift +++ b/kDriveCore/Data/DownloadQueue/DownloadOperation/DownloadArchiveOperation.swift @@ -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) @@ -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 diff --git a/kDriveCore/Data/DownloadQueue/DownloadOperation/DownloadPublicShareArchiveOperation.swift b/kDriveCore/Data/DownloadQueue/DownloadOperation/DownloadPublicShareArchiveOperation.swift index 3bbb611a1..45ea097a1 100644 --- a/kDriveCore/Data/DownloadQueue/DownloadOperation/DownloadPublicShareArchiveOperation.swift +++ b/kDriveCore/Data/DownloadQueue/DownloadOperation/DownloadPublicShareArchiveOperation.swift @@ -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) } }