vibe_large_file_downloader 0.1.1
vibe_large_file_downloader: ^0.1.1 copied to clipboard
Downloads large files to disk in chunks, with resume, progress, cancel and size and checksum verification before the file is put in place.
vibe_large_file_downloader #
A downloader for large files. It streams to disk in chunks (the file is never held in memory), resumes after interruptions, reports progress, and checks the size and checksum before the file is put in place. It is built for big files that must be exactly right, such as model files and helper programs.
- Verified before use. The file is written as
<name>.part. It gets its final name only after the size and checksum match. A wrong checksum deletes the partial file. - Resumable. A leftover
.partfile is continued with aRangerequest. If the host ignoresRange, the download restarts from zero. - Refuses the wrong file early. A HEAD request checks the size before any bytes are fetched.
- HTML guard. If a host answers with a web page (sign-in, quota, confirm form), you get
notAFileinstead of a corrupt file. - Cancel and progress. Cancel keeps the partial file so you can resume later.
- Manual import fallback.
importFileruns the same checks on a file the user downloaded by hand. - Any hash. SHA-256 by default. Pass any
Hashfrompackage:crypto(sha1,sha512) to change it.
It downloads only the URL you give it. There is no search, no catalog and no background activity.
Extracted from the VIBE music player's add-on downloader.
Install #
dependencies:
vibe_large_file_downloader: ^0.1.1
or dart pub add vibe_large_file_downloader. It depends on http, crypto and path.
Platform support #
Dart VM and Flutter on desktop and mobile (it uses dart:io files). Not for the web.
Use #
import 'package:vibe_large_file_downloader/vibe_large_file_downloader.dart';
final downloader = Downloader();
final cancel = FetchCancel();
try {
final result = await downloader.download(
FetchSpec(url: 'https://example.com/model.bin', bytes: 397807744, digest: 'fbb1...4d6b'),
'/data/model.bin',
cancel: cancel, // call cancel.cancel() from a button
onProgress: (got, total) => print('${(100 * got / total).round()}%'),
);
print(result.verified); // true: the checksum matched
} on FetchError catch (e) {
switch (e.code) {
case FetchErrorCode.network: // unreachable or error status
case FetchErrorCode.size: // wrong size
case FetchErrorCode.hash: // wrong checksum, partial file deleted
case FetchErrorCode.notAFile: // the host sent an HTML page
case FetchErrorCode.cancelled: // partial file kept
case FetchErrorCode.missing: // import source not found
}
}
If the host publishes no checksum, leave digest out. The file is then returned with verified == false and the computed checksum in result.digest, so you can record it and show it to the user.
Manual fallback:
await downloader.importFile(spec, '/home/me/Downloads/model.bin', '/data/model.bin');
The source file is copied, never moved or changed.
Details worth knowing #
- The size must be known and exact (
FetchSpec.bytes). That is what makes the early check and resume safe. probe(url)returns the status, the size and all response headers, so you can read a checksum a host publishes in a header. SetsizeHeaders: ['x-linked-size', 'content-length']on theDownloaderwhen a host keeps the real size in another header.- Turn off the HEAD check with
checkHead: falsefor hosts that answer HEAD wrongly. - Turn off the HTML guard with
rejectHtml: falseif the real file is HTML. - The checksum is computed after the download, in one pass over the file, not while streaming.
- It does not unpack archives. Unpack in your own code after
downloadreturns. - Tested against a local server (resume, ignored Range, dropped connection, wrong size or hash, HTML answer, cancel, import). It was not tested against specific public hosts.
License #
MIT.