imgurs/src/cli/upload_image.rs

25 lines
644 B
Rust
Raw Normal View History

2022-01-25 16:13:43 +00:00
use imgurs::api::{configuration::ImgurHandle, upload_image::upload_image as upload_img};
2022-01-22 21:02:51 +00:00
use super::print_image_info;
2022-01-25 16:13:43 +00:00
use base64::encode as base64_encode;
2022-01-23 12:05:32 +00:00
use log::error;
2022-01-25 16:14:23 +00:00
use std::{fs::read as fs_read, path::Path, process::exit};
2022-01-22 21:02:51 +00:00
pub async fn upload_image(client: ImgurHandle, path: &str) {
2022-01-22 22:19:59 +00:00
let mut image: String = path.to_string();
2022-01-22 21:02:51 +00:00
if Path::new(path).exists() {
2022-01-25 16:13:43 +00:00
let bytes = fs_read(path).map_err(|err| err.to_string()).unwrap();
2022-01-22 21:02:51 +00:00
2022-01-25 16:13:43 +00:00
image = base64_encode(bytes);
2022-01-22 21:02:51 +00:00
}
2022-01-25 16:14:23 +00:00
let i = upload_img(client, &image).await.unwrap_or_else(|e| {
2022-01-25 16:13:43 +00:00
error!("{e}");
exit(1);
});
print_image_info(i, true);
2022-01-22 21:02:51 +00:00
}