use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
#[cfg(feature = "validation")]
use validator::Validate;
use crate::domain::entity::UploadSession;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::StorageBackend;
use crate::domain::entity::UploadStatus;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateUploadSessionDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "bucket_id")]
pub bucket_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "user_id")]
pub user_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(max = 1024)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub path: String,
#[cfg_attr(feature = "validation", validate(length(max = 255)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub filename: String,
#[cfg_attr(feature = "validation", validate(length(max = 127)))]
#[serde(default, skip_serializing_if = "Option::is_none", alias = "mime_type")]
pub mime_type: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "file_size")]
pub file_size: i64,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "chunk_size")]
pub chunk_size: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "total_chunks")]
pub total_chunks: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "uploaded_chunks")]
pub uploaded_chunks: i32,
pub status: UploadStatus,
#[serde(alias = "storage_backend")]
pub storage_backend: StorageBackend,
#[serde(alias = "completed_parts")]
pub completed_parts: Vec<i32>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "part_etags")]
pub part_etags: Option<serde_json::Value>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "expires_at")]
pub expires_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateUploadSessionDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "bucket_id")]
pub bucket_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "user_id")]
pub user_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(max = 1024)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub path: String,
#[cfg_attr(feature = "validation", validate(length(max = 255)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub filename: String,
#[cfg_attr(feature = "validation", validate(length(max = 127)))]
#[serde(default, skip_serializing_if = "Option::is_none", alias = "mime_type")]
pub mime_type: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "file_size")]
pub file_size: i64,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "chunk_size")]
pub chunk_size: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "total_chunks")]
pub total_chunks: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "uploaded_chunks")]
pub uploaded_chunks: i32,
pub status: UploadStatus,
#[serde(alias = "storage_backend")]
pub storage_backend: StorageBackend,
#[serde(alias = "completed_parts")]
pub completed_parts: Vec<i32>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "part_etags")]
pub part_etags: Option<serde_json::Value>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "expires_at")]
pub expires_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchUploadSessionDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "bucket_id")]
pub bucket_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "user_id")]
pub user_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 1024)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 255)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub filename: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 127)))]
#[serde(skip_serializing_if = "Option::is_none", alias = "mime_type")]
pub mime_type: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "file_size")]
pub file_size: Option<i64>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "chunk_size")]
pub chunk_size: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "total_chunks")]
pub total_chunks: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "uploaded_chunks")]
pub uploaded_chunks: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<UploadStatus>,
#[serde(skip_serializing_if = "Option::is_none", alias = "storage_backend")]
pub storage_backend: Option<StorageBackend>,
#[serde(skip_serializing_if = "Option::is_none", alias = "completed_parts")]
pub completed_parts: Option<Vec<i32>>,
#[serde(skip_serializing_if = "Option::is_none", alias = "part_etags")]
pub part_etags: Option<serde_json::Value>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "expires_at")]
pub expires_at: Option<DateTime<Utc>>,
}
impl PatchUploadSessionDto {
pub fn has_changes(&self) -> bool {
self.bucket_id.is_some() || self.user_id.is_some() || self.path.is_some() || self.filename.is_some() || self.mime_type.is_some() || self.file_size.is_some() || self.chunk_size.is_some() || self.total_chunks.is_some() || self.uploaded_chunks.is_some() || self.status.is_some() || self.storage_backend.is_some() || self.completed_parts.is_some() || self.part_etags.is_some() || self.expires_at.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UploadSessionResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub bucket_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub user_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub path: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub filename: String,
pub mime_type: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub file_size: i64,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub chunk_size: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub total_chunks: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub uploaded_chunks: i32,
pub status: UploadStatus,
pub storage_backend: StorageBackend,
pub completed_parts: Vec<i32>,
pub part_etags: Option<serde_json::Value>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
pub expires_at: DateTime<Utc>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UploadSessionListResponseDto {
pub items: Vec<UploadSessionResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl UploadSessionListResponseDto {
pub fn new(items: Vec<UploadSessionResponseDto>, total: u64, page: u32, per_page: u32) -> Self {
let total_pages = if per_page > 0 {
((total as f64) / (per_page as f64)).ceil() as u32
} else {
0
};
Self {
items,
total,
page,
per_page,
total_pages,
has_next: page < total_pages,
has_prev: page > 1,
}
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UploadSessionSummaryDto {
pub id: Uuid,
pub bucket_id: Uuid,
pub user_id: Uuid,
pub path: String,
pub created_at: Option<DateTime<Utc>>,
}
impl From<UploadSession> for UploadSessionResponseDto {
fn from(entity: UploadSession) -> Self {
Self {
id: entity.id,
bucket_id: entity.bucket_id,
user_id: entity.user_id,
path: entity.path,
filename: entity.filename,
mime_type: entity.mime_type,
file_size: entity.file_size,
chunk_size: entity.chunk_size,
total_chunks: entity.total_chunks,
uploaded_chunks: entity.uploaded_chunks,
status: entity.status,
storage_backend: entity.storage_backend,
completed_parts: entity.completed_parts,
part_etags: entity.part_etags,
expires_at: entity.expires_at,
metadata: entity.metadata,
}
}
}
impl From<UploadSession> for UploadSessionSummaryDto {
fn from(entity: UploadSession) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
bucket_id: entity.bucket_id,
user_id: entity.user_id,
path: entity.path,
created_at,
}
}
}
impl From<CreateUploadSessionDto> for UploadSession {
fn from(dto: CreateUploadSessionDto) -> Self {
Self {
id: Uuid::new_v4(),
bucket_id: dto.bucket_id,
user_id: dto.user_id,
path: dto.path,
filename: dto.filename,
mime_type: dto.mime_type,
file_size: dto.file_size,
chunk_size: dto.chunk_size,
total_chunks: dto.total_chunks,
uploaded_chunks: dto.uploaded_chunks,
status: dto.status,
storage_backend: dto.storage_backend,
completed_parts: dto.completed_parts,
part_etags: dto.part_etags,
expires_at: dto.expires_at,
metadata: AuditMetadata::default(),
}
}
}
impl From<&UploadSession> for UploadSessionResponseDto {
fn from(entity: &UploadSession) -> Self {
Self {
id: entity.id.clone(),
bucket_id: entity.bucket_id.clone(),
user_id: entity.user_id.clone(),
path: entity.path.clone(),
filename: entity.filename.clone(),
mime_type: entity.mime_type.clone(),
file_size: entity.file_size.clone(),
chunk_size: entity.chunk_size.clone(),
total_chunks: entity.total_chunks.clone(),
uploaded_chunks: entity.uploaded_chunks.clone(),
status: entity.status.clone(),
storage_backend: entity.storage_backend.clone(),
completed_parts: entity.completed_parts.clone(),
part_etags: entity.part_etags.clone(),
expires_at: entity.expires_at.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateUploadSessionDto> for UploadSession {
fn from_create_dto(dto: CreateUploadSessionDto) -> backbone_core::ServiceResult<Self> {
Ok(UploadSession::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateUploadSessionDto> for UploadSession {
fn apply_update(mut self, dto: UpdateUploadSessionDto) -> backbone_core::ServiceResult<Self> {
self.bucket_id = dto.bucket_id;
self.user_id = dto.user_id;
self.path = dto.path;
self.filename = dto.filename;
self.mime_type = dto.mime_type;
self.file_size = dto.file_size;
self.chunk_size = dto.chunk_size;
self.total_chunks = dto.total_chunks;
self.uploaded_chunks = dto.uploaded_chunks;
self.status = dto.status;
self.storage_backend = dto.storage_backend;
self.completed_parts = dto.completed_parts;
self.part_etags = dto.part_etags;
self.expires_at = dto.expires_at;
Ok(self)
}
}