imgurs/src/cli/upload_image.rs

50 lines
1.4 KiB
Rust
Raw Normal View History

use imgurs::ImgurClient;
use notify_rust::Notification;
2022-05-18 18:52:47 +00:00
use crate::{
2022-07-08 09:38:16 +00:00
cli::{clipboard::set_clipboard, print_image_info},
2022-05-18 18:52:47 +00:00
config::toml,
};
// show notification
macro_rules! notify (
($notification: expr) => (
if toml::parse().notification.enabled {
$notification.show().expect("send notification");
}
);
);
pub async fn upload_image(client: ImgurClient, path: String) {
// parse configuration file
let config = toml::parse();
// upload a image to imgur
2022-05-18 17:48:39 +00:00
let mut i = client.upload_image(&path).await.unwrap_or_else(|err| {
2022-02-27 11:32:03 +00:00
notify!(Notification::new()
.summary("Error!")
2022-05-18 18:52:47 +00:00
.body(&format!("Error: {}", err))
2022-02-27 11:32:03 +00:00
.appname("Imgurs")); // I don't think you can set it to error
2022-05-29 09:12:23 +00:00
panic!("send request to imgur api: {}", err)
2022-02-27 11:32:03 +00:00
});
// change domain to proxy (to be set in config)
if config.imgur.image_cdn != "i.imgur.com" {
i.data.link = i.data.link.replace("i.imgur.com", &config.imgur.image_cdn)
}
// print image information from imgur
2022-05-18 18:52:47 +00:00
print_image_info(&i);
// send notification that the image has been uploaded
notify!(Notification::new()
.summary("Imgurs")
.body(&format!("Uploaded {}", i.data.link)));
// if enabled copy link to clipboard
if config.clipboard.enabled {
2022-05-18 18:52:47 +00:00
set_clipboard(&i.data.link)
}
2022-01-22 21:02:51 +00:00
}