wip
This commit is contained in:
parent
0ede68e39a
commit
50fba234b0
26
ConversationsClassic/AppCore/Files/DownloadManager.swift
Normal file
26
ConversationsClassic/AppCore/Files/DownloadManager.swift
Normal 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()
|
||||
}
|
||||
}
|
3
ConversationsClassic/AppCore/Files/FileManager.swift
Normal file
3
ConversationsClassic/AppCore/Files/FileManager.swift
Normal file
|
@ -0,0 +1,3 @@
|
|||
import Foundation
|
||||
|
||||
final class FileManager {}
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue