Implemented the "exists(at: )" function for the FileService service in the module target.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user