2017-11-26 05:12:53 +01:00
|
|
|
//
|
|
|
|
// ImageDownloader.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2017-11-26 05:12:53 +01:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/25/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2019-04-12 00:53:03 +02:00
|
|
|
import Foundation
|
2017-11-26 05:12:53 +01:00
|
|
|
import RSCore
|
|
|
|
import RSWeb
|
|
|
|
|
|
|
|
extension Notification.Name {
|
|
|
|
|
2018-01-05 22:22:16 +01:00
|
|
|
static let ImageDidBecomeAvailable = Notification.Name("ImageDidBecomeAvailableNotification") // UserInfoKey.url
|
2017-11-26 05:12:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
final class ImageDownloader {
|
|
|
|
|
|
|
|
private let folder: String
|
|
|
|
private var diskCache: BinaryDiskCache
|
|
|
|
private let queue: DispatchQueue
|
2019-04-12 00:53:03 +02:00
|
|
|
private var imageCache = [String: Data]() // url: image
|
2017-11-26 05:24:38 +01:00
|
|
|
private var urlsInProgress = Set<String>()
|
|
|
|
private var badURLs = Set<String>() // That return a 404 or whatever. Just skip them in the future.
|
|
|
|
|
2017-11-26 05:12:53 +01:00
|
|
|
init(folder: String) {
|
|
|
|
|
|
|
|
self.folder = folder
|
|
|
|
self.diskCache = BinaryDiskCache(folder: folder)
|
|
|
|
self.queue = DispatchQueue(label: "ImageDownloader serial queue - \(folder)")
|
|
|
|
}
|
|
|
|
|
2017-11-26 05:24:38 +01:00
|
|
|
@discardableResult
|
2019-04-12 00:53:03 +02:00
|
|
|
func image(for url: String) -> Data? {
|
2017-11-26 05:12:53 +01:00
|
|
|
|
2019-04-12 00:53:03 +02:00
|
|
|
if let data = imageCache[url] {
|
|
|
|
return data
|
2017-11-26 05:12:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
findImage(url)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ImageDownloader {
|
|
|
|
|
2019-04-12 00:53:03 +02:00
|
|
|
func cacheImage(_ url: String, _ image: Data) {
|
2017-11-26 05:12:53 +01:00
|
|
|
|
|
|
|
imageCache[url] = image
|
|
|
|
postImageDidBecomeAvailableNotification(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func findImage(_ url: String) {
|
|
|
|
|
2017-11-26 05:24:38 +01:00
|
|
|
guard !urlsInProgress.contains(url) && !badURLs.contains(url) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
urlsInProgress.insert(url)
|
|
|
|
|
2017-11-26 05:12:53 +01:00
|
|
|
readFromDisk(url) { (image) in
|
|
|
|
|
|
|
|
if let image = image {
|
|
|
|
self.cacheImage(url, image)
|
2017-11-26 05:24:38 +01:00
|
|
|
self.urlsInProgress.remove(url)
|
2017-11-26 05:12:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.downloadImage(url) { (image) in
|
|
|
|
|
|
|
|
if let image = image {
|
|
|
|
self.cacheImage(url, image)
|
|
|
|
}
|
2017-11-26 05:24:38 +01:00
|
|
|
self.urlsInProgress.remove(url)
|
2017-11-26 05:12:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 02:01:34 +01:00
|
|
|
func readFromDisk(_ url: String, _ completion: @escaping (Data?) -> Void) {
|
2017-11-26 05:12:53 +01:00
|
|
|
|
|
|
|
queue.async {
|
|
|
|
|
|
|
|
if let data = self.diskCache[self.diskKey(url)], !data.isEmpty {
|
2019-04-12 00:53:03 +02:00
|
|
|
DispatchQueue.main.async {
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(data)
|
2019-04-12 00:53:03 +02:00
|
|
|
}
|
2017-11-26 05:12:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(nil)
|
2017-11-26 05:12:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 02:01:34 +01:00
|
|
|
func downloadImage(_ url: String, _ completion: @escaping (Data?) -> Void) {
|
2017-11-26 05:12:53 +01:00
|
|
|
|
2017-11-26 05:24:38 +01:00
|
|
|
guard let imageURL = URL(string: url) else {
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(nil)
|
2017-11-26 05:12:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-26 05:24:38 +01:00
|
|
|
downloadUsingCache(imageURL) { (data, response, error) in
|
2017-11-26 05:12:53 +01:00
|
|
|
|
|
|
|
if let data = data, !data.isEmpty, let response = response, response.statusIsOK, error == nil {
|
2017-11-26 05:24:38 +01:00
|
|
|
self.saveToDisk(url, data)
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(data)
|
2017-11-26 05:12:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-26 05:24:38 +01:00
|
|
|
if let response = response as? HTTPURLResponse, response.statusCode >= HTTPResponseCode.badRequest && response.statusCode <= HTTPResponseCode.notAcceptable {
|
|
|
|
self.badURLs.insert(url)
|
|
|
|
}
|
2017-11-26 05:12:53 +01:00
|
|
|
if let error = error {
|
|
|
|
appDelegate.logMessage("Error downloading image at \(url): \(error)", type: .warning)
|
|
|
|
}
|
|
|
|
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(nil)
|
2017-11-26 05:12:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveToDisk(_ url: String, _ data: Data) {
|
|
|
|
|
|
|
|
queue.async {
|
|
|
|
self.diskCache[self.diskKey(url)] = data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func diskKey(_ url: String) -> String {
|
|
|
|
|
|
|
|
return (url as NSString).rs_md5Hash()
|
|
|
|
}
|
|
|
|
|
|
|
|
func postImageDidBecomeAvailableNotification(_ url: String) {
|
|
|
|
|
2018-01-09 06:34:39 +01:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
NotificationCenter.default.post(name: .ImageDidBecomeAvailable, object: self, userInfo: [UserInfoKey.url: url])
|
|
|
|
}
|
2017-11-26 05:12:53 +01:00
|
|
|
}
|
|
|
|
}
|