refactor: use global static plugins path

This commit is contained in:
b1n
2024-06-27 22:02:01 +08:00
parent 291d6b658d
commit 23b6a6dbd6
2 changed files with 47 additions and 24 deletions

View File

@@ -1,8 +1,30 @@
#![windows_subsystem = "windows"] #![windows_subsystem = "windows"]
use iced::advanced::Application; use std::{fs, ops::Not};
use pumpbin::Pumpbin;
fn main() -> iced::Result { use anyhow::anyhow;
Pumpbin::run(Pumpbin::settings()) use dirs::data_dir;
use iced::advanced::Application;
use pumpbin::{plugin::CONFIG_FILE_PATH, Pumpbin};
fn main() -> anyhow::Result<()> {
let mut config_path = data_dir().ok_or(anyhow!("Get data_dir failed."))?;
config_path.push("PumpBin");
config_path.push("plugins");
if let Some(parent) = config_path.parent() {
if parent.exists().not() {
fs::create_dir_all(parent)?;
} else if parent.is_dir().not() {
fs::remove_file(parent)?;
fs::create_dir_all(parent)?;
}
}
CONFIG_FILE_PATH
.set(config_path)
.map_err(|_| anyhow!("Set CONFIG_FILE_PATH failed."))?;
Pumpbin::run(Pumpbin::settings())?;
Ok(())
} }

View File

@@ -1,9 +1,14 @@
use std::{collections::HashMap, fmt::Display, fs, ops::Not, path::Path}; use std::{
collections::HashMap,
fmt::Display,
fs,
path::{Path, PathBuf},
sync::OnceLock,
};
use aes_gcm::{aead::Aead, Aes256Gcm, Key, KeyInit, Nonce}; use aes_gcm::{aead::Aead, Aes256Gcm, Key, KeyInit, Nonce};
use anyhow::anyhow; use anyhow::anyhow;
use bincode::{decode_from_slice, encode_to_vec, Decode, Encode}; use bincode::{decode_from_slice, encode_to_vec, Decode, Encode};
use dirs::data_dir;
// 500 MiB // 500 MiB
const LIMIT: usize = 1024 * 1024 * 500; const LIMIT: usize = 1024 * 1024 * 500;
@@ -14,6 +19,8 @@ pub const BINCODE_PLUGIN_CONFIG: bincode::config::Configuration<
> = bincode::config::standard().with_limit(); > = bincode::config::standard().with_limit();
const BINCODE_PLUGINS_CONFIG: bincode::config::Configuration = bincode::config::standard(); const BINCODE_PLUGINS_CONFIG: bincode::config::Configuration = bincode::config::standard();
pub static CONFIG_FILE_PATH: OnceLock<PathBuf> = OnceLock::new();
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)] #[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub struct AesGcmPass { pub struct AesGcmPass {
key_holder: Vec<u8>, key_holder: Vec<u8>,
@@ -194,31 +201,25 @@ pub struct Plugins(pub HashMap<String, Plugin>);
impl Plugins { impl Plugins {
pub fn reade_plugins() -> anyhow::Result<Plugins> { pub fn reade_plugins() -> anyhow::Result<Plugins> {
let mut plugins_path = data_dir().ok_or(anyhow::anyhow!("data_dir is none."))?; let plugins_path = CONFIG_FILE_PATH
plugins_path.push("pumpbin"); .get()
plugins_path.push("plugins"); .ok_or(anyhow!("Get config file path failed."))?;
if plugins_path.exists() && plugins_path.is_file() { let buf = fs::read(plugins_path)?;
let buf = fs::read(plugins_path)?; let (plugins, _) = decode_from_slice(buf.as_slice(), BINCODE_PLUGINS_CONFIG)?;
let (plugins, _) = decode_from_slice(buf.as_slice(), BINCODE_PLUGINS_CONFIG)?; Ok(plugins)
Ok(plugins)
} else {
anyhow::bail!("file not exists.")
}
} }
pub fn uptade_plugins(&self) -> anyhow::Result<()> { pub fn uptade_plugins(&self) -> anyhow::Result<()> {
let buf = encode_to_vec(self, BINCODE_PLUGINS_CONFIG)?; let buf = encode_to_vec(self, BINCODE_PLUGINS_CONFIG)?;
let plugins_path = CONFIG_FILE_PATH
.get()
.ok_or(anyhow!("Get config file path failed."))?;
let mut plugins_path = data_dir().ok_or(anyhow::anyhow!("data_dir is none."))?; if plugins_path.is_dir() {
plugins_path.push("pumpbin"); fs::remove_dir(plugins_path)?;
if plugins_path.exists().not() {
fs::create_dir_all(&plugins_path)?;
} else if plugins_path.exists() && plugins_path.is_dir().not() {
fs::remove_file(&plugins_path)?;
fs::create_dir_all(&plugins_path)?;
} }
plugins_path.push("plugins");
fs::write(plugins_path, buf)?; fs::write(plugins_path, buf)?;
Ok(()) Ok(())