Implemented the "copyFile(from: to: )" function for the FileService service in the library target.

This commit is contained in:
2025-01-18 01:07:52 +01:00
parent af4958a5e8
commit b8d5bea7ae
5 changed files with 26 additions and 17 deletions
@@ -8,7 +8,7 @@ public protocol FileServicing {
// MARK: Functions
func copyItem(from source: URL, to destination: URL) async throws (FileServiceError)
func copyFile(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
@@ -19,8 +19,9 @@ public protocol FileServicing {
public enum FileServiceError: Error, Equatable {
case folderNotCreated
case itemNotCopied
case itemAlreadyExists
case itemEmptyData
case itemNotCopied
case itemNotDeleted
case itemNotExists
case itemNotFileURL
+10 -2
View File
@@ -22,7 +22,7 @@ public struct FileService: FileServicing {
// MARK: Functions
public func copyItem(from source: URL, to destination: URL) async throws (FileServiceError) {
public func copyFile(from source: URL, to destination: URL) async throws (FileServiceError) {
guard try await isItemExists(at: source) else {
throw FileServiceError.itemNotExists
}
@@ -30,8 +30,16 @@ public struct FileService: FileServicing {
throw FileServiceError.itemAlreadyExists
}
var itemData: Data?
do {
try fileManager.copyItem(at: source, to: destination)
itemData = try Data(contentsOf: source)
} catch {
throw FileServiceError.itemEmptyData
}
do {
try itemData?.write(to: destination, options: .atomic)
} catch {
throw FileServiceError.itemNotCopied
}