Просмотр исходного кода

Modify ImageDownloaderDelegate to accept URLReponse

Changed `imageDownloader(_:didDownload:for:)` to `imageDownloader(_:didDownload:with:)` accepting the returned URLResponse from the request.

This allows modifying the image data (like decrypting) based on the response the server sends (for example a specific header)
Faizan Durrani 4 лет назад
Родитель
Сommit
017397124c

+ 2 - 2
Sources/Networking/ImageDownloader.swift

@@ -194,10 +194,10 @@ open class ImageDownloader {
             }
             }
         }
         }
         sessionDelegate.onDidDownloadData.delegate(on: self) { (self, task) in
         sessionDelegate.onDidDownloadData.delegate(on: self) { (self, task) in
-            guard let url = task.originalURL else {
+            guard let response = task.task.response else {
                 return task.mutableData
                 return task.mutableData
             }
             }
-            return (self.delegate ?? self).imageDownloader(self, didDownload: task.mutableData, for: url)
+            return (self.delegate ?? self).imageDownloader(self, didDownload: task.mutableData, with: response)
         }
         }
     }
     }
 
 

+ 2 - 2
Sources/Networking/ImageDownloaderDelegate.swift

@@ -69,7 +69,7 @@ public protocol ImageDownloaderDelegate: AnyObject {
     ///   decrypting or verification). If `nil` returned, the processing is interrupted and a `KingfisherError` with
     ///   decrypting or verification). If `nil` returned, the processing is interrupted and a `KingfisherError` with
     ///   `ResponseErrorReason.dataModifyingFailed` will be raised. You could use this fact to stop the image
     ///   `ResponseErrorReason.dataModifyingFailed` will be raised. You could use this fact to stop the image
     ///   processing flow if you find the data is corrupted or malformed.
     ///   processing flow if you find the data is corrupted or malformed.
-    func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data?
+    func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, with response: URLResponse) -> Data?
 
 
     /// Called when the `ImageDownloader` object successfully downloads and processes an image from specified URL.
     /// Called when the `ImageDownloader` object successfully downloads and processes an image from specified URL.
     ///
     ///
@@ -121,7 +121,7 @@ extension ImageDownloaderDelegate {
     public func isValidStatusCode(_ code: Int, for downloader: ImageDownloader) -> Bool {
     public func isValidStatusCode(_ code: Int, for downloader: ImageDownloader) -> Bool {
         return (200..<400).contains(code)
         return (200..<400).contains(code)
     }
     }
-    public func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data? {
+    public func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, with response: URLResponse) -> Data? {
         return data
         return data
     }
     }
 }
 }