move config members to preload struct

This commit is contained in:
yggverse 2025-07-07 12:51:04 +03:00
parent c34f435153
commit 43d94ee9b6
2 changed files with 31 additions and 7 deletions

View file

@ -31,9 +31,17 @@ async fn main() -> Result<()> {
let config = config::Config::parse(); let config = config::Config::parse();
let debug = Debug::init(&config.debug)?; let debug = Debug::init(&config.debug)?;
let peers = peers::Peers::init(&config.initial_peer)?; let peers = peers::Peers::init(&config.initial_peer)?;
let preload = config let preload = config.preload.map(|ref p| {
.preload Preload::init(
.map(|ref p| Preload::init(p, config.preload_regex, config.clear).unwrap()); p,
config.preload_regex,
config.preload_max_filecount,
config.preload_max_filesize,
config.preload_total_size,
config.clear,
)
.unwrap()
});
let trackers = Trackers::init(&config.tracker)?; let trackers = Trackers::init(&config.tracker)?;
let torrent = config.export_torrents.map(|p| Torrent::init(&p).unwrap()); let torrent = config.export_torrents.map(|p| Torrent::init(&p).unwrap());
let session = librqbit::Session::new_with_opts( let session = librqbit::Session::new_with_opts(
@ -129,7 +137,7 @@ async fn main() -> Result<()> {
if let Some(ref p) = preload { if let Some(ref p) = preload {
for (id, info) in m.file_infos.iter().enumerate() { for (id, info) in m.file_infos.iter().enumerate() {
if p.matches(info.relative_filename.to_str().unwrap()) { if p.matches(info.relative_filename.to_str().unwrap()) {
if config.preload_max_filesize.is_some_and( if p.max_filesize.is_some_and(
|limit| only_files_size + info.len > limit, |limit| only_files_size + info.len > limit,
) { ) {
debug.info(&format!( debug.info(&format!(
@ -137,7 +145,7 @@ async fn main() -> Result<()> {
)); ));
break; break;
} }
if config.preload_max_filecount.is_some_and( if p.max_filecount.is_some_and(
|limit| only_files.len() + 1 > limit, |limit| only_files.len() + 1 > limit,
) { ) {
debug.info(&format!( debug.info(&format!(
@ -216,7 +224,10 @@ async fn main() -> Result<()> {
} }
rss.commit()? rss.commit()?
} }
if config.preload_total_size.is_some_and(|s| index.nodes() > s) { if preload
.as_ref()
.is_some_and(|p| p.total_size.is_some_and(|s| index.nodes() > s))
{
panic!("Preload content size {} bytes reached!", 0) panic!("Preload content size {} bytes reached!", 0)
} }
debug.info(&format!( debug.info(&format!(

View file

@ -4,11 +4,21 @@ use std::{fs, path::PathBuf, str::FromStr};
pub struct Preload { pub struct Preload {
path: PathBuf, path: PathBuf,
pub max_filecount: Option<usize>,
pub max_filesize: Option<u64>,
pub total_size: Option<u64>,
pub regex: Option<Regex>, pub regex: Option<Regex>,
} }
impl Preload { impl Preload {
pub fn init(directory: &str, regex: Option<String>, clear: bool) -> Result<Self> { pub fn init(
directory: &str,
regex: Option<String>,
max_filecount: Option<usize>,
max_filesize: Option<u64>,
total_size: Option<u64>,
clear: bool,
) -> Result<Self> {
let path = PathBuf::from_str(directory)?; let path = PathBuf::from_str(directory)?;
if let Ok(t) = fs::metadata(&path) { if let Ok(t) = fs::metadata(&path) {
if t.is_file() { if t.is_file() {
@ -27,8 +37,11 @@ impl Preload {
} }
fs::create_dir_all(&path)?; fs::create_dir_all(&path)?;
Ok(Self { Ok(Self {
max_filecount,
max_filesize,
path, path,
regex: regex.map(|r| Regex::new(&r).unwrap()), regex: regex.map(|r| Regex::new(&r).unwrap()),
total_size,
}) })
} }