fix: update asset logging to use serialized values and enhance asset struct with default implementation

This commit is contained in:
2026-01-11 09:54:31 +01:00
parent 62f3c49e8a
commit 28b7860c6c
3 changed files with 15 additions and 18 deletions

View File

@@ -10,7 +10,7 @@ use serde_json::Value;
use crate::DATA_STORAGE;
use crate::logs::{LogEventType, log_event};
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct Asset {
id: String,
share_duration: u32,
@@ -83,6 +83,10 @@ impl Asset {
Ok(bytes)
}
pub fn to_value(&self) -> Value {
serde_json::to_value(self).unwrap_or(Value::Null)
}
pub fn save(&self) -> Result<String> {
let id = self.id.clone();
let path = format!("{}{}", DATA_STORAGE, self.id);
@@ -90,16 +94,8 @@ impl Asset {
std::fs::write(&path, self.to_bytes()?)?;
Ok(id)
}
}
impl From<Asset> for Value {
fn from(asset: Asset) -> Self {
serde_json::to_value(asset).unwrap()
}
}
#[derive(Clone)]
pub struct AssetTracker {
assets: Arc<Mutex<Vec<Asset>>>,
@@ -124,7 +120,7 @@ impl AssetTracker {
let removed_assets = assets.extract_if(.., |asset| asset.is_expired());
for asset in removed_assets {
println!("[{}] Removing asset: {}", chrono::Local::now().to_rfc3339(), asset.id());
log_event(LogEventType::AssetDeleted(asset));
log_event(LogEventType::AssetDeleted(asset.to_value()));
}
}