completeMultipartUpload method

Future<PixelBinImage?> completeMultipartUpload(
  1. String? url,
  2. Map<String, String> fields,
  3. int partNumber
)

Implementation

Future<PixelBinImage?> completeMultipartUpload(
    String? url, Map<String, String> fields, int partNumber) async {
  if (url == null) {
    throw Exception("Invalid url: $url");
  }

  final body = {
    ...fields,
    'parts': List.generate(partNumber, (index) => index + 1)
  };

  final request = http.Request('POST', Uri.parse(url))
    ..headers['Content-Type'] = 'application/json'
    ..body = jsonEncode(body);

  final client = http.Client();
  final response = await client.send(request);
  final responseBody = await response.stream.bytesToString();
  print("Response: ${response.statusCode} $responseBody");
  if (response.statusCode >= 200 && response.statusCode < 300) {
    try {
      final data = jsonDecode(responseBody) as Map<String, dynamic>;
      final image = PixelBinImage.fromUrl(data["url"]);
      return image;
    } catch (e) {
      throw Exception(
          "Data can not parsed, but Image is uploaded successfully");
    }
  } else {
    throw Exception("Request Error: ${response.statusCode} $responseBody");
  }
}