2022-01-27 13:14:46 +00:00
|
|
|
use imgurs::api::{upload_image::upload_image as upload_img, ImgurClient};
|
2022-01-22 21:02:51 +00:00
|
|
|
|
2022-01-26 18:55:08 +00:00
|
|
|
use super::print_image_info;
|
|
|
|
|
2022-01-25 16:13:43 +00:00
|
|
|
use base64::encode as base64_encode;
|
2022-01-27 13:14:46 +00:00
|
|
|
use std::{fs::read as fs_read, path::Path};
|
2022-01-22 21:02:51 +00:00
|
|
|
|
2022-01-27 13:14:46 +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() {
|
2022-01-27 13:14:46 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-27 13:14:46 +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
|
|
|
}
|