Download this file
| 1 | import Foundation |
| 2 | |
| 3 | public struct MessageAttachment: Codable, Sendable, Equatable, Identifiable { |
| 4 | public let id: String |
| 5 | public let revision: Int64 |
| 6 | public let kind: String |
| 7 | public let name: String |
| 8 | public let mediaType: String |
| 9 | public let size: Int64 |
| 10 | public let sha256: String |
| 11 | public let width: Int |
| 12 | public let height: Int |
| 13 | public let messageId: String |
| 14 | public let createdAt: Date |
| 15 | } |
| 16 | |
| 17 | extension EquipoClient { |
| 18 | public func upload(_ file: AttachmentFile, progress: @escaping @Sendable (Double) -> Void) async throws -> MessageAttachment { |
| 19 | var request = APIRequest("POST", "/attachments", body: file.data, bearer: token) |
| 20 | request.contentType = file.mediaType |
| 21 | let escaped = file.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? "file" |
| 22 | request.contentDisposition = "attachment; filename*=UTF-8''" + escaped |
| 23 | request.uploadProgress = UploadProgress(progress) |
| 24 | return try await send(request) |
| 25 | } |
| 26 | |
| 27 | public func attachmentData(_ id: String, preview: Bool = false) async throws -> Data { |
| 28 | let id = id.addingPercentEncoding(withAllowedCharacters: .alphanumerics.union(CharacterSet(charactersIn: "_-"))) ?? "" |
| 29 | var request = APIRequest("GET", "/attachments/\(id)" + (preview ? "/preview" : ""), bearer: token) |
| 30 | request.maximumResponseBytes = AttachmentLimits.maxBytes |
| 31 | let response = try await transport.send(request) |
| 32 | guard (200..<300).contains(response.status) else { throw Self.error(response) } |
| 33 | guard response.data.count <= AttachmentLimits.maxBytes else { throw EquipoAPIError.tooLarge } |
| 34 | return response.data |
| 35 | } |
| 36 | } |
| 37 | |