How to download a large file with hyper and resume the error?

I want to download large files (500 MB) with hyper, and be able to resume if the download failed.

Is there any way with hyper to run some function for each piece of data received? The method send()returns Result<Response>, but I can not find any Response methods that return the iterator in pieces. Ideally, I could do something like:

client.get(&url.to_string())
    .send()
    .map(|mut res| {
        let mut chunk = String::new();
        // write this chunk to disk
    });

Is this possible, or will it be mapcalled only after hyper has loaded the entire file?

+4
source share
1 answer

Is there any way with hyper to run some function for each piece of data received?

Hyper Response Read. , Response - , , , .

, , ICECat. Read, .

Response Hyper Response.

{
    let mut file = try_s!(fs::File::create(&tmp_path));
    let mut deflate = try_s!(GzDecoder::new(response));

    let mut buf = [0; 128 * 1024];
    let mut written = 0;
    loop {
        status_line! ("icecat_fetch] " (url) ": " (written / 1024 / 1024) " MiB.");
        let len = match deflate.read(&mut buf) {
            Ok(0) => break,  // EOF.
            Ok(len) => len,
            Err(ref err) if err.kind() == io::ErrorKind::Interrupted => continue,
            Err(err) => return ERR!("{}: Download failed: {}", url, err),
        };
        try_s!(file.write_all(&buf[..len]));
        written += len;
    }
}

try_s!(fs::rename(tmp_path, target_path));
status_line_clear();

(500 ) , , .

HTTP "Range" (. RFC 7233).

"Range" . HTTP "Range" , "Range" - . , Hyper Response .

, "Range" .

+4

Source: https://habr.com/ru/post/1665614/


All Articles