rust-api-template/src/api/routes/files/models.rs

33 lines
628 B
Rust
Raw Normal View History

2025-05-31 15:57:05 +00:00
use blake3::Hasher;
2025-04-05 17:10:55 +00:00
use serde::Serialize;
use tokio_util::bytes::Bytes;
use ts_rs::TS;
use utoipa::ToSchema;
#[derive(Debug, Clone, Serialize, TS, ToSchema)]
#[ts(export)]
pub struct AttachedFile {
pub name: String,
pub hash: String,
pub content_type: String,
pub size: i32,
}
2025-05-04 20:17:09 +00:00
#[derive(Debug)]
2025-04-05 17:10:55 +00:00
pub struct File {
pub name: String,
pub content_type: String,
pub data: Bytes,
pub size: i32,
}
impl File {
pub fn hash(&self) -> String {
2025-05-31 15:57:05 +00:00
let mut hasher = Hasher::new();
2025-04-05 17:10:55 +00:00
hasher.update(&self.data);
let hash = hasher.finalize();
2025-05-31 15:57:05 +00:00
hash.to_hex().to_string()
2025-04-05 17:10:55 +00:00
}
}