GetStream / stream-chat-swiftui

SwiftUI Chat SDK โžœ Stream Chat ๐Ÿ’ฌ
https://getstream.io/chat/sdk/swiftui/
Other
354 stars 88 forks source link

Add FileCDN class enable generating signedURL's in File Attachments #620

Closed bpollman closed 1 month ago

bpollman commented 1 month ago

๐Ÿ”— Issue Link

Jira or Github issue link, if applicable.

๐ŸŽฏ Goal

To use our own CDN, we need the equivalent of ImageCDN to intercept a given URL and provide a signed URL when dealing with File attachments.

๐Ÿ›  Implementation

Example of how we are using it to make an async call for a signed URL.

public final class CustomFileCDN: FileCDN {

    static var customCDNURL = "mycustomCDN.com"

    let attachmentService: ChatAttachmentService

    init(attachmentService: ChatAttachmentService) {
        self.attachmentService = attachmentService
    }

    public func adjustedURL(for url: URL, completion: @escaping ((Result<URL, any Error>) -> Void)) {
        Task {
            do {
                let signedUrl = try await signedUrl(for: url)
                completion(.success(signedUrl))

            } catch {
                completion(.failure(error))
            }
        }
    }

    func signedUrl(for url: URL) async throws -> URL {
        guard
            let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
            let host = components.host,
            host.contains(Self.customCDNURL)
        else {
            return url
        }
        return try await self.attachmentService.signedDownloadUrl(url)
    }
}

๐Ÿงช Testing

Describe the steps how this change can be tested (or why it can't be tested).

๐ŸŽจ Changes

Add relevant screenshots or videos showcasing the changes.

โ˜‘๏ธ Checklist