uploadAttachment method
- required List<
int> bytes, - required String filename,
- required String mimeType,
- AttachmentKind? preferredKind,
Uploads binary file attachment.
Implementation
Future<FeedbackAttachment> uploadAttachment({
required List<int> bytes,
required String filename,
required String mimeType,
AttachmentKind? preferredKind,
}) async {
final kind = preferredKind ??
(mimeType.startsWith('image/') ? AttachmentKind.image : AttachmentKind.r2);
final path = kind == AttachmentKind.image
? '/api/v1/uploads/images'
: '/api/v1/uploads/r2';
final uri = Uri.parse('${config.baseUrl}$path');
final request = http.MultipartRequest('POST', uri);
request.fields['appKey'] = config.appKey;
request.files.add(
http.MultipartFile.fromBytes(
'file',
bytes,
filename: filename,
),
);
try {
final streamedResponse = await _httpClient.send(request);
final response = await http.Response.fromStream(streamedResponse);
if (response.statusCode != 200 && response.statusCode != 201) {
throw UnexpectedStatusException(response.statusCode, response.body);
}
final json = jsonDecode(response.body) as Map<String, dynamic>;
return FeedbackAttachment.fromJson(json);
} catch (e) {
if (e is FeedbackException) rethrow;
throw UnreadableUploadResponseException(e.toString());
}
}