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
This commit is contained in:
MedzikUser 2022-03-10 22:02:03 +01:00
parent 9b813457e7
commit 60b0dc0d84
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
7 changed files with 48 additions and 23 deletions

View File

@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
<!-- next-header --> <!-- next-header -->
## [Unreleased] ## [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 ## [0.5.1] - 2022-03-08
### Cli ### Cli

View File

@ -44,7 +44,7 @@ features = ["derive", "cargo", "std"]
[dependencies.log] [dependencies.log]
version = "0.4.14" version = "0.4.14"
features = ["release_max_level_info"] features = ["release_max_level_info", "max_level_debug"]
[dependencies.simple_logger] [dependencies.simple_logger]
version = "2.1.0" version = "2.1.0"

View File

@ -1,5 +1,6 @@
[imgur] [imgur]
id = '3e3ce0d7ac14d56' id = '3e3ce0d7ac14d56'
image_cdn = 'i.imgur.com'
[notification] [notification]
enabled = true enabled = true

View File

@ -20,6 +20,8 @@ macro_rules! notify (
pub async fn upload_image(client: ImgurClient, path: &str) { pub async fn upload_image(client: ImgurClient, path: &str) {
let mut image: String = path.to_string(); let mut image: String = path.to_string();
let config = toml::parse();
if Path::new(path).exists() { if Path::new(path).exists() {
let bytes = fs_read(path) let bytes = fs_read(path)
.map_err(|err| err.to_string()) .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") 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() notify!(Notification::new()
.summary("Error!") .summary("Error!")
.body(&format!("Error: {}", &err.to_string())) .body(&format!("Error: {}", &err.to_string()))
@ -37,14 +39,17 @@ pub async fn upload_image(client: ImgurClient, path: &str) {
panic!("{}", err) 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()); print_image_info(i.clone());
let body = format!("Uploaded {}", i.data.link); let body = format!("Uploaded {}", i.data.link);
notify!(Notification::new().summary("Imgurs").body(&body)); notify!(Notification::new().summary("Imgurs").body(&body));
let config = toml::parse();
if config.clipboard.enabled { if config.clipboard.enabled {
set_clipboard(i.data.link.clone()) set_clipboard(i.data.link.clone())
} }

View File

@ -10,11 +10,11 @@ pub async fn send_discord_webhook(
let url = toml::parse().discord_webhook.uri; let url = toml::parse().discord_webhook.uri;
let client: WebhookClient = WebhookClient::new(&url); let client: WebhookClient = WebhookClient::new(&url);
let webhook = client client
.send(|message| { .send(|message| {
message.username("Imgurs").embed(|embed| { message.username("Imgurs").embed(|embed| {
embed embed
.title("Imgurs") .title(&link)
.description(&format!("Delete Hash ||{deletehash}||")) .description(&format!("Delete Hash ||{deletehash}||"))
.image(&link) .image(&link)
.footer( .footer(
@ -26,7 +26,5 @@ pub async fn send_discord_webhook(
) )
}) })
}) })
.await; .await
webhook
} }

View File

@ -13,6 +13,7 @@ pub struct Config {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct ConfigImgur { pub struct ConfigImgur {
pub id: String, pub id: String,
pub image_cdn: String,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]

View File

@ -1,11 +1,12 @@
use super::Config; use super::Config;
use anyhow::Error; use anyhow::Error;
use colored::Colorize;
use dirs::config_dir; use dirs::config_dir;
use log::warn; use log::warn;
use std::{ use std::{
fs::{create_dir_all, read_to_string, File}, fs::{create_dir_all, read_to_string, File},
io::Write as _, io::{Write as _, self},
path::Path, path::Path,
}; };
use toml::from_str as toml_from_str; 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"; const CONFIG_DIR: &str = "/imgurs/config.toml";
pub fn parse() -> Config { pub fn parse() -> Config {
toml().unwrap_or_else(|e| { toml().unwrap_or_else(|err| {
warn!("Parse toml config: {e}! Creating config file..."); 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 mut value = String::new();
let config_dir = format!("{}{CONFIG_DIR}", sys_config_dir.to_string_lossy()); io::stdin()
let config_path = Path::new(&config_dir); .read_line(&mut value)
.expect("failed to read line");
let prefix = config_path.parent().unwrap(); if value.to_lowercase() != "n\n" {
create_dir_all(prefix).expect("create config dir"); 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 let sys_config_dir = config_dir().expect("find config dir");
.write_all(default_config.as_bytes()) let config_dir = format!("{}{CONFIG_DIR}", sys_config_dir.to_string_lossy());
.expect("failed write default config to file"); 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!")
}
}) })
} }