This commit is contained in:
fmodf 2024-07-12 13:54:40 +02:00
parent 0ede68e39a
commit 50fba234b0
3 changed files with 29 additions and 25 deletions

View file

@ -0,0 +1,26 @@
import Foundation
final class DownloadManager {
private let urlSession: URLSession
init() {
let configuration = URLSessionConfiguration.default
urlSession = URLSession(configuration: configuration)
}
func download(from url: URL, to localUrl: URL, completion: @escaping (Error?) -> Void) {
let task = urlSession.downloadTask(with: url) { tempLocalUrl, _, error in
if let tempLocalUrl = tempLocalUrl, error == nil {
do {
try FileManager.default.copyItem(at: tempLocalUrl, to: localUrl)
completion(nil)
} catch let writeError {
completion(writeError)
}
} else {
completion(error)
}
}
task.resume()
}
}

View file

@ -0,0 +1,3 @@
import Foundation
final class FileManager {}

View file

@ -69,28 +69,3 @@ private extension FileMiddleware {
return documentsURL.appendingPathComponent(Const.fileFolder)
}
}
private final class DownloadManager {
private let urlSession: URLSession
init() {
let configuration = URLSessionConfiguration.default
urlSession = URLSession(configuration: configuration)
}
func download(from url: URL, to localUrl: URL, completion: @escaping (Error?) -> Void) {
let task = urlSession.downloadTask(with: url) { tempLocalUrl, _, error in
if let tempLocalUrl = tempLocalUrl, error == nil {
do {
try FileManager.default.copyItem(at: tempLocalUrl, to: localUrl)
completion(nil)
} catch let writeError {
completion(writeError)
}
} else {
completion(error)
}
}
task.resume()
}
}