ImageDownloaderDelegate.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // ImageDownloaderDelegate.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2018/10/11.
  6. //
  7. // Copyright (c) 2018年 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. /// Protocol of `ImageDownloader`.
  28. public protocol ImageDownloaderDelegate: AnyObject {
  29. /// Called when the `ImageDownloader` object will start downloading an image from specified URL.
  30. ///
  31. /// - Parameters:
  32. /// - downloader: The `ImageDownloader` object which is used for the downloading operation.
  33. /// - url: URL of the original request URL.
  34. /// - request: The request object for the download process.
  35. ///
  36. func imageDownloader(_ downloader: ImageDownloader, willDownloadImageForURL url: URL, with request: URLRequest?)
  37. /// Called when the `ImageDownloader` completes a downloading request with success or failure.
  38. ///
  39. /// - Parameters:
  40. /// - downloader: The `ImageDownloader` object which is used for the downloading operation.
  41. /// - url: URL of the original request URL.
  42. /// - response: The response object of the downloading process.
  43. /// - error: The error in case of failure.
  44. ///
  45. func imageDownloader(
  46. _ downloader: ImageDownloader,
  47. didFinishDownloadingImageForURL url: URL,
  48. with response: URLResponse?, error: Error?)
  49. /// Called when the `ImageDownloader` object successfully downloaded and processed an image from specified URL.
  50. ///
  51. /// - Parameters:
  52. /// - downloader: The `ImageDownloader` object which is used for the downloading operation.
  53. /// - image: The downloaded and processed image.
  54. /// - url: URL of the original request URL.
  55. /// - response: The original response object of the downloading process.
  56. ///
  57. func imageDownloader(
  58. _ downloader: ImageDownloader,
  59. didDownload image: Image,
  60. for url: URL,
  61. with response: URLResponse?)
  62. /// Checks if a received HTTP status code is valid or not.
  63. /// By default, a status code between 200 to 400 (excluded) is considered as valid.
  64. /// If an invalid code is received, the downloader will raise an .invalidStatusCode error.
  65. ///
  66. /// - Parameters:
  67. /// - code: The received HTTP status code.
  68. /// - downloader: The `ImageDownloader` object asks for validate status code.
  69. /// - Returns: Returns a value to indicate whether this HTTP status code is valid or not.
  70. /// - Note: If the default 200 to 400 valid code does not suit your need,
  71. /// you can implement this method to change that behavior.
  72. func isValidStatusCode(_ code: Int, for downloader: ImageDownloader) -> Bool
  73. /// Called when the `ImageDownloader` object successfully downloaded image data from specified URL. This is
  74. /// your last chance to modify the downloaded data before Kingfisher tries to perform addition processing on
  75. /// the image data.
  76. ///
  77. /// - Parameters:
  78. /// - downloader: The `ImageDownloader` object which is used for the downloading operation.
  79. /// - data: Original downloaded data.
  80. /// - url: URL of the original request URL.
  81. /// - Returns: The data from which Kingfisher would use to create an image.
  82. /// - Note: This callback can be used to preprocess raw image data before creation of
  83. /// Image instance (i.e. decrypting or verification).
  84. func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data?
  85. }
  86. extension ImageDownloaderDelegate {
  87. public func imageDownloader(
  88. _ downloader: ImageDownloader,
  89. willDownloadImageForURL url: URL,
  90. with request: URLRequest?) {}
  91. public func imageDownloader(
  92. _ downloader: ImageDownloader,
  93. didFinishDownloadingImageForURL url: URL,
  94. with response: URLResponse?,
  95. error: Error?) {}
  96. public func imageDownloader(
  97. _ downloader: ImageDownloader,
  98. didDownload image: Image,
  99. for url: URL,
  100. with response: URLResponse?) {}
  101. public func isValidStatusCode(_ code: Int, for downloader: ImageDownloader) -> Bool {
  102. return (200..<400).contains(code)
  103. }
  104. public func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data? {
  105. return data
  106. }
  107. }