uploadMediaList method
Uploads an array of media to the media center of MBurger
- Parameters:
paths
: The array of path of the files that needs to be uploaded to the media center.
- Returns a Future that completes with the media that has been created.
Implementation
Future<List<MBMedia>> uploadMediaList(List<String> paths) async {
List<MBMultipartForm> form = [];
int index = 0;
for (String path in paths) {
String? mime = lookupMimeType(path);
form.add(MBMultipartForm.file(
"media[$index]",
path,
mime != null
? MediaType.parse(mime)
: MediaType(
'application',
'octet-stream',
), // General mime type if nothing else is found
));
index++;
}
String apiName = 'api/media';
var uri = Uri.https(manager.endpoint, apiName);
var request = http.MultipartRequest('POST', uri);
await _addMultipartFormsToRequest(request, form);
request.headers.addAll(await manager.headers());
http.StreamedResponse response = await request.send();
final responseString = await response.stream.bytesToString();
List<dynamic> bodyList = MBManager.checkResponseForType<List<dynamic>>(
responseString,
checkBody: true,
);
List<Map<String, dynamic>> bodyMaps =
List.castFrom<dynamic, Map<String, dynamic>>(bodyList);
return bodyMaps.map((d) => MBMedia(dictionary: d)).toList();
}