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

This commit is contained in:
2025-01-11 04:44:02 +01:00
parent 7d0ad3461a
commit 58151a4e5a
3 changed files with 44 additions and 2 deletions
@@ -8,6 +8,7 @@ public protocol FileServicing {
// MARK: Functions
func delete(at url: URL) async throws (FileServiceError)
func exists(at url: URL) async throws (FileServiceError) -> Bool
}
@@ -16,8 +17,8 @@ public protocol FileServicing {
public enum FileServiceError: Error, Equatable {
case folderNotCreated
case folderNotDeleted
case urlAlreadyExists
case urlNotDeleted
case urlNotExists
case urlNotFileURL
}
@@ -20,6 +20,20 @@ public struct FileService: FileServicing {
}
}
// MARK: Functions
public func delete(at url: URL) async throws (FileServiceError) {
guard try await exists(at: url) else {
throw FileServiceError.urlNotExists
}
do {
try fileManager.removeItem(at: url)
} catch {
throw FileServiceError.urlNotDeleted
}
}
public func exists(at url: URL) async throws (FileServiceError) -> Bool {
guard url.isFileURL else {
throw FileServiceError.urlNotFileURL