Update API [skip ci]

This commit is contained in:
Ilya Laktyushin
2024-11-08 15:45:48 +01:00
parent 83a7019d0f
commit 85b6a8ffe9
40 changed files with 1686 additions and 841 deletions

View File

@@ -0,0 +1,75 @@
import Foundation
import SwiftSignalKit
final class FileDownload: NSObject, URLSessionDownloadDelegate {
private let fileSize: Int64?
private var urlSession: URLSession!
private var completion: ((URL?, Error?) -> Void)?
private var progressHandler: ((Double) -> Void)?
init(from url: URL, fileSize: Int64?, progressHandler: @escaping (Double) -> Void, completion: @escaping (URL?, Error?) -> Void) {
self.progressHandler = progressHandler
self.fileSize = fileSize
self.completion = completion
super.init()
let configuration = URLSessionConfiguration.default
urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)
let downloadTask = self.urlSession.downloadTask(with: url)
downloadTask.resume()
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
var totalBytesExpectedToWrite = totalBytesExpectedToWrite
if totalBytesExpectedToWrite == -1, let fileSize = self.fileSize {
totalBytesExpectedToWrite = fileSize
}
let progress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
progressHandler?(progress)
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
completion?(location, nil)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let error = error {
completion?(nil, error)
}
}
static func getFileSize(url: String) -> Signal<Int64?, NoError> {
if #available(iOS 13.0, *) {
guard let url = URL(string: url) else {
return .single(nil)
}
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
return Signal { subscriber in
let task = URLSession.shared.dataTask(with: request) { _, response, error in
if let _ = error {
subscriber.putNext(nil)
subscriber.putCompletion()
return
}
var fileSize: Int64?
if let httpResponse = response as? HTTPURLResponse, let contentLength = httpResponse.value(forHTTPHeaderField: "Content-Length"), let size = Int64(contentLength) {
fileSize = size
}
subscriber.putNext(fileSize)
subscriber.putCompletion()
}
task.resume()
return ActionDisposable {
task.cancel()
}
}
} else {
return .single(nil)
}
}
}