downloadReleaseAsset function
Future<void>
downloadReleaseAsset(
- String asset,
- String dest, {
- bool executable = false,
- BytesFetcher? fetch,
Downloads release asset asset to dest and checks it against its published sha256; executable marks it runnable.
Implementation
Future<void> downloadReleaseAsset(String asset, String dest, {bool executable = false, BytesFetcher? fetch}) async {
final get = fetch ?? _httpGet;
final uri = releaseAssetUri(asset);
final expected =
String.fromCharCodes(await get(Uri.parse('$uri.sha256'))).trim().split(RegExp(r'\s+')).first.toLowerCase();
final bytes = await get(uri);
final actual = sha256.convert(bytes).toString();
if (actual != expected) {
throw BridgeDownloadError('checksum mismatch for $uri: expected $expected, got $actual');
}
final partial = File('$dest.partial-$pid');
await partial.parent.create(recursive: true);
await partial.writeAsBytes(bytes, flush: true);
if (executable) {
final chmod = await runBounded(
'chmod',
['755', partial.path],
timeout: const Duration(seconds: 5),
phase: 'chmod of the downloaded bridge',
);
if (chmod.exitCode != 0) {
await partial.delete();
throw BridgeDownloadError('chmod failed: ${chmod.stderr}');
}
}
await partial.rename(dest);
}