Skip to content

Commit

Permalink
feat: Authenticating add to kDrive call.
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-coye committed Dec 10, 2024
1 parent ca518b7 commit bf43649
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 46 deletions.
3 changes: 3 additions & 0 deletions kDrive/AppRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,9 @@ public struct AppRouter: AppNavigable {
rootViewController.dismiss(animated: false) {
guard !frozenRootFolder.isDisabled else {
let noDriveViewController = NoDriveUpsaleViewController()
noDriveViewController.dismissCallback = {
rootViewController.dismiss(animated: false) {}
}
let floatingPanel = UpsaleFloatingPanelController(upsaleViewController: noDriveViewController)
rootViewController.present(floatingPanel, animated: true, completion: nil)
return
Expand Down
20 changes: 20 additions & 0 deletions kDrive/UI/Controller/Files/File List/FileListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,28 @@ class FileListViewController: UICollectionViewController, SwipeActionCollectionV
}

guard accountManager.currentAccount != nil else {
#if !ISEXTENSION
let upsaleViewController = UpsaleViewController()

// Create an account
upsaleViewController.freeTrialCallback = { [weak self] in
self?.dismiss(animated: true)
// TODO: Present login
// MatomoUtils.track(eventWithCategory: .account, name: "openCreationWebview")
// present(RegisterViewController.instantiateInNavigationController(delegate: self), animated: true)
}

// Let the user login with the onboarding
upsaleViewController.loginCallback = { [weak self] in
self?.dismiss(animated: true)
}

let floatingPanel = UpsaleFloatingPanelController(upsaleViewController: upsaleViewController)
present(floatingPanel, animated: true, completion: nil)
#else
dismiss(animated: true)
#endif

return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ extension SaveFileViewController: FooterButtonDelegate {

if let publicShareProxy {
Task {
await savePublicShareToDrive(sourceDriveId: publicShareProxy.driveId,
destinationDriveId: drive.accountId,
fileIds: publicShareFileIds,
exceptIds: publicShareExceptIds)
try await savePublicShareToDrive(sourceDriveId: publicShareProxy.driveId,
destinationDriveId: drive.accountId,
fileIds: publicShareFileIds,
exceptIds: publicShareExceptIds)
}
} else {
guard !items.isEmpty else {
Expand All @@ -63,11 +63,14 @@ extension SaveFileViewController: FooterButtonDelegate {
dismiss(animated: true)
}

try? await PublicShareApiFetcher().importShareLinkFiles(sourceDriveId: sourceDriveId,
destinationDriveId: destinationDriveId,
fileIds: fileIds,
exceptIds: exceptIds)
print("savePublicShareToDrive")
guard let currentDriveFileManager = accountManager.currentDriveFileManager else {
return
}

try await currentDriveFileManager.apiFetcher.importShareLinkFiles(sourceDriveId: sourceDriveId,
destinationDriveId: destinationDriveId,
fileIds: fileIds,
exceptIds: exceptIds)
}

private func saveAndDismiss(files: [ImportedFile], directory: File, drive: Drive) async {
Expand Down
3 changes: 3 additions & 0 deletions kDrive/UI/View/Upsale/NoDriveUpsaleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import kDriveResources
import UIKit

public class NoDriveUpsaleViewController: UpsaleViewController {
var dismissCallback: (() -> Void)?

override func configureButtons() {
dismissButton.style = .primaryButton
freeTrialButton.setTitle(KDriveStrings.Localizable.obtainkDriveAdFreeTrialButton, for: .normal)
Expand All @@ -39,5 +41,6 @@ public class NoDriveUpsaleViewController: UpsaleViewController {

@objc public func dismissViewController() {
dismiss(animated: true, completion: nil)
dismissCallback?()
}
}
7 changes: 5 additions & 2 deletions kDrive/UI/View/Upsale/UpsaleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import kDriveResources
import UIKit

public class UpsaleViewController: UIViewController {
var loginCallback: (() -> Void)?
var freeTrialCallback: (() -> Void)?

let titleImageView = UIImageView()

let titleLabel: UILabel = {
Expand Down Expand Up @@ -217,12 +220,12 @@ public class UpsaleViewController: UIViewController {
}

@objc public func freeTrial() {
// TODO: Hook free trial
dismiss(animated: true, completion: nil)
freeTrialCallback?()
}

@objc public func login() {
// TODO: Hook login
dismiss(animated: true, completion: nil)
loginCallback?()
}
}
36 changes: 36 additions & 0 deletions kDriveCore/Data/Api/DriveApiFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,42 @@ public class DriveApiFetcher: ApiFetcher {
public func file(_ file: AbstractFile) async throws -> File {
try await perform(request: authenticatedRequest(.file(file)))
}

public func importShareLinkFiles(sourceDriveId: Int,
destinationDriveId: Int,
fileIds: [Int]?,
exceptIds: [Int]?,
password: String? = nil) async throws -> ValidServerResponse<FileExternalImport> {
let destinationDrive = ProxyDrive(id: destinationDriveId)
let importShareLinkFiles = Endpoint.importShareLinkFiles(destinationDrive: destinationDrive)
var requestParameters: Parameters = [
APIPublicShareParameter.sourceDriveId: sourceDriveId
]

if let fileIds, !fileIds.isEmpty {
requestParameters[APIPublicShareParameter.fileIds] = fileIds
} else if let exceptIds, !exceptIds.isEmpty {
requestParameters[APIPublicShareParameter.exceptFileIds] = exceptIds
}

if let password {
requestParameters[APIPublicShareParameter.password] = password
}

let result: ValidServerResponse<FileExternalImport> = try await perform(request: authenticatedRequest(
importShareLinkFiles,
method: .post,
parameters: requestParameters
))
return result
}
}

enum APIPublicShareParameter {
static let sourceDriveId = "source_drive_id"
static let fileIds = "file_ids"
static let exceptFileIds = "except_file_ids"
static let password = "password"
}

class SyncedAuthenticator: OAuthAuthenticator {
Expand Down
6 changes: 3 additions & 3 deletions kDriveCore/Data/Api/Endpoint+Share.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public extension Endpoint {
return Self.shareUrlV1.appending(path: "/share/\(driveId)/\(linkUuid)/preview/text/\(fileId)")
}

/// Add to my Drive
static func importShareLinkFiles(driveId: Int) -> Endpoint {
return Endpoint(hostKeypath: \.driveHost, path: "/2/drive").appending(path: "/\(driveId)/imports/sharelink")
/// Add Public Share to my Drive
static func importShareLinkFiles(destinationDrive: AbstractDrive) -> Endpoint {
return Endpoint.driveInfoV2(drive: destinationDrive).appending(path: "/imports/sharelink")
}
}
32 changes: 0 additions & 32 deletions kDriveCore/Data/Api/PublicShareApiFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ public enum PublicShareLimitation: String {
case expired = "link_is_not_valid"
}

enum APIPublicShareParameter {
static let sourceDriveId = "source_drive_id"
static let fileIds = "file_ids"
static let exceptFileIds = "except_file_ids"
static let password = "password"
}

public class PublicShareApiFetcher: ApiFetcher {
override public init() {
super.init()
Expand Down Expand Up @@ -97,29 +90,4 @@ public extension PublicShareApiFetcher {
let shareLinkFiles: ValidServerResponse<[File]> = try await perform(request: request)
return shareLinkFiles
}

func importShareLinkFiles(sourceDriveId: Int,
destinationDriveId: Int,
fileIds: [Int]?,
exceptIds: [Int]?,
password: String? = nil) async throws -> ValidServerResponse<FileExternalImport> {
let importShareLinkFilesUrl = Endpoint.importShareLinkFiles(driveId: destinationDriveId).url
var requestParameters: [String: AnyHashable] = [
APIPublicShareParameter.sourceDriveId: sourceDriveId
]

if let fileIds, !fileIds.isEmpty {
requestParameters[APIPublicShareParameter.fileIds] = fileIds
} else if let exceptIds, !exceptIds.isEmpty {
requestParameters[APIPublicShareParameter.exceptFileIds] = exceptIds
}

if let password {
requestParameters[APIPublicShareParameter.password] = password
}

let request = Session.default.request(importShareLinkFilesUrl)
let externalImport: ValidServerResponse<FileExternalImport> = try await perform(request: request)
return externalImport
}
}

0 comments on commit bf43649

Please sign in to comment.