fix: refactor logging events to use owned asset instances and simplify log event structures
This commit is contained in:
@@ -5,11 +5,12 @@ use anyhow::Result;
|
||||
use chrono::{Duration, Utc};
|
||||
use futures::lock::Mutex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::DATA_STORAGE;
|
||||
use crate::logs::{LogEventType, log_event};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Asset {
|
||||
id: String,
|
||||
share_duration: u32,
|
||||
@@ -45,8 +46,8 @@ impl Asset {
|
||||
self.id.clone()
|
||||
}
|
||||
|
||||
pub fn mime(&self) -> &str {
|
||||
&self.mime
|
||||
pub fn mime(&self) -> String {
|
||||
self.mime.clone()
|
||||
}
|
||||
|
||||
pub fn content(&self) -> Vec<u8> {
|
||||
@@ -65,6 +66,10 @@ impl Asset {
|
||||
self.expires_at
|
||||
}
|
||||
|
||||
pub fn mime_type(&self) -> &str {
|
||||
&self.mime
|
||||
}
|
||||
|
||||
pub fn size_bytes(&self) -> usize {
|
||||
self.content.len()
|
||||
}
|
||||
@@ -85,8 +90,16 @@ 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>>>,
|
||||
@@ -110,8 +123,8 @@ impl AssetTracker {
|
||||
let mut assets = self.assets.lock().await;
|
||||
let removed_assets = assets.extract_if(.., |asset| asset.is_expired());
|
||||
for asset in removed_assets {
|
||||
log_event(LogEventType::AssetDeleted(&asset));
|
||||
println!("[{}] Removing asset: {}", chrono::Local::now().to_rfc3339(), asset.id());
|
||||
log_event(LogEventType::AssetDeleted(asset));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user