uploadAssets method

Future<BagelResponse> uploadAssets(
  1. List<Map<String, dynamic>> assets
)

selectedAsset expects a file stream, which can be obtained in Flutter using a File object from the file_picker package. Alternatively, it can also be a blob. assetLink can be a link to a file stored somewhere on the web, but the link should be accessible to the device. The method checks if assetLink exists and if not will use selectedAsset The request is sent via a FormData request. @NOTE ⚠️ Either assetLink or selectedAsset must be included but not both @param {List<Map<String, dynamic>>} assets - a slice of maps containing { 'selectedAsset', 'assetLink', 'fileName' }

@returns {Future

Implementation

Future<BagelResponse> uploadAssets(List<Map<String, dynamic>> assets) async {
  BagelDBRequest request = BagelDBRequest(bagelDB: this, collectionID: '');
  Dio dio = await request._dio();
  FormData formData = FormData();

  for (int i = 0; i < assets.length; i++) {
    Map<String, dynamic> asset = assets[i];
    dynamic assetLink = asset['assetLink'] ?? asset['imageLink'];
    if (assetLink != null) {
      formData.fields.add(MapEntry('urlAssets', assetLink));
    } else {
      String fileName = asset['fileName'] ?? 'fallbackFileName$i';

      File file = asset['selectedAsset'] ?? asset['selectedImage'];
      MultipartFile multipartFile = await MultipartFile.fromFile(
        file.path,
        filename: fileName,
      );

      formData.files.add(MapEntry('fileAssets', multipartFile));
    }
  }
  final baseUrl = request.baseEndpoint;
  String url = '$baseUrl/assets';
  Options options = Options(contentType: 'multipart/form-data');
  return dio.post(url, data: formData, options: options).then((res) {
    return BagelResponse(data: (res.data), statusCode: res.statusCode!);
  });
}