uploadFile method

Future<FilesUploadResult> uploadFile(
  1. String path,
  2. MultipartFile file, {
  3. String? storageName,
})

Upload file

Implementation

Future<FilesUploadResult> uploadFile(String path, MultipartFile file,
    {String? storageName}) async {
  // ignore: prefer_final_locals
  Object? postBody;

  // create path and map variables
  final String requestPath = "/barcode/storage/file/{path}"
      .replaceAll("{format}", "json")
      .replaceAll("{path}", path);

  // query params
  final List<QueryParam> queryParams = [];
  final Map<String, String> headerParams = {};
  final Map<String, String> formParams = {};
  if (storageName != null) {
    queryParams.addAll(
        convertParametersForCollectionFormat("", "storageName", storageName));
  }

  final List<String> contentTypes = ["multipart/form-data"];

  final String contentType =
      contentTypes.isNotEmpty ? contentTypes[0] : "application/json";
  final List<String> authNames = ["JWT"];

  if (contentType.startsWith("multipart/form-data")) {
    bool hasFields = false;
    MultipartRequestPlus? mp;

    mp = MultipartRequestPlus('PUT', Uri.parse(requestPath));
    hasFields = true;
    mp.files.add(file);

    if (hasFields) {
      postBody = mp;
    }
  } else {}

  final response = await _apiClient.invokeAPI(requestPath, 'PUT', queryParams,
      postBody, headerParams, formParams, contentType, authNames);

  if (response.statusCode >= 400) {
    throw ApiException(response.statusCode, response.body);
  } else {
    return _apiClient.deserialize(response.body, 'FilesUploadResult')
        as FilesUploadResult;
  }
}