1use serde::{Deserialize, Serialize};
9use uuid::Uuid;
10use chrono::{DateTime, Utc};
11
12#[cfg(feature = "openapi")]
13#[cfg(feature = "openapi")]
14use utoipa::ToSchema;
15
16#[cfg(feature = "validation")]
17use validator::Validate;
18
19use crate::domain::entity::UploadSession;
20use crate::domain::entity::AuditMetadata;
21use crate::domain::entity::StorageBackend;
22use crate::domain::entity::UploadStatus;
23
24#[derive(Debug, Clone, Deserialize)]
33#[cfg_attr(feature = "openapi", derive(ToSchema))]
34#[cfg_attr(feature = "validation", derive(Validate))]
35#[serde(rename_all = "camelCase")]
36pub struct CreateUploadSessionDto {
37 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
38 #[serde(alias = "bucket_id")]
39 pub bucket_id: Uuid,
40 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
41 #[serde(alias = "user_id")]
42 pub user_id: Uuid,
43 #[cfg_attr(feature = "validation", validate(length(max = 1024)))]
44 #[cfg_attr(feature = "openapi", schema(example = "example"))]
45 pub path: String,
46 #[cfg_attr(feature = "validation", validate(length(max = 255)))]
47 #[cfg_attr(feature = "openapi", schema(example = "example"))]
48 pub filename: String,
49 #[cfg_attr(feature = "validation", validate(length(max = 127)))]
50 #[serde(default, skip_serializing_if = "Option::is_none", alias = "mime_type")]
51 pub mime_type: Option<String>,
52 #[cfg_attr(feature = "openapi", schema(example = 42))]
53 #[serde(alias = "file_size")]
54 pub file_size: i64,
55 #[cfg_attr(feature = "openapi", schema(example = 42))]
56 #[serde(alias = "chunk_size")]
57 pub chunk_size: i32,
58 #[cfg_attr(feature = "openapi", schema(example = 42))]
59 #[serde(alias = "total_chunks")]
60 pub total_chunks: i32,
61 #[cfg_attr(feature = "openapi", schema(example = 42))]
62 #[serde(alias = "uploaded_chunks")]
63 pub uploaded_chunks: i32,
64 pub status: UploadStatus,
65 #[serde(alias = "storage_backend")]
66 pub storage_backend: StorageBackend,
67 #[serde(alias = "completed_parts")]
68 pub completed_parts: Vec<i32>,
69 #[serde(default, skip_serializing_if = "Option::is_none", alias = "part_etags")]
70 pub part_etags: Option<serde_json::Value>,
71 #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
72 #[serde(alias = "expires_at")]
73 pub expires_at: DateTime<Utc>,
74}
75
76#[derive(Debug, Clone, Deserialize)]
85#[cfg_attr(feature = "openapi", derive(ToSchema))]
86#[cfg_attr(feature = "validation", derive(Validate))]
87#[serde(rename_all = "camelCase")]
88pub struct UpdateUploadSessionDto {
89 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
90 #[serde(alias = "bucket_id")]
91 pub bucket_id: Uuid,
92 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
93 #[serde(alias = "user_id")]
94 pub user_id: Uuid,
95 #[cfg_attr(feature = "validation", validate(length(max = 1024)))]
96 #[cfg_attr(feature = "openapi", schema(example = "example"))]
97 pub path: String,
98 #[cfg_attr(feature = "validation", validate(length(max = 255)))]
99 #[cfg_attr(feature = "openapi", schema(example = "example"))]
100 pub filename: String,
101 #[cfg_attr(feature = "validation", validate(length(max = 127)))]
102 #[serde(default, skip_serializing_if = "Option::is_none", alias = "mime_type")]
103 pub mime_type: Option<String>,
104 #[cfg_attr(feature = "openapi", schema(example = 42))]
105 #[serde(alias = "file_size")]
106 pub file_size: i64,
107 #[cfg_attr(feature = "openapi", schema(example = 42))]
108 #[serde(alias = "chunk_size")]
109 pub chunk_size: i32,
110 #[cfg_attr(feature = "openapi", schema(example = 42))]
111 #[serde(alias = "total_chunks")]
112 pub total_chunks: i32,
113 #[cfg_attr(feature = "openapi", schema(example = 42))]
114 #[serde(alias = "uploaded_chunks")]
115 pub uploaded_chunks: i32,
116 pub status: UploadStatus,
117 #[serde(alias = "storage_backend")]
118 pub storage_backend: StorageBackend,
119 #[serde(alias = "completed_parts")]
120 pub completed_parts: Vec<i32>,
121 #[serde(default, skip_serializing_if = "Option::is_none", alias = "part_etags")]
122 pub part_etags: Option<serde_json::Value>,
123 #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
124 #[serde(alias = "expires_at")]
125 pub expires_at: DateTime<Utc>,
126}
127
128#[derive(Debug, Clone, Default, Deserialize)]
137#[cfg_attr(feature = "openapi", derive(ToSchema))]
138#[cfg_attr(feature = "validation", derive(Validate))]
139#[serde(rename_all = "camelCase")]
140pub struct PatchUploadSessionDto {
141 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
142 #[serde(skip_serializing_if = "Option::is_none", alias = "bucket_id")]
143 pub bucket_id: Option<Uuid>,
144 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
145 #[serde(skip_serializing_if = "Option::is_none", alias = "user_id")]
146 pub user_id: Option<Uuid>,
147 #[cfg_attr(feature = "validation", validate(length(max = 1024)))]
148 #[cfg_attr(feature = "openapi", schema(example = "example"))]
149 #[serde(skip_serializing_if = "Option::is_none")]
150 pub path: Option<String>,
151 #[cfg_attr(feature = "validation", validate(length(max = 255)))]
152 #[cfg_attr(feature = "openapi", schema(example = "example"))]
153 #[serde(skip_serializing_if = "Option::is_none")]
154 pub filename: Option<String>,
155 #[cfg_attr(feature = "validation", validate(length(max = 127)))]
156 #[serde(skip_serializing_if = "Option::is_none", alias = "mime_type")]
157 pub mime_type: Option<String>,
158 #[cfg_attr(feature = "openapi", schema(example = 42))]
159 #[serde(skip_serializing_if = "Option::is_none", alias = "file_size")]
160 pub file_size: Option<i64>,
161 #[cfg_attr(feature = "openapi", schema(example = 42))]
162 #[serde(skip_serializing_if = "Option::is_none", alias = "chunk_size")]
163 pub chunk_size: Option<i32>,
164 #[cfg_attr(feature = "openapi", schema(example = 42))]
165 #[serde(skip_serializing_if = "Option::is_none", alias = "total_chunks")]
166 pub total_chunks: Option<i32>,
167 #[cfg_attr(feature = "openapi", schema(example = 42))]
168 #[serde(skip_serializing_if = "Option::is_none", alias = "uploaded_chunks")]
169 pub uploaded_chunks: Option<i32>,
170 #[serde(skip_serializing_if = "Option::is_none")]
171 pub status: Option<UploadStatus>,
172 #[serde(skip_serializing_if = "Option::is_none", alias = "storage_backend")]
173 pub storage_backend: Option<StorageBackend>,
174 #[serde(skip_serializing_if = "Option::is_none", alias = "completed_parts")]
175 pub completed_parts: Option<Vec<i32>>,
176 #[serde(skip_serializing_if = "Option::is_none", alias = "part_etags")]
177 pub part_etags: Option<serde_json::Value>,
178 #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
179 #[serde(skip_serializing_if = "Option::is_none", alias = "expires_at")]
180 pub expires_at: Option<DateTime<Utc>>,
181}
182
183impl PatchUploadSessionDto {
184 pub fn has_changes(&self) -> bool {
186 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()
187 }
188}
189
190#[derive(Debug, Clone, Serialize)]
199#[cfg_attr(feature = "openapi", derive(ToSchema))]
200#[serde(rename_all = "camelCase")]
201pub struct UploadSessionResponseDto {
202 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
203 pub id: Uuid,
204 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
205 pub bucket_id: Uuid,
206 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
207 pub user_id: Uuid,
208 #[cfg_attr(feature = "openapi", schema(example = "example"))]
209 pub path: String,
210 #[cfg_attr(feature = "openapi", schema(example = "example"))]
211 pub filename: String,
212 pub mime_type: Option<String>,
213 #[cfg_attr(feature = "openapi", schema(example = 42))]
214 pub file_size: i64,
215 #[cfg_attr(feature = "openapi", schema(example = 42))]
216 pub chunk_size: i32,
217 #[cfg_attr(feature = "openapi", schema(example = 42))]
218 pub total_chunks: i32,
219 #[cfg_attr(feature = "openapi", schema(example = 42))]
220 pub uploaded_chunks: i32,
221 pub status: UploadStatus,
222 pub storage_backend: StorageBackend,
223 pub completed_parts: Vec<i32>,
224 pub part_etags: Option<serde_json::Value>,
225 #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
226 pub expires_at: DateTime<Utc>,
227 pub metadata: AuditMetadata,
228}
229
230#[derive(Debug, Clone, Serialize)]
239#[cfg_attr(feature = "openapi", derive(ToSchema))]
240#[serde(rename_all = "camelCase")]
241pub struct UploadSessionListResponseDto {
242 pub items: Vec<UploadSessionResponseDto>,
244 pub total: u64,
246 pub page: u32,
248 pub per_page: u32,
250 pub total_pages: u32,
252 pub has_next: bool,
254 pub has_prev: bool,
256}
257
258impl UploadSessionListResponseDto {
259 pub fn new(items: Vec<UploadSessionResponseDto>, total: u64, page: u32, per_page: u32) -> Self {
261 let total_pages = if per_page > 0 {
262 ((total as f64) / (per_page as f64)).ceil() as u32
263 } else {
264 0
265 };
266 Self {
267 items,
268 total,
269 page,
270 per_page,
271 total_pages,
272 has_next: page < total_pages,
273 has_prev: page > 1,
274 }
275 }
276}
277
278#[derive(Debug, Clone, Serialize)]
280#[cfg_attr(feature = "openapi", derive(ToSchema))]
281#[serde(rename_all = "camelCase")]
282pub struct UploadSessionSummaryDto {
283 pub id: Uuid,
284 pub bucket_id: Uuid,
285 pub user_id: Uuid,
286 pub path: String,
287 pub created_at: Option<DateTime<Utc>>,
288}
289
290impl From<UploadSession> for UploadSessionResponseDto {
295 fn from(entity: UploadSession) -> Self {
296 Self {
297 id: entity.id,
298 bucket_id: entity.bucket_id,
299 user_id: entity.user_id,
300 path: entity.path,
301 filename: entity.filename,
302 mime_type: entity.mime_type,
303 file_size: entity.file_size,
304 chunk_size: entity.chunk_size,
305 total_chunks: entity.total_chunks,
306 uploaded_chunks: entity.uploaded_chunks,
307 status: entity.status,
308 storage_backend: entity.storage_backend,
309 completed_parts: entity.completed_parts,
310 part_etags: entity.part_etags,
311 expires_at: entity.expires_at,
312 metadata: entity.metadata,
313 }
314 }
315}
316
317impl From<UploadSession> for UploadSessionSummaryDto {
318 fn from(entity: UploadSession) -> Self {
319 let created_at = backbone_core::PersistentEntity::created_at(&entity);
320 Self {
321 id: entity.id,
322 bucket_id: entity.bucket_id,
323 user_id: entity.user_id,
324 path: entity.path,
325 created_at,
326 }
327 }
328}
329
330impl From<CreateUploadSessionDto> for UploadSession {
331 fn from(dto: CreateUploadSessionDto) -> Self {
332 Self {
333 id: Uuid::new_v4(),
334 bucket_id: dto.bucket_id,
335 user_id: dto.user_id,
336 path: dto.path,
337 filename: dto.filename,
338 mime_type: dto.mime_type,
339 file_size: dto.file_size,
340 chunk_size: dto.chunk_size,
341 total_chunks: dto.total_chunks,
342 uploaded_chunks: dto.uploaded_chunks,
343 status: dto.status,
344 storage_backend: dto.storage_backend,
345 completed_parts: dto.completed_parts,
346 part_etags: dto.part_etags,
347 expires_at: dto.expires_at,
348 metadata: AuditMetadata::default(),
349 }
350 }
351}
352
353impl From<&UploadSession> for UploadSessionResponseDto {
354 fn from(entity: &UploadSession) -> Self {
355 Self {
356 id: entity.id.clone(),
357 bucket_id: entity.bucket_id.clone(),
358 user_id: entity.user_id.clone(),
359 path: entity.path.clone(),
360 filename: entity.filename.clone(),
361 mime_type: entity.mime_type.clone(),
362 file_size: entity.file_size.clone(),
363 chunk_size: entity.chunk_size.clone(),
364 total_chunks: entity.total_chunks.clone(),
365 uploaded_chunks: entity.uploaded_chunks.clone(),
366 status: entity.status.clone(),
367 storage_backend: entity.storage_backend.clone(),
368 completed_parts: entity.completed_parts.clone(),
369 part_etags: entity.part_etags.clone(),
370 expires_at: entity.expires_at.clone(),
371 metadata: entity.metadata.clone(),
372 }
373 }
374}
375
376impl backbone_core::FromCreateDto<CreateUploadSessionDto> for UploadSession {
377 fn from_create_dto(dto: CreateUploadSessionDto) -> backbone_core::ServiceResult<Self> {
378 Ok(UploadSession::from(dto))
379 }
380}
381
382impl backbone_core::ApplyUpdateDto<UpdateUploadSessionDto> for UploadSession {
383 fn apply_update(mut self, dto: UpdateUploadSessionDto) -> backbone_core::ServiceResult<Self> {
384 self.bucket_id = dto.bucket_id;
385 self.user_id = dto.user_id;
386 self.path = dto.path;
387 self.filename = dto.filename;
388 self.mime_type = dto.mime_type;
389 self.file_size = dto.file_size;
390 self.chunk_size = dto.chunk_size;
391 self.total_chunks = dto.total_chunks;
392 self.uploaded_chunks = dto.uploaded_chunks;
393 self.status = dto.status;
394 self.storage_backend = dto.storage_backend;
395 self.completed_parts = dto.completed_parts;
396 self.part_etags = dto.part_etags;
397 self.expires_at = dto.expires_at;
398 Ok(self)
399 }
400}
401
402