-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathAttachmentUploader.swift
More file actions
67 lines (60 loc) · 2.41 KB
/
AttachmentUploader.swift
File metadata and controls
67 lines (60 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Copyright © 2026 Stream.io Inc. All rights reserved.
//
import Foundation
/// The component responsible to upload files.
public protocol AttachmentUploader: Sendable {
/// Uploads a type-erased attachment, and returns the attachment with the remote information.
/// - Parameters:
/// - attachment: A type-erased attachment.
/// - progress: The progress of the upload.
/// - completion: The callback with the uploaded attachment.
func upload(
_ attachment: AnyChatMessageAttachment,
progress: (@Sendable (Double) -> Void)?,
completion: @escaping @Sendable (Result<UploadedAttachment, Error>) -> Void
)
/// Uploads a standalone attachment (not tied to message or channel), and returns the attachment with the remote information.
/// - Parameters:
/// - attachment: A standalone attachment.
/// - progress: The progress of the upload.
/// - completion: The callback with the uploaded attachment.
func uploadStandaloneAttachment<Payload>(
_ attachment: StreamAttachment<Payload>,
progress: (@Sendable (Double) -> Void)?,
completion: @escaping @Sendable (Result<UploadedFile, Error>) -> Void
)
}
public class StreamAttachmentUploader: AttachmentUploader, @unchecked Sendable {
let cdnClient: CDNClient
init(cdnClient: CDNClient) {
self.cdnClient = cdnClient
}
public func upload(
_ attachment: AnyChatMessageAttachment,
progress: (@Sendable (Double) -> Void)?,
completion: @escaping @Sendable (Result<UploadedAttachment, Error>) -> Void
) {
cdnClient.uploadAttachment(attachment, progress: progress) { (result: Result<UploadedFile, Error>) in
completion(result.map { file in
let uploadedAttachment = UploadedAttachment(
attachment: attachment,
remoteURL: file.fileURL,
thumbnailURL: file.thumbnailURL
)
return uploadedAttachment
})
}
}
public func uploadStandaloneAttachment<Payload>(
_ attachment: StreamAttachment<Payload>,
progress: (@Sendable (Double) -> Void)?,
completion: @escaping @Sendable (Result<UploadedFile, Error>) -> Void
) {
cdnClient.uploadStandaloneAttachment(
attachment,
progress: progress,
completion: completion
)
}
}