27 lines
822 B
Swift
27 lines
822 B
Swift
|
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()
|
||
|
}
|
||
|
}
|