Implemented the UploadFileUseCase use case for the Browse module.
This commit is contained in:
parent
c968982355
commit
b35544b014
41
Modules/Sources/Browse/Logic/Adapters/FileAdapter.swift
Normal file
41
Modules/Sources/Browse/Logic/Adapters/FileAdapter.swift
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
//
|
||||||
|
// FileAdapter.swift
|
||||||
|
// Browse
|
||||||
|
//
|
||||||
|
// Created by Javier Cicchelli on 17/12/2022.
|
||||||
|
// Copyright © 2022 Röck+Cöde. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import APIService
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct FileAdapter {
|
||||||
|
func callAsFunction(url: URL) throws -> File {
|
||||||
|
guard url.isFileURL else {
|
||||||
|
throw FileAdapterError.urlIsNotFileURL
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = try Data(contentsOf: url)
|
||||||
|
let name = try url
|
||||||
|
.resourceValues(forKeys: [.nameKey])
|
||||||
|
.allValues
|
||||||
|
.first(where: { $0.key == .nameKey })
|
||||||
|
.map(\.value)
|
||||||
|
|
||||||
|
guard let name = name as? String else {
|
||||||
|
throw FileAdapterError.nameNotCasted
|
||||||
|
}
|
||||||
|
|
||||||
|
return .init(
|
||||||
|
name: name,
|
||||||
|
data: data
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Errors
|
||||||
|
|
||||||
|
enum FileAdapterError: Error {
|
||||||
|
case urlIsNotFileURL
|
||||||
|
case nameNotCasted
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
//
|
||||||
|
// UploadFileUseCase.swift
|
||||||
|
// Browse
|
||||||
|
//
|
||||||
|
// Created by Javier Cicchelli on 17/12/2022.
|
||||||
|
// Copyright © 2022 Röck+Cöde. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import APIService
|
||||||
|
import DependencyInjection
|
||||||
|
import Dependencies
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct UploadFileUseCase {
|
||||||
|
|
||||||
|
// MARK: Properties
|
||||||
|
|
||||||
|
let apiService: APIService
|
||||||
|
|
||||||
|
private let fileAdapter: FileAdapter = .init()
|
||||||
|
|
||||||
|
// MARK: Functions
|
||||||
|
|
||||||
|
func callAsFunction(
|
||||||
|
id: String,
|
||||||
|
url: URL,
|
||||||
|
username: String,
|
||||||
|
password: String
|
||||||
|
) async throws {
|
||||||
|
_ = try await apiService.uploadFile(
|
||||||
|
id: id,
|
||||||
|
file: fileAdapter(url: url),
|
||||||
|
credentials: .init(
|
||||||
|
username: username,
|
||||||
|
password: password
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Initialisers
|
||||||
|
|
||||||
|
extension UploadFileUseCase {
|
||||||
|
init() {
|
||||||
|
@Dependency(\.apiService) var apiService
|
||||||
|
|
||||||
|
self.init(apiService: apiService)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user