Implemented the UploadFileUseCase use case for the Browse module.

This commit is contained in:
Javier Cicchelli 2022-12-17 11:56:41 +01:00
parent c968982355
commit b35544b014
2 changed files with 91 additions and 0 deletions

View 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
}

View File

@ -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)
}
}