mirror of
https://github.com/YGGverse/aquatic-crawler.git
synced 2026-04-01 01:25:36 +00:00
separate torrent storage features
This commit is contained in:
parent
25a226eb0f
commit
b30be0e9f8
5 changed files with 56 additions and 48 deletions
32
src/torrent.rs
Normal file
32
src/torrent.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
use anyhow::Result;
|
||||
use std::{fs, io::Write, path::PathBuf, str::FromStr};
|
||||
|
||||
pub struct Torrent {
|
||||
storage: PathBuf,
|
||||
}
|
||||
|
||||
impl Torrent {
|
||||
pub fn init(path: &str) -> Result<Self> {
|
||||
Ok(Self {
|
||||
storage: PathBuf::from_str(path)?.canonicalize()?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn persist(&self, infohash: &str, data: &[u8]) -> Result<Option<PathBuf>> {
|
||||
Ok(if self.path(infohash).exists() {
|
||||
None
|
||||
} else {
|
||||
let p = self.path(infohash);
|
||||
let mut f = fs::File::create(&p)?;
|
||||
f.write_all(data)?;
|
||||
Some(p)
|
||||
})
|
||||
}
|
||||
|
||||
fn path(&self, infohash: &str) -> PathBuf {
|
||||
let mut p = PathBuf::new();
|
||||
p.push(&self.storage);
|
||||
p.push(format!("{infohash}.torrent"));
|
||||
p
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue