Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify ImageDownloaderDelegate to accept URLReponse #1676

Merged
merged 2 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Networking/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
11 changes: 10 additions & 1 deletion Sources/Networking/ImageDownloaderDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}