use std::collections::HashMap;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
pub const DEFAULT_CONTENT_TYPE: &str = "application/octet-stream";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StoredFile {
pub key: String,
pub size: u64,
pub content_type: String,
pub stored_at: DateTime<Utc>,
pub metadata: HashMap<String, String>,
}
impl StoredFile {
#[must_use]
pub fn new(key: impl Into<String>, size: u64, content_type: Option<&str>) -> Self {
Self {
key: key.into(),
size,
content_type: content_type_or_default(content_type).to_string(),
stored_at: Utc::now(),
metadata: HashMap::new(),
}
}
#[must_use]
pub fn with_metadata(mut self, metadata: HashMap<String, String>) -> Self {
self.metadata = metadata;
self
}
#[must_use]
pub fn with_stored_at(mut self, stored_at: DateTime<Utc>) -> Self {
self.stored_at = stored_at;
self
}
}
#[must_use]
pub fn content_type_or_default(content_type: Option<&str>) -> &str {
content_type
.map(str::trim)
.filter(|value| !value.is_empty())
.unwrap_or(DEFAULT_CONTENT_TYPE)
}