Skip to content

Commit

Permalink
0.3.0: Allow to upload multiple images
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Nov 2, 2017
1 parent a48e275 commit 99a5ba7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fie"
version = "0.2.0"
version = "0.3.0"
authors = ["Douman <douman@gmx.se>"]
repository = "https://github.com/DoumanAsh/fie"
description = "Small and cute twitter app."
Expand Down
11 changes: 7 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -62,7 +65,7 @@ pub enum Commands {
///* First - Text.
///* Second - Tags.
///* Third - Image to attach.
Post(String, Option<Vec<String>>, Option<String>)
Post(String, Option<Vec<String>>, Option<Vec<String>>)
}

impl Commands {
Expand All @@ -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)
},
Expand Down
38 changes: 28 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,39 @@ fn run() -> Result<i32, String> {

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<Box<Future<Item=(), Error=()>>> = 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))
}
Expand Down

0 comments on commit 99a5ba7

Please sign in to comment.