Skip to content

Commit 5f001eb

Browse files
Added endpoint in the chat client
1 parent dc691a0 commit 5f001eb

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed

Sources/StreamChat/APIClient/AttachmentUploader/AttachmentUploader.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ public protocol AttachmentUploader {
1616
progress: ((Double) -> Void)?,
1717
completion: @escaping (Result<UploadedAttachment, Error>) -> Void
1818
)
19+
20+
/// Uploads a standalone attachment (not tied to message or channel), and returns the attachment with the remote information.
21+
/// - Parameters:
22+
/// - attachment: A standalone attachment.
23+
/// - progress: The progress of the upload.
24+
/// - completion: The callback with the uploaded attachment.
25+
func upload<Payload>(
26+
_ attachment: StreamAttachment<Payload>,
27+
progress: ((Double) -> Void)?,
28+
completion: @escaping (Result<UploadedFile, Error>) -> Void
29+
)
1930
}
2031

2132
public class StreamAttachmentUploader: AttachmentUploader {
@@ -41,4 +52,12 @@ public class StreamAttachmentUploader: AttachmentUploader {
4152
})
4253
}
4354
}
55+
56+
public func upload<Payload>(
57+
_ attachment: StreamAttachment<Payload>,
58+
progress: ((Double) -> Void)?,
59+
completion: @escaping (Result<UploadedFile, Error>) -> Void
60+
) {
61+
cdnClient.uploadAttachment(attachment, progress: progress, completion: completion)
62+
}
4463
}

Sources/StreamChat/ChatClient.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,26 @@ public class ChatClient {
623623
loadAppSettings { continuation.resume(with: $0) }
624624
}
625625
}
626+
627+
// MARK: - Upload attachments
628+
629+
/// Uploads an attachment to the specified CDN.
630+
///
631+
/// - Parameters:
632+
/// - attachment: the attachment to be uploaded.
633+
/// - progress: the progress of the upload.
634+
/// - completion: called when the attachment is uploaded.
635+
public func upload<Payload>(
636+
_ attachment: StreamAttachment<Payload>,
637+
progress: ((Double) -> Void)?,
638+
completion: @escaping (Result<UploadedFile, Error>) -> Void
639+
) {
640+
apiClient.attachmentUploader.upload(
641+
attachment,
642+
progress: progress,
643+
completion: completion
644+
)
645+
}
626646

627647
// MARK: - Internal
628648

Sources/StreamChat/Models/Attachments/ChatMessageAttachment.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ public struct AttachmentUploadingState: Hashable {
114114

115115
/// The information about file size/mimeType.
116116
public let file: AttachmentFile
117+
118+
/// Public init.
119+
public init(localFileURL: URL, state: LocalAttachmentState, file: AttachmentFile) {
120+
self.localFileURL = localFileURL
121+
self.state = state
122+
self.file = file
123+
}
117124
}
118125

119126
// MARK: - Type erasure/recovery

TestTools/StreamChatTestTools/SpyPattern/Spy/AttachmentUploader_Spy.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,22 @@ final class AttachmentUploader_Spy: AttachmentUploader, Spy {
2828
}
2929
}
3030
}
31+
32+
func upload<Payload>(
33+
_ attachment: StreamAttachment<Payload>,
34+
progress: ((Double) -> Void)?,
35+
completion: @escaping (Result<UploadedFile, any Error>) -> Void
36+
) {
37+
record()
38+
39+
if let uploadAttachmentProgress = uploadAttachmentProgress {
40+
progress?(uploadAttachmentProgress)
41+
}
42+
43+
if let uploadAttachmentResult = uploadAttachmentResult {
44+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
45+
completion(uploadAttachmentResult.map { UploadedFile(fileURL: $0.remoteURL )})
46+
}
47+
}
48+
}
3149
}

TestTools/StreamChatTestTools/SpyPattern/Spy/CDNClient_Spy.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,21 @@ final class CDNClient_Spy: CDNClient, Spy {
2828
}
2929
}
3030
}
31+
32+
func uploadAttachment<Payload>(
33+
_ attachment: StreamAttachment<Payload>,
34+
progress: ((Double) -> Void)?,
35+
completion: @escaping (Result<UploadedFile, any Error>) -> Void
36+
) {
37+
record()
38+
if let uploadAttachmentProgress = uploadAttachmentProgress {
39+
progress?(uploadAttachmentProgress)
40+
}
41+
42+
if let uploadAttachmentResult = uploadAttachmentResult {
43+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
44+
completion(uploadAttachmentResult.map { UploadedFile(fileURL: $0) })
45+
}
46+
}
47+
}
3148
}

TestTools/StreamChatTestTools/TestData/CustomCDNClient.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ public final class CustomCDNClient: CDNClient {
1313
progress: ((Double) -> Void)?,
1414
completion: @escaping (Result<URL, Error>) -> Void
1515
) {}
16+
17+
public func uploadAttachment<Payload>(
18+
_ attachment: StreamChat.StreamAttachment<Payload>,
19+
progress: ((Double) -> Void)?,
20+
completion: @escaping (Result<StreamChat.UploadedFile, any Error>) -> Void
21+
) {}
1622
}

0 commit comments

Comments
 (0)