2025-01-11 18:40:09 +01:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
public struct CreateRootFolderTask {
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
private let fileService: FileServicing
|
|
|
|
|
|
|
|
// MARK: Initialisers
|
|
|
|
|
|
|
|
public init(fileService: FileServicing) {
|
|
|
|
self.fileService = fileService
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
public func callAsFunction(
|
|
|
|
name: String,
|
|
|
|
at location: URL? = nil
|
|
|
|
) async throws -> URL {
|
2025-01-12 02:16:30 +01:00
|
|
|
guard !name.isEmpty else {
|
|
|
|
throw CreateRootFolderError.nameIsEmpty
|
|
|
|
}
|
|
|
|
|
2025-01-11 18:40:09 +01:00
|
|
|
let rootFolder = if let location {
|
|
|
|
location
|
|
|
|
} else {
|
|
|
|
await fileService.currentFolder
|
|
|
|
}
|
2025-01-12 21:25:51 +01:00
|
|
|
|
|
|
|
let newFolder = rootFolder.appendingPath(name)
|
2025-01-11 18:40:09 +01:00
|
|
|
|
|
|
|
try await fileService.createFolder(at: newFolder)
|
|
|
|
|
|
|
|
return newFolder
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2025-01-12 02:16:30 +01:00
|
|
|
|
|
|
|
// MARK: - Errors
|
|
|
|
|
|
|
|
public enum CreateRootFolderError: Error {
|
|
|
|
case nameIsEmpty
|
|
|
|
}
|