reorganize Torrent structs, implement parser, isolate system errors output, remove file listing from rss feed (will be moved to the torrent page)

This commit is contained in:
yggverse 2025-08-09 01:27:56 +03:00
parent dc7585311e
commit 9291328401
5 changed files with 156 additions and 155 deletions

79
src/torrent.rs Normal file
View file

@ -0,0 +1,79 @@
mod file;
use chrono::{DateTime, Utc};
use file::File;
use librqbit_core::{torrent_metainfo, torrent_metainfo::TorrentMetaV1Owned};
use rocket::serde::Serialize;
#[derive(Clone, Debug, Serialize)]
#[serde(crate = "rocket::serde")]
pub struct Torrent {
pub announce: Option<String>,
pub comment: Option<String>,
pub created_by: Option<String>,
pub creation_date: Option<DateTime<Utc>>,
pub files: Option<Vec<File>>,
pub info_hash: String,
pub is_private: bool,
pub length: Option<u64>,
pub name: Option<String>,
pub publisher_url: Option<String>,
pub publisher: Option<String>,
pub size: u64,
/// File (modified)
pub time: DateTime<Utc>,
}
impl Torrent {
pub fn from_storage(bytes: &[u8], time: DateTime<Utc>) -> Result<Self, String> {
let i: TorrentMetaV1Owned =
torrent_metainfo::torrent_from_bytes(bytes).map_err(|e| e.to_string())?;
Ok(Torrent {
info_hash: i.info_hash.as_string(),
announce: i.announce.map(|a| a.to_string()),
comment: i.comment.map(|c| c.to_string()),
created_by: i.created_by.map(|c| c.to_string()),
creation_date: i
.creation_date
.map(|t| DateTime::from_timestamp_nanos(t as i64)),
size: i.info.length.unwrap_or_default()
+ i.info
.files
.as_ref()
.map(|files| files.iter().map(|f| f.length).sum::<u64>())
.unwrap_or_default(),
files: i.info.files.map(|files| {
let mut b = Vec::with_capacity(files.len());
for f in files.iter() {
b.push(File {
name: String::from_utf8(
f.path
.iter()
.enumerate()
.flat_map(|(n, b)| {
if n == 0 {
b.0.to_vec()
} else {
let mut p = vec![b'/'];
p.extend(b.0.to_vec());
p
}
})
.collect(),
)
.ok(),
length: f.length,
})
}
b.sort_by(|a, b| a.name.cmp(&b.name)); // @TODO optional
b
}),
publisher_url: i.publisher_url.map(|u| u.to_string()),
publisher: i.publisher.map(|p| p.to_string()),
is_private: i.info.private,
length: i.info.length,
name: i.info.name.map(|e| e.to_string()),
time,
})
}
}