This PR contains the work done to create a new *Hummingbird* project with very basic configuration from the _colibri_ executable, just like the project you could create with the [Hummingbird template](https://github.com/hummingbird-project/template) project in Github. Reviewed-on: #3 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
80 lines
2.1 KiB
Swift
80 lines
2.1 KiB
Swift
import Foundation
|
|
|
|
public struct FileService: FileServicing {
|
|
|
|
// MARK: Properties
|
|
|
|
private let fileManager: FileManager
|
|
|
|
// MARK: Initialisers
|
|
|
|
public init(fileManager: FileManager = .default) {
|
|
self.fileManager = fileManager
|
|
}
|
|
|
|
// MARK: Computed
|
|
|
|
public var currentFolder: URL {
|
|
get async {
|
|
.init(at: fileManager.currentDirectoryPath)
|
|
}
|
|
}
|
|
|
|
// MARK: Functions
|
|
|
|
public func copyFile(from source: URL, to destination: URL) async throws (FileServiceError) {
|
|
guard try await !isItemExists(at: destination) else {
|
|
throw FileServiceError.itemAlreadyExists
|
|
}
|
|
|
|
var itemData: Data?
|
|
|
|
do {
|
|
itemData = try Data(contentsOf: source)
|
|
} catch {
|
|
throw FileServiceError.itemEmptyData
|
|
}
|
|
|
|
do {
|
|
try itemData?.write(to: destination, options: .atomic)
|
|
} catch {
|
|
throw FileServiceError.itemNotCopied
|
|
}
|
|
}
|
|
|
|
public func createFolder(at location: URL) async throws (FileServiceError) {
|
|
guard try await !isItemExists(at: location) else {
|
|
throw FileServiceError.itemAlreadyExists
|
|
}
|
|
|
|
do {
|
|
try fileManager.createDirectory(at: location, withIntermediateDirectories: true)
|
|
} catch {
|
|
throw FileServiceError.folderNotCreated
|
|
}
|
|
}
|
|
|
|
public func deleteItem(at location: URL) async throws (FileServiceError) {
|
|
guard try await isItemExists(at: location) else {
|
|
throw FileServiceError.itemNotExists
|
|
}
|
|
|
|
do {
|
|
try fileManager.removeItem(at: location)
|
|
} catch {
|
|
throw FileServiceError.itemNotDeleted
|
|
}
|
|
}
|
|
|
|
public func isItemExists(at location: URL) async throws (FileServiceError) -> Bool {
|
|
guard location.isFileURL else {
|
|
throw FileServiceError.itemNotFileURL
|
|
}
|
|
|
|
let filePath = location.pathString
|
|
|
|
return fileManager.fileExists(atPath: filePath)
|
|
}
|
|
|
|
}
|