surf
surf copied to clipboard
Callback for download requests
So I want to download a very big file, but with the current API I can only store it in memory and then write to a file, that is a HUGE problem, also I want to show a progress bar or something like that, so I figure out a manner to make this possible:
struct Manager;
impl Manager {
pub fn new() -> Manager {
Manager {}
}
}
impl surf::DownloadManager for Manager {
async fn process_headers(&self, headers: HEADERS) {
// Process content-length, content-disposition, etc
}
async fn process_chunk(&self, chunk: &[u8]) {
// Write chunk to disk and report download progress...
}
}
async fn get() -> Result<(), surf::Exception> {
let mngr = Manager::new();
let mut res = surf::callback_request::get("https://httpbin.org/get", mngr).await?;
Ok(())
}
What do you think? for now, I am using curl directly
I think what you want is a custom struct that impls AsyncBufReader
and can compare the amount of bytes being read to e.g. a Content-Length
header or stat'd file size.
Yeah, I don't see I can pass a reader to surf::Request::body, Are you thinking on put that more... explicitly on the docs?
You want the Response
, and the Response
implements AsyncBufRead
, which is what I was referring to (mistakenly mis-spelt).
Ok, thank you and sorry for the issue