diff --git a/examples/file_download.rs b/examples/file_download_upload.rs similarity index 57% rename from examples/file_download.rs rename to examples/file_download_upload.rs index beed3c25a..bf982de53 100644 --- a/examples/file_download.rs +++ b/examples/file_download_upload.rs @@ -1,23 +1,23 @@ /// 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::{self, OpenOptions}, - io::Write, + fs::{self, File, OpenOptions}, + io::{Read, Seek, SeekFrom, Write}, path::PathBuf, }; -use curl::easy::{Easy2, Handler, WriteError}; +use curl::easy::{Easy2, Handler, ReadError, WriteError}; #[derive(Clone)] enum Collector { - File(PathBuf), + File(PathBuf, usize), Ram(Vec), } impl Handler for Collector { fn write(&mut self, data: &[u8]) -> Result { match self { - Collector::File(download_path) => { + Collector::File(download_path, _size) => { println!("File chunk size: {}", data.len()); let mut file = OpenOptions::new() .create(true) @@ -40,12 +40,41 @@ impl Handler for Collector { } } } + + fn read(&mut self, data: &mut [u8]) -> Result { + match self { + Collector::File(path, size) => { + let mut file = File::open(path).map_err(|e| { + eprintln!("{}", e); + ReadError::Abort + })?; + + // Seek to the desired offset + file.seek(SeekFrom::Start(*size as u64)).map_err(|e| { + eprintln!("{}", e); + ReadError::Abort + })?; + + let read_size = file.read(data).map_err(|e| { + eprintln!("{}", e); + ReadError::Abort + })?; + println!("Read Size: {}", read_size); + + // Update this so that we could seek succeding blocks of data from the file + *size += read_size; + + Ok(read_size) + } + Collector::Ram(_) => Ok(0), + } + } } impl Collector { fn get_response_body(&self) -> Option> { match self { - Collector::File(_) => None, + Collector::File(_, _) => None, Collector::Ram(container) => Some(container.clone()), } } @@ -54,7 +83,7 @@ impl Collector { fn example_file_download() { // File Download let target_path = PathBuf::from(""); - let collector = Collector::File(target_path.clone()); + let collector = Collector::File(target_path.clone(), 0); let mut easy2 = Easy2::new(collector); easy2 @@ -101,7 +130,34 @@ fn example_response_as_body() { eprintln!("Response Body: {:?}", response_body); } +fn example_file_upload() { + let target_path = PathBuf::from(""); + let file_size = fs::metadata(target_path.clone()).unwrap().len(); + + let collector = Collector::File(target_path.clone(), 0); + let mut easy2 = Easy2::new(collector); + + easy2 + .url("") + .unwrap(); + easy2.in_filesize(file_size).unwrap(); + easy2.upload(true).unwrap(); + easy2.perform().unwrap(); + + let status_code = easy2.response_code().unwrap(); + let response_body = easy2.get_ref().get_response_body().take(); + let content_type = easy2.content_type(); + + eprintln!("Status Code: {}", status_code); + eprintln!("content-type: {:?}", content_type); + eprintln!("Response Body: {:?}", response_body); + + let file = fs::metadata(target_path.clone()).unwrap(); + assert!(file.len() != 0); +} + fn main() { example_file_download(); example_response_as_body(); + example_file_upload(); }