uploadImage static method

  1. @Deprecated('Please use uploadFile function to upload multipart')
Future uploadImage(
  1. String filename,
  2. String fileType, {
  3. String endPoint = 'file-upload',
  4. bool isUserAvatar = false,
  5. bool hasAuth = true,
  6. String thumbnail = '',
  7. required String token,
  8. required String userName,
  9. Map<String, String>? customHeader,
  10. bool? defaultResponse,
  11. String tokenKey = 'Bearer ',
  12. bool? useDefaultURl,
  13. bool? showLogs,
  14. bool? usePreCheckFn,
  15. int? callTimeoutInSec,
})

Implementation

@Deprecated('Please use uploadFile function to upload multipart')
static Future<dynamic> uploadImage(
  String filename,
  String fileType, {
  String endPoint = 'file-upload',
  bool isUserAvatar = false,
  bool hasAuth = true,
  String thumbnail = '',
  required String token,
  required String userName,
  Map<String, String>? customHeader,
  bool? defaultResponse,
  String tokenKey = 'Bearer ',
  bool? useDefaultURl,
  bool? showLogs,
      bool? usePreCheckFn,
  int? callTimeoutInSec,
}) async {
  dynamic response;
  try {
  await callPreCheckFn(usePreCheckFn);
  Uri url = HttpCalls.getRequestURL(endPoint, useDefaultURl: useDefaultURl);
  var header = {'Accept': 'application/json'};

  if (hasAuth) {
    header[Static.httpCallTokenKey??HttpHeaders.authorizationHeader] = '${Static.canHttpCallAddBearerAsPreToken?tokenKey:''}$token';
  }
  if (headerAddOns != null) {
    header.addAll(headerAddOns!);
  }

  showLog((customHeader ?? httpHeader ?? header),
      showLog: showLogs, logName: endPoint);

    var request = http.MultipartRequest(
      'POST',
      url,
    );
    request.files.add(await http.MultipartFile.fromPath('file', filename));
    if (fileType == 'video') {
      request.files
          .add(await http.MultipartFile.fromPath('thumbnail', thumbnail));
    }
    request.fields['fileType'] = fileType;
    request.fields['userName'] = userName;

    if (hasAuth) request.headers.addAll(customHeader ?? httpHeader ?? header);
    var streamedResponse = await request.send();
    var result = await Response.fromStream(streamedResponse);
    showLog(result.body.toString(),
        enableJsonEncode: false, showLog: showLogs, logName: endPoint);
    response =
        HttpCalls.getDataObject(result, defaultResponse: defaultResponse);
  } catch (e) {

    response = errorHandler(e, response, defaultResponse);
  }
  return response;
}