rename infohash_file to infohash, storage to optional preload, torrent_tracker to tracker

This commit is contained in:
yggverse 2025-07-07 11:57:54 +03:00
parent 57b246a879
commit ee78171a12
4 changed files with 48 additions and 33 deletions

67
src/preload.rs Normal file
View file

@ -0,0 +1,67 @@
use anyhow::{Result, bail};
use std::{fs, path::PathBuf, str::FromStr};
pub struct Preload(PathBuf);
impl Preload {
pub fn init(directory: &str, clear: bool) -> Result<Self> {
let p = PathBuf::from_str(directory)?;
if let Ok(t) = fs::metadata(&p) {
if t.is_file() {
bail!("Storage destination is not directory!");
}
if t.is_dir() && clear {
for i in fs::read_dir(&p)? {
let r = i?.path();
if r.is_dir() {
fs::remove_dir_all(&r)?;
} else {
fs::remove_file(&r)?;
}
}
}
}
fs::create_dir_all(&p)?;
Ok(Self(p))
}
pub fn output_folder(&self, infohash: &str, create: bool) -> Result<String> {
let mut p = PathBuf::new();
p.push(&self.0);
p.push(infohash);
if p.is_file() {
bail!("File destination is not directory!");
}
if create {
fs::create_dir_all(&p)?;
}
if !p.is_dir() {
bail!("Destination directory not exists!")
}
Ok(p.to_string_lossy().to_string())
}
pub fn absolute(&self, infohash: &str, file: &PathBuf) -> PathBuf {
let mut p = PathBuf::new();
p.push(&self.0);
p.push(infohash);
p.push(file);
p
}
/// Recursively remove all files under the `infohash` location (see rqbit#408)
pub fn cleanup(&self, infohash: &str, keep_filenames: Option<Vec<PathBuf>>) -> Result<()> {
for e in walkdir::WalkDir::new(self.output_folder(infohash, false)?) {
let e = e?;
let p = e.into_path();
if p.is_file() && keep_filenames.as_ref().is_none_or(|k| !k.contains(&p)) {
fs::remove_file(p)?;
}
}
Ok(())
}
pub fn path(&self) -> PathBuf {
self.0.clone()
}
}