From 99a5ba7561577bd891e0e7434143dc94f3b80b59 Mon Sep 17 00:00:00 2001 From: Douman Date: Thu, 2 Nov 2017 10:43:23 +0300 Subject: [PATCH] 0.3.0: Allow to upload multiple images --- Cargo.toml | 2 +- src/cli.rs | 11 +++++++---- src/main.rs | 38 ++++++++++++++++++++++++++++---------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 595d62e..20498c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fie" -version = "0.2.0" +version = "0.3.0" authors = ["Douman "] repository = "https://github.com/DoumanAsh/fie" description = "Small and cute twitter app." diff --git a/src/cli.rs b/src/cli.rs index 63577b4..5dff691 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -35,9 +35,12 @@ fn new_command() -> App<'static, 'static> { .arg(arg("tag").short("t") .takes_value(true) .number_of_values(1) - .multiple(true)) + .multiple(true) + .help("Adds hashtag at the last line of post.")) .arg(arg("image").short("i") - .takes_value(true)) + .multiple(true) + .takes_value(true) + .help("Adds image to post. Normally up to 4.")) } pub fn parser() -> App<'static, 'static> { @@ -62,7 +65,7 @@ pub enum Commands { ///* First - Text. ///* Second - Tags. ///* Third - Image to attach. - Post(String, Option>, Option) + Post(String, Option>, Option>) } impl Commands { @@ -74,7 +77,7 @@ impl Commands { "post" => { let message = matches.value_of("message").unwrap().to_string(); let tags = matches.values_of("tag").map(|values| values.map(|tag| format!("#{}", tag)).collect()); - let image = matches.value_of("image").map(|image| image.to_string()); + let image = matches.values_of("image").map(|images| images.map(|image| image.to_string()).collect()); Commands::Post(message, tags, image) }, diff --git a/src/main.rs b/src/main.rs index b28719b..fb832ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,21 +49,39 @@ fn run() -> Result { tokio_core.run(futures::future::join_all(jobs)).unwrap(); }, - cli::Commands::Post(message, tags, Some(image)) => { + cli::Commands::Post(message, tags, Some(images)) => { let mut jobs: Vec>> = vec![]; - let image = utils::open_image(image).map_err(error_formatter!("Cannot open image!"))?; + let images = { + let mut result = vec![]; + for image in images { + result.push(utils::open_image(image).map_err(error_formatter!("Cannot open image!"))?); + } + result + }; + if args.flags.gab { - let gab_post = gab.upload_image(&image).map_err(error_formatter!("Cannot upload image.")) - .and_then(handle_bad_hyper_response!("Cannot upload image.")) - .and_then(|response| response.body().concat2().map_err(error_formatter!("Cannot read image upload's response"))) - .and_then(|body| serde_json::from_slice(&body).map_err(error_formatter!("Cannot parse image upload's response"))) - .and_then(|response: api::gab::payload::UploadResponse| gab.post_w_images(&message, &tags, &[response.id]).map_err(error_formatter!("Cannot post."))) - .then(api::gab::Client::handle_post); + let mut gab_images: Vec<_> = vec![]; + for image in images.iter() { + gab_images.push(gab.upload_image(&image).map_err(error_formatter!("Cannot upload image.")) + .and_then(handle_bad_hyper_response!("Cannot upload image.")) + .and_then(|response| response.body().concat2().map_err(error_formatter!("Cannot read image upload's response"))) + .and_then(|body| serde_json::from_slice(&body).map_err(error_formatter!("Cannot parse image upload's response"))) + .map(|response: api::gab::payload::UploadResponse| response.id)); + } + + let gab_post = futures::future::join_all(gab_images).and_then(|images| gab.post_w_images(&message, &tags, &images).map_err(error_formatter!("Cannot post."))) + .then(api::gab::Client::handle_post); jobs.push(Box::new(gab_post)) } if args.flags.twitter { - let tweet = twitter.upload_image(&image).map_err(error_formatter!("Cannot upload image.")) - .and_then(|rsp| twitter.post_w_images(&message, &tags, &[rsp.response.id]).map_err(error_formatter!("Cannot tweet."))) + let mut tweet_images: Vec<_> = vec![]; + for image in images.iter() { + tweet_images.push(twitter.upload_image(&image).map_err(error_formatter!("Cannot upload image.")) + .map(|rsp| rsp.response.id)); + } + + let tweet = futures::future::join_all(tweet_images) + .and_then(|images| twitter.post_w_images(&message, &tags, &images).map_err(error_formatter!("Cannot tweet."))) .then(api::twitter::Client::handle_post); jobs.push(Box::new(tweet)) }