Skip to content

Commit

Permalink
Respect user passing nil for downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Sep 28, 2017
1 parent aaa60ef commit c1fae0f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
16 changes: 10 additions & 6 deletions Sources/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ public protocol ImageDownloaderDelegate: class {
- parameter data: Downloaded data.
- parameter url: URL of the original request URL.

- returns: The data from which Kingfisher should use to create an image.

- Note: This callback can be used to preprocess raw image data
before creation of UIImage instance (i.e. decrypting).
before creation of UIImage instance (i.e. decrypting or verification).
*/
func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data?
}
Expand Down Expand Up @@ -551,11 +553,13 @@ class ImageDownloaderSessionHandler: NSObject, URLSessionDataDelegate, Authentic

self.cleanFetchLoad(for: url)

let data: Data
if let modifiedData = downloader.delegate?.imageDownloader(downloader, didDownload: fetchLoad.responseData as Data, for: url) {
data = modifiedData
let data: Data?
let fetchedData = fetchLoad.responseData as Data

if let delegate = downloader.delegate {
data = delegate.imageDownloader(downloader, didDownload: fetchedData, for: url)
} else {
data = fetchLoad.responseData as Data
data = fetchedData
}

// Cache the processed images. So we do not need to re-process the image if using the same processor.
Expand All @@ -570,7 +574,7 @@ class ImageDownloaderSessionHandler: NSObject, URLSessionDataDelegate, Authentic
let processor = options.processor

var image = imageCache[processor.identifier]
if image == nil {
if let data = data, image == nil {
image = processor.process(item: .data(data), options: options)
// Add the processed image to cache.
// If `image` is nil, nothing will happen (since the key is not existing before).
Expand Down
25 changes: 25 additions & 0 deletions Tests/KingfisherTests/ImageDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,31 @@ class ImageDownloaderTests: XCTestCase {

waitForExpectations(timeout: 5, handler: nil)
}

func testDownloadedDataCouldBeModified() {
let expectation = self.expectation(description: "wait for downloading image")

let URLString = testKeys[0]
_ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)

let url = URL(string: URLString)!

downloader.delegate = self
downloader.downloadImage(with: url) { image, error, imageURL, data in
XCTAssertNil(image)
XCTAssertNotNil(error)
XCTAssertEqual(error?.code, KingfisherError.badData.rawValue)
self.downloader.delegate = nil
expectation.fulfill()
}
waitForExpectations(timeout: 5, handler: nil)
}
}

extension ImageDownloaderTests: ImageDownloaderDelegate {
func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data? {
return nil
}
}

class URLModifier: ImageDownloadRequestModifier {
Expand Down

0 comments on commit c1fae0f

Please sign in to comment.