uploadFileFromURL static method

EitherResponse<String> uploadFileFromURL({
  1. required Map<String, dynamic> metaData,
  2. required String uploadUrl,
  3. required String filePath,
  4. required String contentType,
  5. String? successMessage,
  6. String? errorMessage,
})

Uploads file to the server

*uploadUrl: url to upload the file in server (aws, firebase etc)

*filePath: actual file path of the selected file (pdf, images etc)

*contentType: content type of provided file

Returns either success or failure response based on server status code

Implementation

///Returns either success or failure response based on server status code

static EitherResponse<String> uploadFileFromURL({
  required Map<String, dynamic> metaData,
  required String uploadUrl,
  required String filePath,
  required String contentType,
  String? successMessage,
  String? errorMessage,
}) async {
  try {
    final FormData formData = FormData.fromMap(metaData);

    final MultipartFile file = await _createMultiPartFile(filePath);

    formData.files.add(MapEntry(FileTypeConstants.file, file));

    final response = await _client.post(uploadUrl, data: formData);

    return (response.statusCode ?? 500).isSuccessful
        ? right(successMessage ?? 'Upload successful')
        : left(errorMessage ?? 'Error Uploading');
  } on DioException {
    return left(errorMessage ?? 'Error Uploading with Exception');
  } catch (_) {
    return left(errorMessage ?? 'Error Uploading with Exception');
  }
}