upload method

Future<String> upload(
  1. DeviceScreenshot screenshot
)

Upload a given screenshot and print the resulting url in the debugging console.

Implementation

Future<String> upload(DeviceScreenshot screenshot) async {
  var request = http.MultipartRequest('POST', Uri.parse(host));
  request.files.add(
    http.MultipartFile.fromBytes(
      'file',
      screenshot.bytes,
      filename: 'screenshot.png',
    ),
  );
  final response = await request.send();

  if (response.statusCode != 200) {
    throw Error();
  }

  final bodyString = await response.stream.transform(utf8.decoder).join();
  final body = jsonDecode(bodyString);

  if (!body['success']) {
    throw Error();
  }

  final link = body['link'];
  // ignore: avoid_print
  print('[DevicePreview] Screenshot available here: $link');

  return link;
}