ImageDownloaderDelegate.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // ImageDownloaderDelegate.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2018/10/11.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. #if os(macOS)
  28. import AppKit
  29. #else
  30. import UIKit
  31. #endif
  32. /// Protocol for handling events for ``ImageDownloader``.
  33. ///
  34. /// This delegate protocol provides a set of methods related to the stages and rules of the image downloader. You use
  35. /// the provided methods to inspect the downloader working phases or respond to some events to make decisions.
  36. public protocol ImageDownloaderDelegate: AnyObject {
  37. /// Called when the ``ImageDownloader`` object is about to start downloading an image from a specified URL.
  38. ///
  39. /// - Parameters:
  40. /// - downloader: The ``ImageDownloader`` object used for the downloading operation.
  41. /// - url: The URL of the starting request.
  42. /// - request: The request object for the download process.
  43. func imageDownloader(_ downloader: ImageDownloader, willDownloadImageForURL url: URL, with request: URLRequest?)
  44. /// Called when the ``ImageDownloader`` completes a downloading request with success or failure.
  45. ///
  46. /// - Parameters:
  47. /// - downloader: The ``ImageDownloader`` object used for the downloading operation.
  48. /// - url: The URL of the original request.
  49. /// - response: The response object of the downloading process.
  50. /// - error: The error in case of failure.
  51. func imageDownloader(
  52. _ downloader: ImageDownloader,
  53. didFinishDownloadingImageForURL url: URL,
  54. with response: URLResponse?,
  55. error: Error?)
  56. /// Called when the ``ImageDownloader`` object successfully downloads image data with a specified task.
  57. ///
  58. /// This is your last chance to verify or modify the downloaded data before Kingfisher attempts to perform
  59. /// additional processing on the image data.
  60. ///
  61. /// - Parameters:
  62. /// - downloader: The ``ImageDownloader`` object used for the downloading operation.
  63. /// - data: The original downloaded data.
  64. /// - dataTask: The data task containing request and response information for the download.
  65. ///
  66. /// - Returns: The data that Kingfisher should use to create an image. You need to provide valid data that is in
  67. /// one of the supported image file formats. Kingfisher will process this data and attempt to convert it into an
  68. /// image object.
  69. ///
  70. ///
  71. /// This method can be used to preprocess raw image data before the creation of the `Image` instance (e.g.,
  72. /// decrypting or verification). If `nil` is returned, the processing is interrupted and a
  73. /// ``KingfisherError/ResponseErrorReason/dataModifyingFailed(task:)`` error will be raised. You can use this fact
  74. /// to stop the image processing flow if you find that the data is corrupted or malformed.
  75. ///
  76. /// > If this method is implemented, ``ImageDownloaderDelegate/imageDownloader(_:didDownload:for:)-5btcl`` will not
  77. /// be called anymore.
  78. func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, with dataTask: SessionDataTask) -> Data?
  79. /// Called when the ``ImageDownloader`` object successfully downloads image data from a specified URL.
  80. ///
  81. /// This is your last chance to verify or modify the downloaded data before Kingfisher attempts to perform
  82. /// additional processing on the image data.
  83. ///
  84. /// - Parameters:
  85. /// - downloader: The ``ImageDownloader`` object used for the downloading operation.
  86. /// - data: The original downloaded data.
  87. /// - url: The URL of the original request.
  88. ///
  89. /// - Returns: The data that Kingfisher should use to create an image. You need to provide valid data that is in
  90. /// one of the supported image file formats. Kingfisher will process this data and attempt to convert it into an
  91. /// image object.
  92. ///
  93. /// This method can be used to preprocess raw image data before the creation of the `Image` instance (e.g.,
  94. /// decrypting or verification). If `nil` is returned, the processing is interrupted and a
  95. /// ``KingfisherError/ResponseErrorReason/dataModifyingFailed(task:)`` error will be raised. You can use this fact
  96. /// to stop the image processing flow if you find that the data is corrupted or malformed.
  97. ///
  98. /// > If ``imageDownloader(_:didDownload:with:)-9w9fn`` is implemented, this method will not be called anymore.
  99. func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data?
  100. /// Called when the ``ImageDownloader`` object successfully downloads and processes an image from a specified URL.
  101. ///
  102. /// - Parameters:
  103. /// - downloader: The ``ImageDownloader`` object used for the downloading operation.
  104. /// - image: The downloaded and processed image.
  105. /// - url: The URL of the original request.
  106. /// - response: The original response object of the downloading process.
  107. func imageDownloader(
  108. _ downloader: ImageDownloader,
  109. didDownload image: KFCrossPlatformImage,
  110. for url: URL,
  111. with response: URLResponse?)
  112. /// Checks if a received HTTP status code is valid or not.
  113. ///
  114. /// By default, a status code in the range `200..<400` is considered as valid. If an invalid code is received,
  115. /// the downloader will raise a ``KingfisherError/ResponseErrorReason/invalidHTTPStatusCode(response:)`` error.
  116. ///
  117. /// - Parameters:
  118. /// - code: The received HTTP status code.
  119. /// - downloader: The ``ImageDownloader`` object requesting validation of the status code.
  120. /// - Returns: A value indicating whether this HTTP status code is valid or not.
  121. ///
  122. /// > If the default range of `200..<400` as valid codes does not suit your needs, you can implement this method to
  123. /// change that behavior.
  124. func isValidStatusCode(_ code: Int, for downloader: ImageDownloader) -> Bool
  125. /// Called when the task has received a valid HTTP response after passing other checks such as the status code.
  126. /// You can perform additional checks or verifications on the response to determine if the download should be
  127. /// allowed or cancelled.
  128. ///
  129. /// For example, this is useful if you want to verify some header values in the response before actually starting
  130. /// the download.
  131. ///
  132. /// If implemented, you have to return a proper response disposition, such as `.allow` to start the actual
  133. /// downloading or `.cancel` to cancel the task. If `.cancel` is used as the disposition, the downloader will raise
  134. /// a ``KingfisherError/ResponseErrorReason/cancelledByDelegate(response:)`` error. If not implemented, any response
  135. /// that passes other checks will be allowed, and the download will start.
  136. ///
  137. /// - Parameters:
  138. /// - downloader: The `ImageDownloader` object used for the downloading operation.
  139. /// - response: The original response object of the downloading process.
  140. ///
  141. /// - Returns: The disposition for the download task. You have to return either `.allow` or `.cancel`.
  142. func imageDownloader(
  143. _ downloader: ImageDownloader,
  144. didReceive response: URLResponse
  145. ) async -> URLSession.ResponseDisposition
  146. }
  147. // Default implementation for `ImageDownloaderDelegate`.
  148. extension ImageDownloaderDelegate {
  149. public func imageDownloader(
  150. _ downloader: ImageDownloader,
  151. willDownloadImageForURL url: URL,
  152. with request: URLRequest?) {}
  153. public func imageDownloader(
  154. _ downloader: ImageDownloader,
  155. didFinishDownloadingImageForURL url: URL,
  156. with response: URLResponse?,
  157. error: Error?) {}
  158. public func imageDownloader(
  159. _ downloader: ImageDownloader,
  160. didDownload image: KFCrossPlatformImage,
  161. for url: URL,
  162. with response: URLResponse?) {}
  163. public func isValidStatusCode(_ code: Int, for downloader: ImageDownloader) -> Bool {
  164. return (200..<400).contains(code)
  165. }
  166. public func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, with task: SessionDataTask) -> Data? {
  167. guard let url = task.originalURL else {
  168. return data
  169. }
  170. return imageDownloader(downloader, didDownload: data, for: url)
  171. }
  172. public func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data? {
  173. return data
  174. }
  175. public func imageDownloader(
  176. _ downloader: ImageDownloader,
  177. didReceive response: URLResponse
  178. ) async -> URLSession.ResponseDisposition {
  179. .allow
  180. }
  181. }