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

33 lines
630 B
Rust
Raw Normal View History

2025-04-05 17:10:55 +00:00
use serde::Serialize;
use sha2::{Digest, Sha256};
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 {
let mut hasher = Sha256::new();
hasher.update(&self.data);
let hash = hasher.finalize();
format!("{hash:X}")
}
}