From d6beea0860dff372399ca4fb90d8287ff9d08142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Coye=20de=20Brune=CC=81lis?= Date: Fri, 29 Nov 2024 17:15:14 +0100 Subject: [PATCH] feat: Download public share --- .../DownloadArchiveOperation.swift | 26 ++++++++++++++----- .../Data/DownloadQueue/DownloadQueue.swift | 3 +++ kDriveCore/Data/Models/File.swift | 4 +++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/kDriveCore/Data/DownloadQueue/DownloadArchiveOperation.swift b/kDriveCore/Data/DownloadQueue/DownloadArchiveOperation.swift index b2b2769e9..7df1e755d 100644 --- a/kDriveCore/Data/DownloadQueue/DownloadArchiveOperation.swift +++ b/kDriveCore/Data/DownloadQueue/DownloadArchiveOperation.swift @@ -16,6 +16,7 @@ along with this program. If not, see . */ +import Alamofire import CocoaLumberjackSwift import FileProvider import Foundation @@ -29,6 +30,7 @@ public class DownloadArchiveOperation: Operation { @LazyInjectService var appContextService: AppContextServiceable private let archiveId: String + private let shareDrive: AbstractDrive private let driveFileManager: DriveFileManager private let urlSession: FileDownloadSession private let publicShareProxy: PublicShareProxy? @@ -70,10 +72,12 @@ public class DownloadArchiveOperation: Operation { } public init(archiveId: String, + shareDrive: AbstractDrive, driveFileManager: DriveFileManager, urlSession: FileDownloadSession, publicShareProxy: PublicShareProxy? = nil) { self.archiveId = archiveId + self.shareDrive = shareDrive self.driveFileManager = driveFileManager self.urlSession = urlSession self.publicShareProxy = publicShareProxy @@ -120,6 +124,22 @@ public class DownloadArchiveOperation: Operation { "[DownloadOperation] Downloading Archive of public share files \(archiveId) with session \(urlSession.identifier)" ) + let url = Endpoint.getArchive(drive: shareDrive, uuid: 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() + } + + func authenticatedDownload() { + DDLogInfo("[DownloadOperation] Downloading Archive of files \(archiveId) with session \(urlSession.identifier)") + let url = Endpoint.getArchive(drive: driveFileManager.drive, uuid: archiveId).url if let userToken = accountManager.getTokenForUserId(driveFileManager.drive.userId) { @@ -146,12 +166,6 @@ public class DownloadArchiveOperation: Operation { } } - func authenticatedDownload() { - DDLogInfo("[DownloadOperation] Downloading Archive of files \(archiveId) with session \(urlSession.identifier)") - - // TODO: missing imp - } - func downloadCompletion(url: URL?, response: URLResponse?, error: Error?) { let statusCode = (response as? HTTPURLResponse)?.statusCode ?? -1 diff --git a/kDriveCore/Data/DownloadQueue/DownloadQueue.swift b/kDriveCore/Data/DownloadQueue/DownloadQueue.swift index 11e711834..f6e41dee5 100644 --- a/kDriveCore/Data/DownloadQueue/DownloadQueue.swift +++ b/kDriveCore/Data/DownloadQueue/DownloadQueue.swift @@ -197,6 +197,7 @@ public final class DownloadQueue: ParallelismHeuristicDelegate { let operation = DownloadArchiveOperation( archiveId: archiveId, + shareDrive: publicShareProxy.proxyDrive, driveFileManager: driveFileManager, urlSession: self.bestSession, publicShareProxy: publicShareProxy @@ -209,6 +210,7 @@ public final class DownloadQueue: ParallelismHeuristicDelegate { OperationQueueHelper.disableIdleTimer(false, hasOperationsInQueue: !self.operationsInQueue.isEmpty) } } + self.operationQueue.addOperation(operation) self.archiveOperationsInQueue[archiveId] = operation } @@ -226,6 +228,7 @@ public final class DownloadQueue: ParallelismHeuristicDelegate { let operation = DownloadArchiveOperation( archiveId: archiveId, + shareDrive: drive, driveFileManager: driveFileManager, urlSession: self.bestSession ) diff --git a/kDriveCore/Data/Models/File.swift b/kDriveCore/Data/Models/File.swift index db5690028..c973d6848 100644 --- a/kDriveCore/Data/Models/File.swift +++ b/kDriveCore/Data/Models/File.swift @@ -193,6 +193,10 @@ public struct PublicShareProxy { self.fileId = fileId self.shareLinkUid = shareLinkUid } + + public var proxyDrive: ProxyDrive { + ProxyDrive(id: driveId) + } } public enum SortType: String {