imgurs/src/cli/upload_image.rs

23 lines
677 B
Rust
Raw Normal View History

use imgurs::api::{upload_image::upload_image as upload_img, ImgurClient};
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;
use std::{fs::read as fs_read, path::Path};
2022-01-22 21:02:51 +00:00
pub async fn upload_image(client: ImgurClient, 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() {
let bytes = fs_read(path)
.map_err(|err| err.to_string())
.expect("read file");
2022-01-25 16:13:43 +00:00
image = base64_encode(bytes);
2022-01-27 15:49:47 +00:00
} else if !validator::validate_url(path) {
panic!("{path} is not a url")
2022-01-22 21:02:51 +00:00
}
let i = upload_img(client, &image).await.expect("send api request");
2022-01-25 16:13:43 +00:00
print_image_info(i, true);
2022-01-22 21:02:51 +00:00
}