233 lines
7.2 KiB
Rust
233 lines
7.2 KiB
Rust
use magic_crypt::{MagicCryptTrait, new_magic_crypt};
|
|
use std::fs::{read_to_string, write};
|
|
use std::path::PathBuf;
|
|
use std::path::Path;
|
|
use std::fs::File;
|
|
use serde_json::{Value, json};
|
|
use rust_fuzzy_search::fuzzy_search_best_n;
|
|
|
|
pub fn encrypt_n_save_file(path: PathBuf, content: String){
|
|
let mc = new_magic_crypt!("7J?VKYJib`=NIOpapW+zP8wD_#lPjP77Z)Q:4QUlH4`0rpMz7]>EIC%$RV-9*iW9AZ>Qk!OmAuH`8GzJ93xR4`KTl*-#fuCKzDfrPe&-A0?^Mz<F8IJ((oe+X,73iG", 256);
|
|
|
|
let mut encrypted: String = String::new();
|
|
|
|
for line in content.lines() {
|
|
encrypted = encrypted.to_owned() + mc.encrypt_str_to_base64(line).as_str() + "\n";
|
|
}
|
|
let res = write(path, encrypted);
|
|
match res {
|
|
Ok(_) => {},
|
|
Err(_) => println!("File save error!")
|
|
}
|
|
}
|
|
|
|
pub fn decrypt_file(path: PathBuf) -> String {
|
|
let mc = new_magic_crypt!("7J?VKYJib`=NIOpapW+zP8wD_#lPjP77Z)Q:4QUlH4`0rpMz7]>EIC%$RV-9*iW9AZ>Qk!OmAuH`8GzJ93xR4`KTl*-#fuCKzDfrPe&-A0?^Mz<F8IJ((oe+X,73iG", 256);
|
|
|
|
let mut decrypted: String = String::new();
|
|
|
|
let lines = read_to_string(path);
|
|
match lines {
|
|
Ok(lines) => {
|
|
for line in lines.lines() {
|
|
let dec_line = mc.decrypt_base64_to_string(&line);
|
|
match dec_line {
|
|
Ok(line) => {
|
|
decrypted = decrypted.to_owned() + line.as_str() + "\n";
|
|
return decrypted;
|
|
},
|
|
Err(_) => {
|
|
println!("Что-то пошло не так!");
|
|
}
|
|
}
|
|
}
|
|
return String::new();
|
|
},
|
|
Err(_) => {
|
|
println!("Что-то не так...");
|
|
return String::new();
|
|
}
|
|
}
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn check_or_create_tasks_file(app_handle: tauri::AppHandle) {
|
|
let mut path = app_handle.path_resolver().app_local_data_dir().unwrap();
|
|
path.push("ToDo");
|
|
path.push("tasks");
|
|
|
|
path.set_extension("enc");
|
|
|
|
let exist: bool = Path::new(&path).exists();
|
|
if exist{
|
|
let content = read_to_string(path);
|
|
return match content {
|
|
Ok(_) => {},
|
|
Err(_) => {
|
|
println!("Не удалось открыть конфигурационный файл!");
|
|
}
|
|
}
|
|
} else{
|
|
let path = Path::new(&path);
|
|
let prefix = path.parent().unwrap();
|
|
let file = std::fs::create_dir_all(prefix);
|
|
match file{
|
|
Ok(_) => {},
|
|
Err(err) => println!("Не удалось создать директории! \n{}", err)
|
|
}
|
|
let file = File::create(path);
|
|
match file{
|
|
Ok(_) => {
|
|
let data = String::from("{\
|
|
\"id\": 0,\
|
|
\"tasks\": {}\
|
|
}");
|
|
encrypt_n_save_file(path.into(), data);
|
|
},
|
|
Err(err) => println!("Не удалось создать файл! \n{}", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn get_tasks(app_handle: tauri::AppHandle) -> Value {
|
|
let mut path = app_handle.path_resolver().app_local_data_dir().unwrap();
|
|
path.push("ToDo");
|
|
path.push("tasks");
|
|
|
|
path.set_extension("enc");
|
|
|
|
let content = decrypt_file(path);
|
|
let data: Value = serde_json::from_str(content.as_str()).unwrap();
|
|
|
|
let res = &data["tasks"];
|
|
|
|
return res.clone();
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn search_tasks(app_handle: tauri::AppHandle, value: String) -> Value {
|
|
let mut path = app_handle.path_resolver().app_local_data_dir().unwrap();
|
|
path.push("ToDo");
|
|
path.push("tasks");
|
|
|
|
path.set_extension("enc");
|
|
|
|
let content = decrypt_file(path);
|
|
let data: Value = serde_json::from_str(content.as_str()).unwrap();
|
|
|
|
let mut tasks = vec![];
|
|
|
|
let binding = data.clone();
|
|
for (_key, value) in binding["tasks"].as_object().unwrap() {
|
|
tasks.push(value["name"].as_str().unwrap().to_lowercase());
|
|
}
|
|
|
|
let tasks_str: Vec<&str> = tasks.iter().map(|s| &**s).collect();
|
|
|
|
let n : usize = 5;
|
|
let binding = value.to_lowercase();
|
|
let res : Vec<(&str, f32)> = fuzzy_search_best_n(&binding, &tasks_str, n);
|
|
|
|
let mut result = json!({});
|
|
|
|
let res1 = res.clone();
|
|
let binding1 = data.clone();
|
|
for (name, score) in res1 {
|
|
if score != 0.0 {
|
|
for (key, value) in binding1["tasks"].as_object().unwrap() {
|
|
if value.get("name").unwrap().as_str().unwrap().to_lowercase().as_str() == name {
|
|
result[key] = binding1["tasks"].get(key).unwrap().clone();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn add_task(app_handle: tauri::AppHandle, date: String, time: String, name: String, description: String, priority: String) {
|
|
let mut path = app_handle.path_resolver().app_local_data_dir().unwrap();
|
|
path.push("ToDo");
|
|
path.push("tasks");
|
|
|
|
path.set_extension("enc");
|
|
|
|
let content = decrypt_file(path.clone());
|
|
let mut data: Value = serde_json::from_str(content.as_str()).unwrap();
|
|
|
|
let pending = data.clone();
|
|
let id = &pending["id"];
|
|
|
|
let task = json!({
|
|
"id": id.to_string(),
|
|
"date": date,
|
|
"time": time,
|
|
"name": name,
|
|
"description": description,
|
|
"priority": priority,
|
|
"completed": false
|
|
});
|
|
|
|
data["id"] = (id.as_i64().unwrap() + 1).into();
|
|
|
|
data["tasks"][id.to_string()] = task;
|
|
encrypt_n_save_file(path.into(), data.to_string());
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn edit_task(app_handle: tauri::AppHandle, id_task: String, name: String, description: String, priority: String) {
|
|
let mut path = app_handle.path_resolver().app_local_data_dir().unwrap();
|
|
path.push("ToDo");
|
|
path.push("tasks");
|
|
|
|
path.set_extension("enc");
|
|
|
|
let content = decrypt_file(path.clone());
|
|
let mut data: Value = serde_json::from_str(content.as_str()).unwrap();
|
|
data["tasks"][id_task.clone()]["name"] = json!(name);
|
|
data["tasks"][id_task.clone()]["description"] = json!(description);
|
|
data["tasks"][id_task]["priority"] = json!(priority);
|
|
|
|
encrypt_n_save_file(path.into(), data.to_string());
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn delete_task(app_handle: tauri::AppHandle, id_task: String) {
|
|
let mut path = app_handle.path_resolver().app_local_data_dir().unwrap();
|
|
path.push("ToDo");
|
|
path.push("tasks");
|
|
|
|
path.set_extension("enc");
|
|
|
|
let content = decrypt_file(path.clone());
|
|
let mut data: Value = serde_json::from_str(content.as_str()).unwrap();
|
|
|
|
let tasks = data["tasks"].as_object_mut().unwrap();
|
|
let binding = tasks.clone();
|
|
for (key, value) in binding{
|
|
if id_task == value["id"] {
|
|
tasks.remove(&key);
|
|
break;
|
|
}
|
|
}
|
|
data["tasks"] = json!(tasks);
|
|
encrypt_n_save_file(path.into(), data.to_string());
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn set_task_field(app_handle: tauri::AppHandle, id_task: String, field: String, value: String) {
|
|
let mut path = app_handle.path_resolver().app_local_data_dir().unwrap();
|
|
path.push("ToDo");
|
|
path.push("tasks");
|
|
|
|
path.set_extension("enc");
|
|
|
|
let content = decrypt_file(path.clone());
|
|
let mut data: Value = serde_json::from_str(content.as_str()).unwrap();
|
|
data["tasks"][id_task][field] = json!(value);
|
|
|
|
encrypt_n_save_file(path.into(), data.to_string());
|
|
} |