do_upload function
Implementation
Future<void> do_upload(String platform, String file_path, String? version,
[String? ios_bundle_id]) async {
final file_to_upload = File(file_path);
var settings = await get_config();
Map<String, String?> url_params = {
'user_id': settings['user_id'],
'app_id': settings['app_id'],
'key': settings['key'],
'platform': platform,
'version': version
};
if (platform == 'ios') {
if ((ios_bundle_id == null) &&
(settings.containsKey('ios_bundle_identifier')))
ios_bundle_id = settings.ios_bundle_identifier;
if (ios_bundle_id == null) {
print(
'Error: iOS bundle identifier must be specified as an argument, or included in your ".apphost" config file.');
return;
}
url_params['ios_bundle_identifier'] = ios_bundle_id;
}
var uri = Uri.https('appho.st', 'api/get_upload_url', url_params);
print('Fetching upload URL...');
var response = await http.get(uri);
String upload_url = response.body;
if (!upload_url.startsWith('https:')) {
throw 'Error fetching upload URL: ' + upload_url;
}
print('Uploading file...');
final stream_request = http.StreamedRequest('PUT', Uri.parse(upload_url));
stream_request.headers['Content-Type'] = 'application/octet-stream';
stream_request.headers['Content-Length'] =
(await file_to_upload.length()).toString();
file_to_upload.openRead().listen((chunk) {
stream_request.sink.add(chunk);
}, onDone: () {
stream_request.sink.close();
});
await stream_request.send();
print('Upload complete! Install your app from:\n');
Uri version_uri = Uri.https('appho.st', 'api/get_current_version/', {
'u': settings['user_id'],
'a': settings['app_id'],
'platform': platform
});
final version_response = await http.get(version_uri);
final version_info = json.decode(version_response.body);
print(version_info['url']);
print('');
}