another.im-ios/ConversationsClassic/AppCore/Files/DownloadManager.swift

27 lines
822 B
Swift
Raw Normal View History

2024-07-12 11:54:40 +00:00
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()
}
}