mirror of
https://github.com/pumpbin/pumpbin
synced 2026-03-14 23:04:30 -07:00
refactor: use global static plugins path
This commit is contained in:
30
src/main.rs
30
src/main.rs
@@ -1,8 +1,30 @@
|
||||
#![windows_subsystem = "windows"]
|
||||
|
||||
use iced::advanced::Application;
|
||||
use pumpbin::Pumpbin;
|
||||
use std::{fs, ops::Not};
|
||||
|
||||
fn main() -> iced::Result {
|
||||
Pumpbin::run(Pumpbin::settings())
|
||||
use anyhow::anyhow;
|
||||
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(())
|
||||
}
|
||||
|
||||
@@ -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 anyhow::anyhow;
|
||||
use bincode::{decode_from_slice, encode_to_vec, Decode, Encode};
|
||||
use dirs::data_dir;
|
||||
|
||||
// 500 MiB
|
||||
const LIMIT: usize = 1024 * 1024 * 500;
|
||||
@@ -14,6 +19,8 @@ pub const BINCODE_PLUGIN_CONFIG: bincode::config::Configuration<
|
||||
> = bincode::config::standard().with_limit();
|
||||
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)]
|
||||
pub struct AesGcmPass {
|
||||
key_holder: Vec<u8>,
|
||||
@@ -194,31 +201,25 @@ pub struct Plugins(pub HashMap<String, Plugin>);
|
||||
|
||||
impl Plugins {
|
||||
pub fn reade_plugins() -> anyhow::Result<Plugins> {
|
||||
let mut plugins_path = data_dir().ok_or(anyhow::anyhow!("data_dir is none."))?;
|
||||
plugins_path.push("pumpbin");
|
||||
plugins_path.push("plugins");
|
||||
let plugins_path = CONFIG_FILE_PATH
|
||||
.get()
|
||||
.ok_or(anyhow!("Get config file path failed."))?;
|
||||
|
||||
if plugins_path.exists() && plugins_path.is_file() {
|
||||
let buf = fs::read(plugins_path)?;
|
||||
let (plugins, _) = decode_from_slice(buf.as_slice(), BINCODE_PLUGINS_CONFIG)?;
|
||||
Ok(plugins)
|
||||
} else {
|
||||
anyhow::bail!("file not exists.")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn uptade_plugins(&self) -> anyhow::Result<()> {
|
||||
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."))?;
|
||||
plugins_path.push("pumpbin");
|
||||
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)?;
|
||||
if plugins_path.is_dir() {
|
||||
fs::remove_dir(plugins_path)?;
|
||||
}
|
||||
plugins_path.push("plugins");
|
||||
|
||||
fs::write(plugins_path, buf)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user