|
|
@ -164,7 +164,7 @@ extension URLSessionTask {
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Properties
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
|
|
|
|
static let TAG = "[GiphyDownloader]"
|
|
|
|
let TAG = "[GiphyDownloader]"
|
|
|
|
|
|
|
|
|
|
|
|
static let sharedInstance = GiphyDownloader()
|
|
|
|
static let sharedInstance = GiphyDownloader()
|
|
|
|
|
|
|
|
|
|
|
@ -287,7 +287,7 @@ extension URLSessionTask {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
guard let downloadSession = self.giphyDownloadSession() else {
|
|
|
|
guard let downloadSession = self.giphyDownloadSession() else {
|
|
|
|
owsFail("\(GiphyDownloader.TAG) Couldn't create session manager.")
|
|
|
|
owsFail("\(self.TAG) Couldn't create session manager.")
|
|
|
|
self.assetRequestDidFail(assetRequest:assetRequest)
|
|
|
|
self.assetRequestDidFail(assetRequest:assetRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -341,23 +341,23 @@ extension URLSessionTask {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let error = error {
|
|
|
|
if let error = error {
|
|
|
|
Logger.error("\(GiphyDownloader.TAG) download failed with error: \(error)")
|
|
|
|
Logger.error("\(TAG) download failed with error: \(error)")
|
|
|
|
assetRequestDidFail(assetRequest:assetRequest)
|
|
|
|
assetRequestDidFail(assetRequest:assetRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
guard let httpResponse = task.response as? HTTPURLResponse else {
|
|
|
|
guard let httpResponse = task.response as? HTTPURLResponse else {
|
|
|
|
Logger.error("\(GiphyDownloader.TAG) missing or unexpected response: \(task.response)")
|
|
|
|
Logger.error("\(TAG) missing or unexpected response: \(task.response)")
|
|
|
|
assetRequestDidFail(assetRequest:assetRequest)
|
|
|
|
assetRequestDidFail(assetRequest:assetRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let statusCode = httpResponse.statusCode
|
|
|
|
let statusCode = httpResponse.statusCode
|
|
|
|
guard statusCode >= 200 && statusCode < 400 else {
|
|
|
|
guard statusCode >= 200 && statusCode < 400 else {
|
|
|
|
Logger.error("\(GiphyDownloader.TAG) response has invalid status code: \(statusCode)")
|
|
|
|
Logger.error("\(TAG) response has invalid status code: \(statusCode)")
|
|
|
|
assetRequestDidFail(assetRequest:assetRequest)
|
|
|
|
assetRequestDidFail(assetRequest:assetRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
guard let assetFilePath = assetRequest.assetFilePath else {
|
|
|
|
guard let assetFilePath = assetRequest.assetFilePath else {
|
|
|
|
Logger.error("\(GiphyDownloader.TAG) task is missing asset file")
|
|
|
|
Logger.error("\(TAG) task is missing asset file")
|
|
|
|
assetRequestDidFail(assetRequest:assetRequest)
|
|
|
|
assetRequestDidFail(assetRequest:assetRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -390,7 +390,32 @@ extension URLSessionTask {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var animatedDataCount = [URLSessionDownloadTask: Int64]()
|
|
|
|
|
|
|
|
var stillDataCount = [URLSessionDownloadTask: Int64]()
|
|
|
|
|
|
|
|
var totalDataCount = [URLSessionDownloadTask: Int64]()
|
|
|
|
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
|
|
|
|
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Log accumulated data usage in debug
|
|
|
|
|
|
|
|
if _isDebugAssertConfiguration() {
|
|
|
|
|
|
|
|
let assetRequest = downloadTask.assetRequest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
totalDataCount[downloadTask] = totalBytesWritten
|
|
|
|
|
|
|
|
if assetRequest.rendition.isStill {
|
|
|
|
|
|
|
|
stillDataCount[downloadTask] = totalBytesWritten
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
animatedDataCount[downloadTask] = totalBytesWritten
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let megabyteCount = { (dataCountMap: [URLSessionDownloadTask: Int64]) -> String in
|
|
|
|
|
|
|
|
let sum = dataCountMap.values.reduce(0, +)
|
|
|
|
|
|
|
|
let megabyteCount = Float(sum) / 1000 / 1000
|
|
|
|
|
|
|
|
return String(format: "%06.2f MB", megabyteCount)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.info("\(TAG) Still bytes written: \(megabyteCount(stillDataCount))")
|
|
|
|
|
|
|
|
Logger.info("\(TAG) Animated bytes written: \(megabyteCount(animatedDataCount))")
|
|
|
|
|
|
|
|
Logger.info("\(TAG) Total bytes written: \(megabyteCount(totalDataCount))")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let assetRequest = downloadTask.assetRequest
|
|
|
|
let assetRequest = downloadTask.assetRequest
|
|
|
|
guard !assetRequest.wasCancelled else {
|
|
|
|
guard !assetRequest.wasCancelled else {
|
|
|
|
downloadTask.cancel()
|
|
|
|
downloadTask.cancel()
|
|
|
|