Implemented the "exists(at: )" function for the FileService service in the module target.

This commit is contained in:
2025-01-11 03:24:22 +01:00
parent 739fe0c8de
commit 7d0ad3461a
3 changed files with 98 additions and 15 deletions
+16 -2
View File
@@ -1,9 +1,23 @@
import Foundation
protocol FileServicing {
public protocol FileServicing {
// MARK: Properties
// MARK: Computed
var currentFolder: URL { get async }
// MARK: Functions
func exists(at url: URL) async throws (FileServiceError) -> Bool
}
// MARK: - Errors
public enum FileServiceError: Error, Equatable {
case folderNotCreated
case folderNotDeleted
case urlAlreadyExists
case urlNotExists
case urlNotFileURL
}
+30 -8
View File
@@ -1,6 +1,6 @@
import Foundation
struct FileService: FileServicing {
public struct FileService: FileServicing {
// MARK: Properties
@@ -8,19 +8,41 @@ struct FileService: FileServicing {
// MARK: Initialisers
init(fileManager: FileManager = .default) {
public init(fileManager: FileManager = .default) {
self.fileManager = fileManager
}
// MARK: Computed
var currentFolder: URL {
public var currentFolder: URL {
get async {
if #available(macOS 13.0, *) {
.init(filePath: fileManager.currentDirectoryPath)
} else {
.init(fileURLWithPath: fileManager.currentDirectoryPath)
}
.init(at: fileManager.currentDirectoryPath)
}
}
public func exists(at url: URL) async throws (FileServiceError) -> Bool {
guard url.isFileURL else {
throw FileServiceError.urlNotFileURL
}
let filePath = getPath(for: url)
return fileManager.fileExists(atPath: filePath)
}
}
// MARK: - Helpers
private extension FileService {
// MARK: Functions
func getPath(for url: URL) -> String {
if #available(macOS 13.0, *) {
return url.path()
} else {
return url.path
}
}