separate torrent storage features

This commit is contained in:
yggverse 2025-07-07 01:43:15 +03:00
parent 25a226eb0f
commit b30be0e9f8
5 changed files with 56 additions and 48 deletions

View file

@ -1,5 +1,5 @@
use anyhow::{Result, bail};
use std::{fs, io::Write, path::PathBuf, str::FromStr};
use std::{fs, path::PathBuf, str::FromStr};
pub struct Storage(PathBuf);
@ -25,18 +25,6 @@ impl Storage {
Ok(Self(p))
}
pub fn torrent_exists(&self, infohash: &str) -> bool {
fs::metadata(self.torrent(infohash))
.is_ok_and(|p| p.is_file() || p.is_dir() || p.is_symlink())
}
pub fn save_torrent(&self, infohash: &str, bytes: &[u8]) -> Result<PathBuf> {
let p = self.torrent(infohash);
let mut f = fs::File::create(&p)?;
f.write_all(bytes)?;
Ok(p)
}
pub fn output_folder(&self, infohash: &str, create: bool) -> Result<String> {
let mut p = PathBuf::new();
p.push(&self.0);
@ -76,11 +64,4 @@ impl Storage {
pub fn path(&self) -> PathBuf {
self.0.clone()
}
fn torrent(&self, infohash: &str) -> PathBuf {
let mut p = PathBuf::new();
p.push(&self.0);
p.push(format!("{infohash}.torrent"));
p
}
}