Skip to content

Commit

Permalink
feat: Removed unnecessary async
Browse files Browse the repository at this point in the history
  • Loading branch information
1101-1 committed Jan 30, 2024
1 parent baf72bb commit 4977bb4
Show file tree
Hide file tree
Showing 15 changed files with 25 additions and 23 deletions.
1 change: 0 additions & 1 deletion src/bot/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::types::state::{HandlerResult, MyDialogue, State};

use super::file_info::get_history::send_history;


#[derive(BotCommands, Clone)]
#[command(
rename_rule = "lowercase",
Expand Down
6 changes: 3 additions & 3 deletions src/bot/file_actions/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ pub async fn download_file(
let mut dst = File::create(&file_path).await?;
bot.download_file(&telegram_file.path, &mut dst).await?;

let aes_key = set_aes_key().await;
let encoded_key = convert_aes_to_base64(aes_key).await;
let aes_key = set_aes_key();
let encoded_key = convert_aes_to_base64(aes_key);

let mut open_file = File::open(&file_path).await?;
let mut file_data = Vec::new();
open_file.read_to_end(&mut file_data).await?;

let encrypted_data = match encrypt_data(&file_data, aes_key).await {
let encrypted_data = match encrypt_data(&file_data, aes_key) {
Ok(encrypted_data) => encrypted_data,
Err(_err) => {
bot.send_message(msg.chat.id, "Unable to crypt file. Try again")
Expand Down
2 changes: 1 addition & 1 deletion src/bot/file_actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod download;
pub mod get_file;
pub mod send_file;
pub mod send_file;
4 changes: 2 additions & 2 deletions src/bot/file_actions/send_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub async fn send_file(
}
};
if let Some(key) = aes_key {
let key_bytes = match convert_base64_to_aes(key).await {
let key_bytes = match convert_base64_to_aes(key) {
Ok(key) => key,
Err(_err) => {
bot.send_message(msg.chat.id, "Invalid key. Try again")
Expand All @@ -41,7 +41,7 @@ pub async fn send_file(
let mut file_data = Vec::new();
file.read_to_end(&mut file_data).await?;

let data = decrypt_data(file_data, key_bytes).await.unwrap();
let data = decrypt_data(file_data, key_bytes).unwrap();

bot.send_document(
msg.chat.id,
Expand Down
5 changes: 4 additions & 1 deletion src/bot/file_info/get_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use teloxide::{requests::Requester, types::Message, Bot};

use crate::{types::state::{HandlerResult, MyDialogue, State}, bot::file_actions::send_file::send_file};
use crate::{
bot::file_actions::send_file::send_file,
types::state::{HandlerResult, MyDialogue, State},
};

pub async fn get_file_info(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
if let Some(text) = msg.text() {
Expand Down
2 changes: 1 addition & 1 deletion src/bot/file_info/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod encryption_type;
pub mod get_history;
pub mod get_info;
pub mod get_info;
2 changes: 1 addition & 1 deletion src/bot/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod command;
pub mod file_actions;
pub mod file_info;
pub mod file_info;
2 changes: 1 addition & 1 deletion src/crypt/aes_key.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rand::thread_rng;
use rand_core::RngCore;

pub async fn set_aes_key() -> [u8; 32] {
pub fn set_aes_key() -> [u8; 32] {
let mut gen_aes_key = [0u8; 32];
thread_rng().fill_bytes(&mut gen_aes_key);
gen_aes_key
Expand Down
4 changes: 2 additions & 2 deletions src/crypt/base64_convert.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use base64::{engine::general_purpose, Engine};

pub async fn convert_base64_to_aes(aes_key: String) -> Result<[u8; 32], tokio::io::Error> {
pub fn convert_base64_to_aes(aes_key: String) -> Result<[u8; 32], tokio::io::Error> {
let key_vec = match general_purpose::URL_SAFE_NO_PAD.decode(aes_key) {
Ok(key) => key,
Err(_err) => {
Expand All @@ -23,6 +23,6 @@ pub async fn convert_base64_to_aes(aes_key: String) -> Result<[u8; 32], tokio::i
Ok(key_array)
}

pub async fn convert_aes_to_base64(aes_bytes: [u8; 32]) -> String {
pub fn convert_aes_to_base64(aes_bytes: [u8; 32]) -> String {
general_purpose::URL_SAFE_NO_PAD.encode(aes_bytes)
}
2 changes: 1 addition & 1 deletion src/crypt/decryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use aes::{
};
use aes_gcm::KeyInit;

pub async fn decrypt_data(buf: Vec<u8>, aes_key: [u8; 32]) -> Result<Vec<u8>, tokio::io::Error> {
pub fn decrypt_data(buf: Vec<u8>, aes_key: [u8; 32]) -> Result<Vec<u8>, tokio::io::Error> {
let cipher = Aes256::new(&GenericArray::from_slice(&aes_key));
let mut decrypted_data = buf.to_vec().to_owned();

Expand Down
2 changes: 1 addition & 1 deletion src/crypt/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use aes::{
};
use aes_gcm::KeyInit;

pub async fn encrypt_data(data: &[u8], aes_key: [u8; 32]) -> Result<Vec<u8>, tokio::io::Error> {
pub fn encrypt_data(data: &[u8], aes_key: [u8; 32]) -> Result<Vec<u8>, tokio::io::Error> {
let cipher = Aes256::new(&GenericArray::from_slice(&aes_key));
let mut padded_data = data.to_vec();
let pad_len = 16 - (data.len() % 16);
Expand Down
2 changes: 1 addition & 1 deletion src/db/checkers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub async fn find_dublicate(short_url: String) -> String {
.await
.unwrap()
{
find_dublicate(generate_short_path_url().await).await;
find_dublicate(generate_short_path_url()).await;
}

short_url
Expand Down
2 changes: 1 addition & 1 deletion src/db/insert_to_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub async fn insert_main_info(
.await
.unwrap()
{
short_path_url = find_dublicate(generate_short_path_url().await).await;
short_path_url = find_dublicate(generate_short_path_url()).await;
};

let document = doc! {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/generate_uuid.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub async fn generate_uuid_v4() -> String {
pub fn generate_uuid_v4() -> String {
uuid::Uuid::new_v4().to_string()
}
10 changes: 5 additions & 5 deletions src/tools/short_url.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use rand::Rng;

const CHARS: [char; 52] = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];

const NUMBERS: [char; 10] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];

pub async fn generate_short_path_url() -> String {
pub fn generate_short_path_url() -> String {
let mut short_url: Vec<char> = Vec::with_capacity(8);

for _ in 0..8 {
Expand Down

0 comments on commit 4977bb4

Please sign in to comment.