downloadVerified function
Future<File>
downloadVerified(
- DownloadAsset asset,
- File destination, {
- Client? client,
- String? source,
Downloads asset atomically, reusing an existing file only if its hash matches.
A failed download never replaces an existing destination.
Tries each of DownloadAsset.urls in order. A URL whose path ends in
.zip and that has a fragment, such as .../libraries.zip#text/x/lib.so,
names that member of the archive, read with HTTP range requests without
the rest of the archive. Each request gets up to three attempts on a
connection error, a timeout or HTTP 408, 429, 500, 502, 503 or 504, and a
dropped download resumes where it stopped. At the last URL, a failed lookup
or a lost connection keeps being retried for 75 seconds from the first
failure, so a short network outage passes and having no network at all
fails only after that long.
Implementation
Future<File> downloadVerified(
DownloadAsset asset,
File destination, {
http.Client? client,
String? source,
}) async {
if (!RegExp(r'^[a-f0-9]{64}$').hasMatch(asset.sha256)) {
throw ArgumentError.value(asset.sha256, 'sha256', 'Expected SHA-256 hex');
}
if (await destination.exists() &&
(await sha256.bind(destination.openRead()).first).toString() ==
asset.sha256) {
return destination;
}
await destination.parent.create(recursive: true);
final configured = source ?? Platform.environment['MEDIAPIPE_ASSET_SOURCE'];
final locations = configured == null || configured.isEmpty
? asset.urls.toList()
: [
configured.startsWith('http://') || configured.startsWith('https://')
? Uri.parse(
'${configured.endsWith('/') ? configured : '$configured/'}${asset.sha256}',
).toString()
: File.fromUri(
Directory(configured).uri.resolve(asset.sha256),
).uri.toString(),
];
final failures = <DownloadFailure>[];
final connection = _Connection(client ?? http.Client());
try {
for (final location in locations) {
// A location further on may answer now, so only the last one waits.
connection.patience = location == locations.last
? _outagePatience
: Duration.zero;
final temporary = await destination.parent.createTemp('.download-');
final partial = File.fromUri(temporary.uri.resolve('asset'));
try {
final uri = Uri.parse(location);
if (uri.scheme == 'file') {
await File.fromUri(uri).copy(partial.path);
} else if (uri.path.endsWith('.zip') && uri.fragment.isNotEmpty) {
await connection.zipMember(
uri.removeFragment(),
Uri.decodeComponent(uri.fragment),
partial,
File.fromUri(temporary.uri.resolve('compressed')),
);
} else {
await connection.file(uri, partial);
}
final digest = (await sha256.bind(partial.openRead()).first).toString();
if (digest != asset.sha256) {
throw StateError(
'SHA-256 mismatch: expected ${asset.sha256}, received $digest',
);
}
await partial.rename(destination.path);
return destination;
} catch (error) {
failures.add(DownloadFailure(location, error.toString()));
} finally {
await temporary.delete(recursive: true);
}
}
throw DownloadException(failures);
} finally {
if (client == null) connection.client.close();
}
}