Skip to content

Commit

Permalink
add file upload example using Easy2
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoLeonardo committed Oct 21, 2023
1 parent 7cc456d commit 1d027c9
Showing 1 changed file with 63 additions and 7 deletions.
70 changes: 63 additions & 7 deletions examples/file_download.rs → examples/file_download_upload.rs
Original file line number Diff line number Diff line change
@@ -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<u8>),
}

impl Handler for Collector {
fn write(&mut self, data: &[u8]) -> Result<usize, WriteError> {
match self {
Collector::File(download_path) => {
Collector::File(download_path, _size) => {
println!("File chunk size: {}", data.len());
let mut file = OpenOptions::new()
.create(true)
Expand All @@ -40,12 +40,41 @@ impl Handler for Collector {
}
}
}

fn read(&mut self, data: &mut [u8]) -> Result<usize, ReadError> {
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<Vec<u8>> {
match self {
Collector::File(_) => None,
Collector::File(_, _) => None,
Collector::Ram(container) => Some(container.clone()),
}
}
Expand All @@ -54,7 +83,7 @@ impl Collector {
fn example_file_download() {
// File Download
let target_path = PathBuf::from("<SAVE DOWNLOADED FILE HERE>");
let collector = Collector::File(target_path.clone());
let collector = Collector::File(target_path.clone(), 0);
let mut easy2 = Easy2::new(collector);

easy2
Expand Down Expand Up @@ -101,7 +130,34 @@ fn example_response_as_body() {
eprintln!("Response Body: {:?}", response_body);
}

fn example_file_upload() {
let target_path = PathBuf::from("<SOURCE FILE PATH TO BE UPLOADED HERE>");
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("<INPUT YOUR TARGET UPLOAD LOCATION HERE>")
.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();
}

0 comments on commit 1d027c9

Please sign in to comment.