Skip to content

Commit

Permalink
Introduce urlRequest(forImage) to ImageCDN
Browse files Browse the repository at this point in the history
This will allow users to inject their HTTP headers to image loading requests
  • Loading branch information
Bahadir Oncel authored and b-onc committed Jul 20, 2021
1 parent bd0143b commit 11ed592
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming

### ✅ Added
- `urlRequest(forImage url:)` added to `ImageCDN` protocol, this can be used to inject custom HTTP headers into image loading requests [#1291](https://github.com/GetStream/stream-chat-swift/issues/1291)

### 🐞 Fixed
- Fix an issue where member role sent from backend was not recognized by the SDK [#1288](https://github.com/GetStream/stream-chat-swift/pull/1288)
- Fix crash in `ChannelListUpdater` caused by the lifetime not aligned with `ChatClient` [#1289](https://github.com/GetStream/stream-chat-swift/pull/1289)
Expand Down
25 changes: 22 additions & 3 deletions Sources/StreamChatUI/Utils/ImageCDN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public protocol ImageCDN {
/// - Returns: String to be used as an image cache key.
func cachingKey(forImage url: URL) -> String

/// Prepare and return a `URLRequest` for the given image `URL`
/// This function can be used to inject custom headers for image loading request.
func urlRequest(forImage url: URL) -> URLRequest

/// Enhance image URL with size parameters to get thumbnail
/// - Parameters:
/// - originalURL: URL of the image to get the thumbnail for.
Expand All @@ -20,10 +24,19 @@ public protocol ImageCDN {
func thumbnailURL(originalURL: URL, preferredSize: CGSize) -> URL
}

public struct StreamImageCDN: ImageCDN {
extension ImageCDN {
public func urlRequest(forImage url: URL) -> URLRequest {
URLRequest(url: url)
}
}

open class StreamImageCDN: ImageCDN {
public static var streamCDNURL = "stream-io-cdn.com"

public func cachingKey(forImage url: URL) -> String {
// Initializer required for subclasses
public init() {}

open func cachingKey(forImage url: URL) -> String {
let key = url.absoluteString

guard
Expand All @@ -40,7 +53,13 @@ public struct StreamImageCDN: ImageCDN {
return components.string ?? key
}

public func thumbnailURL(originalURL: URL, preferredSize: CGSize) -> URL {
// Although this is the same as the default impl, we still need it
// so subclasses can safely override it
open func urlRequest(forImage url: URL) -> URLRequest {
URLRequest(url: url)
}

open func thumbnailURL(originalURL: URL, preferredSize: CGSize) -> URL {
guard
var components = URLComponents(url: originalURL, resolvingAgainstBaseURL: true),
let host = components.host,
Expand Down
7 changes: 6 additions & 1 deletion Sources/StreamChatUI/Utils/UIImageView+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ extension UIImageView {
}

let imageKey = components.imageCDN.cachingKey(forImage: url)
let request = ImageRequest(url: url, processors: preprocessors, options: ImageRequestOptions(filteredURL: imageKey))
let urlRequest = components.imageCDN.urlRequest(forImage: url)
let request = ImageRequest(
urlRequest: urlRequest,
processors: preprocessors,
options: ImageRequestOptions(filteredURL: imageKey)
)
let options = ImageLoadingOptions(placeholder: placeholder)

currentImageLoadingTask = Nuke.loadImage(with: request, options: options, into: self, completion: completion)
Expand Down
5 changes: 5 additions & 0 deletions fastlane/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ Builds SlackClone app
fastlane build_messenger_clone
```
Builds MessengerClone app
### build_youtube_clone
```
fastlane build_youtube_clone
```
Builds YouTubeClone app
### build_docs_snippets
```
fastlane build_docs_snippets
Expand Down

0 comments on commit 11ed592

Please sign in to comment.