소스 검색

Merge pull request #1676 from FaizanDurrani/master

Modify ImageDownloaderDelegate to accept URLReponse
Wei Wang 4 년 전
부모
커밋
c74594b8a9
2개의 변경된 파일12개의 추가작업 그리고 3개의 파일을 삭제
  1. 2 2
      Sources/Networking/ImageDownloader.swift
  2. 10 1
      Sources/Networking/ImageDownloaderDelegate.swift

+ 2 - 2
Sources/Networking/ImageDownloader.swift

@@ -194,10 +194,10 @@ open class ImageDownloader {
             }
         }
         sessionDelegate.onDidDownloadData.delegate(on: self) { (self, task) in
-            guard let url = task.originalURL else {
+            guard task.originalURL != nil, let response = task.task.response else {
                 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)
         }
     }
 

+ 10 - 1
Sources/Networking/ImageDownloaderDelegate.swift

@@ -69,6 +69,9 @@ public protocol ImageDownloaderDelegate: AnyObject {
     ///   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
     ///   processing flow if you find the data is corrupted or malformed.
+    func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, with response: URLResponse) -> Data?
+  
+    @available(*, deprecated, message: "use `imageDownloader(_:didDownload:with:)` instead")
     func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data?
 
     /// Called when the `ImageDownloader` object successfully downloads and processes an image from specified URL.
@@ -121,7 +124,13 @@ extension ImageDownloaderDelegate {
     public func isValidStatusCode(_ code: Int, for downloader: ImageDownloader) -> Bool {
         return (200..<400).contains(code)
     }
+  
+    public func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, with response: URLResponse) -> Data? {
+      guard let url = response.url else { return data }
+      return imageDownloader(downloader, didDownload: data, for: url)
+    }
+  
     public func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data? {
-        return data
+      return data
     }
 }