mirror of
https://github.com/pumpbin/pumpbin
synced 2026-03-14 23:04:30 -07:00
feat(bincode): add 500MiB limit to plugin
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
use std::{fs, path::PathBuf, usize};
|
use std::{fs, path::PathBuf, usize};
|
||||||
|
|
||||||
use bincode::encode_to_vec;
|
|
||||||
use dirs::{desktop_dir, home_dir};
|
use dirs::{desktop_dir, home_dir};
|
||||||
use iced::{
|
use iced::{
|
||||||
advanced::Application,
|
advanced::Application,
|
||||||
@@ -419,9 +418,6 @@ impl Application for Maker {
|
|||||||
|
|
||||||
let plugin_name = self.plugin_name().to_owned();
|
let plugin_name = self.plugin_name().to_owned();
|
||||||
let make_plugin = async move {
|
let make_plugin = async move {
|
||||||
let buf = encode_to_vec(plugin, bincode::config::standard())
|
|
||||||
.map_err(|_| "Encode plugin failed.".to_string())?;
|
|
||||||
|
|
||||||
let file = AsyncFileDialog::new()
|
let file = AsyncFileDialog::new()
|
||||||
.set_directory(desktop_dir().unwrap_or(".".into()))
|
.set_directory(desktop_dir().unwrap_or(".".into()))
|
||||||
.set_file_name(format!("{}.b1n", plugin_name))
|
.set_file_name(format!("{}.b1n", plugin_name))
|
||||||
@@ -431,7 +427,9 @@ impl Application for Maker {
|
|||||||
.await
|
.await
|
||||||
.ok_or("Canceled plugin saving.".to_string())?;
|
.ok_or("Canceled plugin saving.".to_string())?;
|
||||||
|
|
||||||
fs::write(file.path(), buf).map_err(|_| "Write plugin failed.".to_string())?;
|
plugin
|
||||||
|
.write_plugin(file.path())
|
||||||
|
.map_err(|_| "Write plugin failed.".to_string())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,9 +2,18 @@ use std::{collections::HashMap, fmt::Display, fs, ops::Not, path::Path};
|
|||||||
|
|
||||||
use aes_gcm::{aead::Aead, Aes256Gcm, KeyInit, Nonce};
|
use aes_gcm::{aead::Aead, Aes256Gcm, KeyInit, Nonce};
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use bincode::{decode_from_reader, decode_from_slice, encode_to_vec, Decode, Encode};
|
use bincode::{decode_from_slice, encode_to_vec, Decode, Encode};
|
||||||
use dirs::data_dir;
|
use dirs::data_dir;
|
||||||
|
|
||||||
|
// 500 MiB
|
||||||
|
const LIMIT: usize = 1024 * 1024 * 500;
|
||||||
|
pub const BINCODE_PLUGIN_CONFIG: bincode::config::Configuration<
|
||||||
|
bincode::config::LittleEndian,
|
||||||
|
bincode::config::Varint,
|
||||||
|
bincode::config::Limit<LIMIT>,
|
||||||
|
> = bincode::config::standard().with_limit();
|
||||||
|
const BINCODE_PLUGINS_CONFIG: bincode::config::Configuration = bincode::config::standard();
|
||||||
|
|
||||||
#[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>,
|
||||||
@@ -168,12 +177,16 @@ impl Plugin {
|
|||||||
|
|
||||||
impl Plugin {
|
impl Plugin {
|
||||||
pub fn reade_plugin(path: &Path) -> anyhow::Result<Plugin> {
|
pub fn reade_plugin(path: &Path) -> anyhow::Result<Plugin> {
|
||||||
let config = bincode::config::standard();
|
let buf = fs::read(path)?;
|
||||||
|
let (plugin, _) = decode_from_slice(buf.as_slice(), BINCODE_PLUGIN_CONFIG)?;
|
||||||
|
Ok(plugin)
|
||||||
|
}
|
||||||
|
|
||||||
let file = fs::File::open(path)?;
|
pub fn write_plugin(&self, path: &Path) -> anyhow::Result<()> {
|
||||||
let reader = std::io::BufReader::new(file);
|
let buf = encode_to_vec(self, BINCODE_PLUGIN_CONFIG)?;
|
||||||
let decoded_plugin = decode_from_reader(reader, config)?;
|
fs::write(path, buf.as_slice())?;
|
||||||
Ok(decoded_plugin)
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,24 +195,21 @@ 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 config = bincode::config::standard();
|
|
||||||
|
|
||||||
let mut plugins_path = data_dir().ok_or(anyhow::anyhow!("data_dir is none."))?;
|
let mut plugins_path = data_dir().ok_or(anyhow::anyhow!("data_dir is none."))?;
|
||||||
plugins_path.push("pumpbin");
|
plugins_path.push("pumpbin");
|
||||||
plugins_path.push("plugins");
|
plugins_path.push("plugins");
|
||||||
|
|
||||||
if plugins_path.exists() && plugins_path.is_file() {
|
if plugins_path.exists() && plugins_path.is_file() {
|
||||||
let plugins = fs::read(plugins_path)?;
|
let buf = fs::read(plugins_path)?;
|
||||||
let (decoded_plugins, _) = decode_from_slice(plugins.as_slice(), config)?;
|
let (plugins, _) = decode_from_slice(buf.as_slice(), BINCODE_PLUGINS_CONFIG)?;
|
||||||
Ok(decoded_plugins)
|
Ok(plugins)
|
||||||
} else {
|
} else {
|
||||||
anyhow::bail!("file not exists.")
|
anyhow::bail!("file not exists.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uptade_plugins(&self) -> anyhow::Result<()> {
|
pub fn uptade_plugins(&self) -> anyhow::Result<()> {
|
||||||
let config = bincode::config::standard();
|
let buf = encode_to_vec(self, BINCODE_PLUGINS_CONFIG)?;
|
||||||
let buf = encode_to_vec(&self.0, config)?;
|
|
||||||
|
|
||||||
let mut plugins_path = data_dir().ok_or(anyhow::anyhow!("data_dir is none."))?;
|
let mut plugins_path = data_dir().ok_or(anyhow::anyhow!("data_dir is none."))?;
|
||||||
plugins_path.push("pumpbin");
|
plugins_path.push("pumpbin");
|
||||||
|
|||||||
Reference in New Issue
Block a user