Files
Oxicloud/src/application/dtos/file_dto.rs
T

113 lines
3.0 KiB
Rust
Raw Normal View History

2025-03-17 21:28:08 +01:00
use crate::domain::entities::file::File;
2026-02-14 01:29:34 +01:00
use serde::{Deserialize, Serialize};
2025-03-17 21:28:08 +01:00
use super::display_helpers::{format_file_size, mime_to_category, mime_to_icon_class, mime_to_icon_special_class};
2025-03-17 21:28:08 +01:00
/// DTO for file responses
2025-03-27 01:13:34 +01:00
#[derive(Debug, Clone, Serialize, Deserialize)]
2025-03-17 21:28:08 +01:00
pub struct FileDto {
/// File ID
pub id: String,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// File name
pub name: String,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// Path to the file (relative)
pub path: String,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// Size in bytes
pub size: u64,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// MIME type
pub mime_type: String,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// Parent folder ID
pub folder_id: Option<String>,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// Creation timestamp
pub created_at: u64,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// Last modification timestamp
pub modified_at: u64,
// ── Pre-computed display fields ──
/// FontAwesome icon CSS class (e.g. "fas fa-file-image")
pub icon_class: String,
/// Extra CSS class for icon styling (e.g. "image-icon", "" when default)
pub icon_special_class: String,
/// Human-readable file category (e.g. "Image", "Document")
pub category: String,
/// Human-readable formatted size (e.g. "3.27 MB")
pub size_formatted: String,
2025-03-17 21:28:08 +01:00
}
impl From<File> for FileDto {
fn from(file: File) -> Self {
let mime = file.mime_type();
let size = file.size();
2025-03-17 21:28:08 +01:00
Self {
2025-03-19 00:44:27 +01:00
id: file.id().to_string(),
name: file.name().to_string(),
path: file.path_string().to_string(),
size,
mime_type: mime.to_string(),
2025-03-19 00:44:27 +01:00
folder_id: file.folder_id().map(String::from),
created_at: file.created_at(),
modified_at: file.modified_at(),
icon_class: mime_to_icon_class(mime).to_string(),
icon_special_class: mime_to_icon_special_class(mime).to_string(),
category: mime_to_category(mime).to_string(),
size_formatted: format_file_size(size),
2025-03-17 21:28:08 +01:00
}
}
2025-03-19 00:44:27 +01:00
}
// To convert from FileDto to File for batch handlers
2025-03-19 00:44:27 +01:00
impl From<FileDto> for File {
fn from(dto: FileDto) -> Self {
// Display fields (icon_class, icon_special_class, category, size_formatted)
// are not part of the domain entity and are ignored.
2025-03-19 00:44:27 +01:00
File::from_dto(
2026-02-14 01:29:34 +01:00
dto.id,
dto.name,
2025-03-19 00:44:27 +01:00
dto.path,
dto.size,
dto.mime_type,
dto.folder_id,
dto.created_at,
2026-02-14 01:29:34 +01:00
dto.modified_at,
2025-03-19 00:44:27 +01:00
)
}
2025-03-20 09:22:31 +01:00
}
impl FileDto {
/// Creates an empty file DTO for stub implementations
pub fn empty() -> Self {
Self {
id: "stub-id".to_string(),
name: "stub-file".to_string(),
path: "/stub/path".to_string(),
size: 0,
mime_type: "application/octet-stream".to_string(),
folder_id: None,
created_at: 0,
modified_at: 0,
icon_class: "fas fa-file".to_string(),
icon_special_class: String::new(),
category: "Document".to_string(),
size_formatted: "0 Bytes".to_string(),
2025-03-20 09:22:31 +01:00
}
}
}
impl Default for FileDto {
fn default() -> Self {
Self::empty()
}
2026-02-14 01:29:34 +01:00
}