Skip to content

Commit

Permalink
refactor(DownloadArchiveOperation): Split dedicated public share code
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-coye committed Dec 30, 2024
1 parent 20ff0ef commit 3ff747c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public class DownloadArchiveOperation: Operation {
@LazyInjectService var accountManager: AccountManageable
@LazyInjectService var appContextService: AppContextServiceable

private let archiveId: String
private let shareDrive: AbstractDrive
private let driveFileManager: DriveFileManager
private let urlSession: FileDownloadSession
private let publicShareProxy: PublicShareProxy?
private var progressObservation: NSKeyValueObservation?
private var backgroundTaskIdentifier: UIBackgroundTaskIdentifier = .invalid

let archiveId: String
let shareDrive: AbstractDrive
let urlSession: FileDownloadSession
var progressObservation: NSKeyValueObservation?

public var task: URLSessionDownloadTask?
public var error: DriveError?
public var archiveUrl: URL?
Expand Down Expand Up @@ -74,13 +74,11 @@ public class DownloadArchiveOperation: Operation {
public init(archiveId: String,
shareDrive: AbstractDrive,
driveFileManager: DriveFileManager,
urlSession: FileDownloadSession,
publicShareProxy: PublicShareProxy? = nil) {
urlSession: FileDownloadSession) {
self.archiveId = archiveId
self.shareDrive = shareDrive
self.driveFileManager = driveFileManager
self.urlSession = urlSession
self.publicShareProxy = publicShareProxy
}

// MARK: - Public methods
Expand Down Expand Up @@ -112,34 +110,7 @@ public class DownloadArchiveOperation: Operation {
}

override public func main() {
guard let publicShareProxy else {
authenticatedDownload()
return
}

publicShareDownload(proxy: publicShareProxy)
}

func publicShareDownload(proxy: PublicShareProxy) {
DDLogInfo(
"[DownloadOperation] Downloading Archive of public share files \(archiveId) with session \(urlSession.identifier)"
)

let url = Endpoint.downloadPublicShareArchive(
drive: shareDrive,
linkUuid: proxy.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()
authenticatedDownload()
}

func authenticatedDownload() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Infomaniak kDrive - iOS App
Copyright (C) 2024 Infomaniak Network SA

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import CocoaLumberjackSwift
import FileProvider
import Foundation
import InfomaniakCore
import InfomaniakDI

public final class DownloadPublicShareArchiveOperation: DownloadArchiveOperation {
private let publicShareProxy: PublicShareProxy

public init(archiveId: String,
shareDrive: AbstractDrive,
driveFileManager: DriveFileManager,
urlSession: FileDownloadSession,
publicShareProxy: PublicShareProxy) {
self.publicShareProxy = publicShareProxy
super.init(archiveId: archiveId, shareDrive: shareDrive, driveFileManager: driveFileManager, urlSession: urlSession)
}

override public init(archiveId: String,
shareDrive: AbstractDrive,
driveFileManager: DriveFileManager,
urlSession: FileDownloadSession) {
fatalError("Unavailable")
}

override public func main() {
publicShareDownload()
}

func publicShareDownload() {
DDLogInfo(
"[DownloadPublicShareArchiveOperation] Downloading Archive of public share files \(archiveId) with session \(urlSession.identifier)"
)

let url = Endpoint.downloadPublicShareArchive(
drive: shareDrive,
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()
}
}
2 changes: 1 addition & 1 deletion kDriveCore/Data/DownloadQueue/DownloadQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public final class DownloadQueue: ParallelismHeuristicDelegate {
dispatchQueue.async {
OperationQueueHelper.disableIdleTimer(true)

let operation = DownloadArchiveOperation(
let operation = DownloadPublicShareArchiveOperation(
archiveId: archiveId,
shareDrive: publicShareProxy.proxyDrive,
driveFileManager: driveFileManager,
Expand Down

0 comments on commit 3ff747c

Please sign in to comment.