fix: update asset logging to use serialized values and enhance asset struct with default implementation
This commit is contained in:
11
src/api.rs
11
src/api.rs
@@ -5,8 +5,8 @@ use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{
|
||||
data_mgt::AssetTracker,
|
||||
logs::{ LogEvent, LogEventType, log_event},
|
||||
data_mgt::{Asset, AssetTracker},
|
||||
logs::{LogEvent, LogEventType, log_event},
|
||||
};
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
@@ -45,8 +45,8 @@ async fn api_upload(
|
||||
Some(uploader_ip.clone()),
|
||||
);
|
||||
|
||||
log_event(LogEventType::AssetUploaded(asset.clone()));
|
||||
let id = asset.id();
|
||||
log_event(LogEventType::AssetUploaded(asset.to_value()));
|
||||
assets.add_asset(asset).await;
|
||||
let response_body = json!({ "link": format!("/bhs/{}", id) });
|
||||
Ok(HttpResponse::Ok().json(response_body))
|
||||
@@ -93,8 +93,7 @@ async fn api_stats(assets: web::Data<AssetTracker>) -> Result<HttpResponse, acti
|
||||
use crate::LOG_DIR;
|
||||
use std::fs;
|
||||
|
||||
let (active_assets, storage_bytes, image_count, text_count) =
|
||||
assets.stats_summary().await;
|
||||
let (active_assets, storage_bytes, image_count, text_count) = assets.stats_summary().await;
|
||||
|
||||
let mut total_uploads = 0;
|
||||
let mut total_deleted = 0;
|
||||
@@ -110,6 +109,7 @@ async fn api_stats(assets: web::Data<AssetTracker>) -> Result<HttpResponse, acti
|
||||
request_count += 1;
|
||||
}
|
||||
LogEventType::AssetUploaded(asset) => {
|
||||
let asset = serde_json::from_value::<Asset>(asset).unwrap_or_default();
|
||||
total_uploads += 1;
|
||||
recent_activity.push(ActivityItem {
|
||||
action: "upload".to_string(),
|
||||
@@ -119,6 +119,7 @@ async fn api_stats(assets: web::Data<AssetTracker>) -> Result<HttpResponse, acti
|
||||
});
|
||||
}
|
||||
LogEventType::AssetDeleted(asset) => {
|
||||
let asset = serde_json::from_value::<Asset>(asset).unwrap_or_default();
|
||||
total_deleted += 1;
|
||||
recent_activity.push(ActivityItem {
|
||||
action: "delete".to_string(),
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@ use std::{fs::OpenOptions, io::Write};
|
||||
|
||||
use actix_web::HttpRequest;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{LOG_DIR, data_mgt::Asset};
|
||||
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct LogHttpRequest {
|
||||
pub method: String,
|
||||
@@ -49,8 +49,8 @@ impl From<HttpRequest> for LogHttpRequest {
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum LogEventType {
|
||||
AssetUploaded(Asset),
|
||||
AssetDeleted(Asset),
|
||||
AssetUploaded(Value),
|
||||
AssetDeleted(Value),
|
||||
HttpRequest(LogHttpRequest),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user