From 60b0dc0d84472a715362f3b3873825195acdad77 Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Thu, 10 Mar 2022 22:02:03 +0100 Subject: [PATCH] read commit description - webhook: added url in title - cli: change image domain to your own (set in config) - if the configuration file cannot be open, ask the user whether to overwrite the file instead of overwriting it without asking - logger: max_level_debug in debug binary --- CHANGELOG.md | 5 +++++ Cargo.toml | 2 +- config.toml | 1 + src/cli/upload_image.rs | 11 ++++++++--- src/cli/webhook.rs | 8 +++----- src/config/mod.rs | 1 + src/config/toml.rs | 43 +++++++++++++++++++++++++++-------------- 7 files changed, 48 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98098cc..8192968 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### CLI +- webhook: added url in title +- cli: change image domain to your own (set in config) +- if the configuration file cannot be open, ask the user whether to overwrite the file instead of overwriting it without asking +- logger: set `max_level_debug` in debug binary ## [0.5.1] - 2022-03-08 ### Cli diff --git a/Cargo.toml b/Cargo.toml index f77fc06..6d59a75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,7 +44,7 @@ features = ["derive", "cargo", "std"] [dependencies.log] version = "0.4.14" -features = ["release_max_level_info"] +features = ["release_max_level_info", "max_level_debug"] [dependencies.simple_logger] version = "2.1.0" diff --git a/config.toml b/config.toml index cc6cf05..d4c616d 100644 --- a/config.toml +++ b/config.toml @@ -1,5 +1,6 @@ [imgur] id = '3e3ce0d7ac14d56' +image_cdn = 'i.imgur.com' [notification] enabled = true diff --git a/src/cli/upload_image.rs b/src/cli/upload_image.rs index 807ec9f..445c480 100644 --- a/src/cli/upload_image.rs +++ b/src/cli/upload_image.rs @@ -20,6 +20,8 @@ macro_rules! notify ( pub async fn upload_image(client: ImgurClient, path: &str) { let mut image: String = path.to_string(); + let config = toml::parse(); + if Path::new(path).exists() { let bytes = fs_read(path) .map_err(|err| err.to_string()) @@ -29,7 +31,7 @@ pub async fn upload_image(client: ImgurClient, path: &str) { panic!("{path} is not a url") } - let i = upload_img(client, &image).await.unwrap_or_else(|err| { + let mut i = upload_img(client, &image).await.unwrap_or_else(|err| { notify!(Notification::new() .summary("Error!") .body(&format!("Error: {}", &err.to_string())) @@ -37,14 +39,17 @@ pub async fn upload_image(client: ImgurClient, path: &str) { panic!("{}", err) }); + + if config.imgur.image_cdn != "i.imgur.com" { + i.data.link = i.data.link.replace("i.imgur.com", "cdn.magicuser.cf") + } + print_image_info(i.clone()); let body = format!("Uploaded {}", i.data.link); notify!(Notification::new().summary("Imgurs").body(&body)); - let config = toml::parse(); - if config.clipboard.enabled { set_clipboard(i.data.link.clone()) } diff --git a/src/cli/webhook.rs b/src/cli/webhook.rs index f9f2133..6b6d2fa 100644 --- a/src/cli/webhook.rs +++ b/src/cli/webhook.rs @@ -10,11 +10,11 @@ pub async fn send_discord_webhook( let url = toml::parse().discord_webhook.uri; let client: WebhookClient = WebhookClient::new(&url); - let webhook = client + client .send(|message| { message.username("Imgurs").embed(|embed| { embed - .title("Imgurs") + .title(&link) .description(&format!("Delete Hash ||{deletehash}||")) .image(&link) .footer( @@ -26,7 +26,5 @@ pub async fn send_discord_webhook( ) }) }) - .await; - - webhook + .await } diff --git a/src/config/mod.rs b/src/config/mod.rs index 713c677..309490e 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -13,6 +13,7 @@ pub struct Config { #[derive(Debug, Deserialize)] pub struct ConfigImgur { pub id: String, + pub image_cdn: String, } #[derive(Debug, Deserialize)] diff --git a/src/config/toml.rs b/src/config/toml.rs index f1d6ab2..ae23c1e 100644 --- a/src/config/toml.rs +++ b/src/config/toml.rs @@ -1,11 +1,12 @@ use super::Config; use anyhow::Error; +use colored::Colorize; use dirs::config_dir; use log::warn; use std::{ fs::{create_dir_all, read_to_string, File}, - io::Write as _, + io::{Write as _, self}, path::Path, }; use toml::from_str as toml_from_str; @@ -13,25 +14,39 @@ use toml::from_str as toml_from_str; const CONFIG_DIR: &str = "/imgurs/config.toml"; pub fn parse() -> Config { - toml().unwrap_or_else(|e| { - warn!("Parse toml config: {e}! Creating config file..."); + toml().unwrap_or_else(|err| { + let mut stdout = std::io::stdout(); - let default_config = include_str!(concat!("../../config.toml")); + write!(stdout, "{}", "The configuration file could not be opened. Do you want to create/overwrite with DEFAULT values? (Y/n): ".yellow()).unwrap(); + stdout.flush().unwrap(); - let sys_config_dir = config_dir().expect("find config dir"); - let config_dir = format!("{}{CONFIG_DIR}", sys_config_dir.to_string_lossy()); - let config_path = Path::new(&config_dir); + let mut value = String::new(); + io::stdin() + .read_line(&mut value) + .expect("failed to read line"); - let prefix = config_path.parent().unwrap(); - create_dir_all(prefix).expect("create config dir"); + if value.to_lowercase() != "n\n" { + warn!("Parse toml config: {err}! Creating config file..."); - let mut file_ref = File::create(config_path).expect("create failed"); + let default_config = include_str!(concat!("../../config.toml")); - file_ref - .write_all(default_config.as_bytes()) - .expect("failed write default config to file"); + let sys_config_dir = config_dir().expect("find config dir"); + let config_dir = format!("{}{CONFIG_DIR}", sys_config_dir.to_string_lossy()); + let config_path = Path::new(&config_dir); - toml().expect("parse toml config") + let prefix = config_path.parent().unwrap(); + create_dir_all(prefix).expect("create config dir"); + + let mut file_ref = File::create(config_path).expect("create failed"); + + file_ref + .write_all(default_config.as_bytes()) + .expect("failed write default config to file"); + + toml().expect("parse toml config") + } else { + panic!("New config creation cancelled!") + } }) }