Skip to content

Commit

Permalink
add example to use Easy2 in downloading files
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoLeonardo committed Oct 19, 2023
1 parent de57280 commit e021321
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions examples/file_download.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/// This is an example how to use Easy2 to download a file.
/// Can able to resume a download and control download speed.
use std::{fs::OpenOptions, io::Write, path::PathBuf};

use curl::easy::{Easy2, Handler, WriteError};

enum Collector {
File(PathBuf),
// You can add what type of storage you want
}

impl Handler for Collector {
fn write(&mut self, data: &[u8]) -> Result<usize, WriteError> {
match self {
Collector::File(download_path) => {
println!("File chunk size: {}", data.len());
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(download_path)
.map_err(|e| {
eprintln!("{}", e);
WriteError::Pause
})?;

file.write_all(data).map_err(|e| {
eprintln!("{}", e);
WriteError::Pause
})?;
Ok(data.len())
}
}
}
}
fn main() {
let collector = Collector::File(PathBuf::from("<Your target path>"));
let mut easy2 = Easy2::new(collector);

easy2.url("https://www.rust-lang.org/").unwrap();
easy2.get(true).unwrap();
// Setting of download speed control in bytes per second
easy2.max_recv_speed(2000).unwrap();
// Can resume download by giving a byte offset
easy2.resume_from(0).unwrap();
easy2.perform().unwrap();

let status_code = easy2.response_code().unwrap();
let content_type = easy2.content_type().unwrap();
eprintln!("Content Type: {:?}", content_type);
eprintln!("Status Code: {}", status_code);
}

0 comments on commit e021321

Please sign in to comment.