implement torrent files index option

This commit is contained in:
yggverse 2025-07-09 02:33:02 +03:00
parent d0d469ee78
commit 29ff0b52cc
5 changed files with 95 additions and 25 deletions

View file

@ -1,7 +1,5 @@
use chrono::{DateTime, Utc};
const NAME_MAX_LEN: usize = 125; // + 3 bytes for `...` offset @TODO optional
/// The `Index` value
pub struct Value {
pub time: DateTime<Utc>,
@ -9,15 +7,22 @@ pub struct Value {
// Isolate by applying internal filter on value set
size: Option<u64>,
name: Option<String>,
list: Option<Vec<(String, u64)>>,
}
impl Value {
/// Create new `Self` with current timestamp
pub fn new(node: u64, size: Option<u64>, name: Option<String>) -> Self {
pub fn new(
node: u64,
size: Option<u64>,
name: Option<String>,
list: Option<Vec<(String, u64)>>,
) -> Self {
Self {
time: Utc::now(),
node,
size,
list: filter_list(list),
name: filter_name(name),
}
}
@ -25,23 +30,38 @@ impl Value {
pub fn name(&self) -> Option<&String> {
self.name.as_ref()
}
/// Get reference to the safely constructed files `list` member
pub fn list(&self) -> Option<&Vec<(String, u64)>> {
self.list.as_ref()
}
/// Get reference to the safely constructed `length` member
pub fn size(&self) -> Option<u64> {
self.size
}
}
/// Prevent unexpected memory usage on store values from unknown source
fn filter_name(value: Option<String>) -> Option<String> {
value.map(|v| {
if v.len() > NAME_MAX_LEN {
format!("{}...", sanitize(&v[..NAME_MAX_LEN]))
} else {
v
}
value.map(crop)
}
fn filter_list(value: Option<Vec<(String, u64)>>) -> Option<Vec<(String, u64)>> {
value.map(|f| {
f.into_iter()
.map(|(n, l)| (crop(sanitize(&n)), l))
.collect()
})
}
/// Crop long values (prevents unexpected memory pool usage)
fn crop(value: String) -> String {
const L: usize = 125; // + 3 bytes for `...` offset, 128 max @TODO optional
if value.len() > L {
format!("{}...", sanitize(&value[..L]))
} else {
value
}
}
/// Strip tags & bom chars from string
fn sanitize(value: &str) -> String {
use voca_rs::strip::*;