Implemented the "copyItem(from: to: )" function for the FileService service in the library target.
This commit is contained in:
@@ -8,6 +8,7 @@ public protocol FileServicing {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func copyItem(from source: URL, to destination: URL) async throws (FileServiceError)
|
||||
func createFolder(at location: URL) async throws (FileServiceError)
|
||||
func deleteItem(at location: URL) async throws (FileServiceError)
|
||||
func isItemExists(at location: URL) async throws (FileServiceError) -> Bool
|
||||
@@ -18,6 +19,7 @@ public protocol FileServicing {
|
||||
|
||||
public enum FileServiceError: Error, Equatable {
|
||||
case folderNotCreated
|
||||
case itemNotCopied
|
||||
case itemAlreadyExists
|
||||
case itemNotDeleted
|
||||
case itemNotExists
|
||||
|
||||
@@ -21,6 +21,21 @@ public struct FileService: FileServicing {
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
public func copyItem(from source: URL, to destination: URL) async throws (FileServiceError) {
|
||||
guard try await isItemExists(at: source) else {
|
||||
throw FileServiceError.itemNotExists
|
||||
}
|
||||
guard try await !isItemExists(at: destination) else {
|
||||
throw FileServiceError.itemAlreadyExists
|
||||
}
|
||||
|
||||
do {
|
||||
try fileManager.copyItem(at: source, to: destination)
|
||||
} catch {
|
||||
throw FileServiceError.itemNotCopied
|
||||
}
|
||||
}
|
||||
|
||||
public func createFolder(at location: URL) async throws (FileServiceError) {
|
||||
guard try await !isItemExists(at: location) else {
|
||||
|
||||
Reference in New Issue
Block a user