| 1 |
efrain |
1 |
//
|
|
|
2 |
// ImageLoader.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 3/9/22.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
import Combine
|
|
|
9 |
import UIKit
|
|
|
10 |
import var CommonCrypto.CC_MD5_DIGEST_LENGTH
|
|
|
11 |
import func CommonCrypto.CC_MD5
|
|
|
12 |
import typealias CommonCrypto.CC_LONG
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
class ImageLoader: ObservableObject {
|
|
|
16 |
@Published var image: UIImage?
|
|
|
17 |
|
| 9 |
efrain |
18 |
private let appDao = AppDao.sharedInstance
|
| 1 |
efrain |
19 |
private(set) var isLoading = false
|
|
|
20 |
|
|
|
21 |
private let url: URL
|
|
|
22 |
private var cache: ImageCache?
|
|
|
23 |
//private var cancellable: AnyCancellable?
|
|
|
24 |
|
|
|
25 |
//private static let imageProcessingQueue = DispatchQueue(label: "image-processing")
|
|
|
26 |
|
|
|
27 |
init(url: URL, cache: ImageCache? = nil) {
|
|
|
28 |
self.url = url
|
|
|
29 |
self.cache = cache
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
/*
|
|
|
33 |
deinit {
|
|
|
34 |
cancel()
|
|
|
35 |
}
|
|
|
36 |
*/
|
|
|
37 |
|
|
|
38 |
func load() {
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
let urlString : String = url.absoluteString
|
|
|
42 |
|
|
|
43 |
print("ImageLoader URL : \(urlString) ")
|
|
|
44 |
|
|
|
45 |
//guard !isLoading else { return }
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
let md5Data = MD5(string: urlString)
|
|
|
49 |
let key = md5Data.map {
|
|
|
50 |
String(format: "%02hhx", $0)
|
|
|
51 |
|
|
|
52 |
}.joined()
|
|
|
53 |
|
|
|
54 |
if let image = cache?[key as NSString] {
|
|
|
55 |
print("ImageLoader in Cache")
|
|
|
56 |
self.image = image
|
|
|
57 |
return
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
print("ImageLoader not in Cache")
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
var request = URLRequest(url: url)
|
|
|
64 |
|
|
|
65 |
let headerSecurity = HeaderSecurity()
|
|
|
66 |
|
|
|
67 |
request.httpMethod = "GET"
|
|
|
68 |
request.addValue(appData.deviceUuid, forHTTPHeaderField: Constants.HTTP_HEADER_SECURITY_TOKEN)
|
|
|
69 |
request.addValue(headerSecurity.secret, forHTTPHeaderField: Constants.HTTP_HEADER_SECURITY_SECRET)
|
|
|
70 |
request.addValue(String(headerSecurity.created), forHTTPHeaderField: Constants.HTTP_HEADER_SECURITY_CREATED)
|
|
|
71 |
request.addValue(String(headerSecurity.rand), forHTTPHeaderField: Constants.HTTP_HEADER_SECURITY_RAND)
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
print("ImageLoader Header \(Constants.HTTP_HEADER_SECURITY_TOKEN) = \(appData.deviceUuid) ")
|
|
|
75 |
print("ImageLoader Header \( Constants.HTTP_HEADER_SECURITY_SECRET) = \(headerSecurity.secret) ")
|
|
|
76 |
print("ImageLoader Header \(Constants.HTTP_HEADER_SECURITY_CREATED) = \(headerSecurity.created) ")
|
|
|
77 |
print("ImageLoader Header \(Constants.HTTP_HEADER_SECURITY_RAND) = \(headerSecurity.rand) ")
|
|
|
78 |
|
|
|
79 |
/*
|
|
|
80 |
cancellable = URLSession.shared.dataTaskPublisher(for: request)
|
|
|
81 |
.map { UIImage(data: $0.data) }
|
|
|
82 |
.replaceError(with: nil)
|
|
|
83 |
.handleEvents(receiveSubscription: { [weak self] _ in self?.onStart() },
|
|
|
84 |
receiveOutput: { [weak self] in self?.cache($0) },
|
|
|
85 |
receiveCompletion: { [weak self] _ in self?.onFinish() },
|
|
|
86 |
receiveCancel: { [weak self] in self?.onFinish() })
|
|
|
87 |
.subscribe(on: Self.imageProcessingQueue)
|
|
|
88 |
.receive(on: DispatchQueue.main)
|
|
|
89 |
.sink { [weak self] in self?.image = $0 }*/
|
|
|
90 |
|
|
|
91 |
let task = URLSession.shared.dataTask(with: request, completionHandler: setImageFromData)
|
|
|
92 |
task.resume()
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
private func setImageFromData(data: Data?, urlResponse: URLResponse?, error: Error?) {
|
|
|
96 |
guard error == nil else {
|
|
|
97 |
print("\(error!)")
|
|
|
98 |
return
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
guard let content = data else {
|
|
|
102 |
print("No data")
|
|
|
103 |
return
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
DispatchQueue.main.async {
|
|
|
107 |
self.image = UIImage(data: content)
|
|
|
108 |
// self.dataHasLoaded = true
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/*
|
|
|
113 |
func cancel() {
|
|
|
114 |
cancellable?.cancel()
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
private func onStart() {
|
|
|
118 |
isLoading = true
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
private func onFinish() {
|
|
|
122 |
isLoading = false
|
|
|
123 |
}*/
|
|
|
124 |
|
|
|
125 |
private func cache(_ image: UIImage?) {
|
|
|
126 |
let urlString : String = url.absoluteString
|
|
|
127 |
|
|
|
128 |
print("ImageLoader URL : \(urlString) ")
|
|
|
129 |
|
|
|
130 |
//guard !isLoading else { return }
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
let md5Data = MD5(string: urlString)
|
|
|
134 |
let key = md5Data.map {
|
|
|
135 |
String(format: "%02hhx", $0)
|
|
|
136 |
|
|
|
137 |
}.joined()
|
|
|
138 |
|
|
|
139 |
|
|
|
140 |
image.map { cache?[key as NSString] = $0 }
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
private func MD5(string: String) -> Data {
|
|
|
144 |
let length = Int(CC_MD5_DIGEST_LENGTH)
|
|
|
145 |
let messageData = string.data(using:.utf8)!
|
|
|
146 |
var digestData = Data(count: length)
|
|
|
147 |
|
|
|
148 |
_ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
|
|
|
149 |
messageData.withUnsafeBytes { messageBytes -> UInt8 in
|
|
|
150 |
if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
|
|
|
151 |
let messageLength = CC_LONG(messageData.count)
|
|
|
152 |
CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
|
|
|
153 |
}
|
|
|
154 |
return 0
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
return digestData
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
}
|
|
|
161 |
|