2026-03-03 15:55:15 +00:00
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
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};
|
2026-03-29 18:49:10 +02:00
|
|
|
|
use utoipa::ToSchema;
|
2025-03-17 21:28:08 +01:00
|
|
|
|
|
2026-02-21 13:33:18 +01:00
|
|
|
|
use super::display_helpers::{
|
|
|
|
|
|
category_for, format_file_size, icon_class_for, icon_special_class_for,
|
|
|
|
|
|
};
|
2026-02-16 01:09:28 +01:00
|
|
|
|
|
2025-03-17 21:28:08 +01:00
|
|
|
|
/// DTO for file responses
|
2026-03-29 18:49:10 +02:00
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
|
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
|
|
|
|
|
2026-03-03 15:55:15 +00:00
|
|
|
|
/// MIME type — `Arc<str>` because MIME values repeat across files
|
|
|
|
|
|
/// and DTOs are cloned on every request (clone is O(1) atomic increment).
|
2026-03-29 18:49:10 +02:00
|
|
|
|
#[schema(value_type = String)]
|
2026-03-03 15:55:15 +00:00
|
|
|
|
pub mime_type: Arc<str>,
|
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,
|
2026-02-16 01:09:28 +01:00
|
|
|
|
|
2026-03-03 15:55:15 +00:00
|
|
|
|
// ── Pre-computed display fields (Arc<str>: values come from static tables) ──
|
2026-02-16 01:09:28 +01:00
|
|
|
|
/// FontAwesome icon CSS class (e.g. "fas fa-file-image")
|
2026-03-29 18:49:10 +02:00
|
|
|
|
#[schema(value_type = String)]
|
2026-03-03 15:55:15 +00:00
|
|
|
|
pub icon_class: Arc<str>,
|
2026-02-16 01:09:28 +01:00
|
|
|
|
|
|
|
|
|
|
/// Extra CSS class for icon styling (e.g. "image-icon", "" when default)
|
2026-03-29 18:49:10 +02:00
|
|
|
|
#[schema(value_type = String)]
|
2026-03-03 15:55:15 +00:00
|
|
|
|
pub icon_special_class: Arc<str>,
|
2026-02-16 01:09:28 +01:00
|
|
|
|
|
|
|
|
|
|
/// Human-readable file category (e.g. "Image", "Document")
|
2026-03-29 18:49:10 +02:00
|
|
|
|
#[schema(value_type = String)]
|
2026-03-03 15:55:15 +00:00
|
|
|
|
pub category: Arc<str>,
|
2026-02-16 01:09:28 +01:00
|
|
|
|
|
|
|
|
|
|
/// Human-readable formatted size (e.g. "3.27 MB")
|
|
|
|
|
|
pub size_formatted: String,
|
2026-02-21 13:39:27 +01:00
|
|
|
|
|
|
|
|
|
|
/// Owner user ID (omitted from JSON when None)
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
|
pub owner_id: Option<String>,
|
2026-03-05 13:40:02 -05:00
|
|
|
|
|
|
|
|
|
|
/// Sort date for Photos timeline — COALESCE(EXIF captured_at, created_at).
|
|
|
|
|
|
/// Only populated by the /api/photos endpoint.
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
|
pub sort_date: Option<u64>,
|
2026-03-15 14:14:24 -04:00
|
|
|
|
|
2026-06-06 15:37:27 +02:00
|
|
|
|
/// Raw BLAKE3 content hash. Populated from `File::content_hash()`.
|
|
|
|
|
|
/// Exposed in REST JSON so API consumers can use it for
|
|
|
|
|
|
/// content-addressable URLs, dedup verification, and integrity
|
|
|
|
|
|
/// audits. Distinct from `etag` (which is an HTTP-only cache
|
|
|
|
|
|
/// token whose formula may grow to include `modified_at` etc.).
|
|
|
|
|
|
pub content_hash: String,
|
|
|
|
|
|
|
|
|
|
|
|
/// Opaque HTTP ETag. Populated from `File::etag()`. Used by
|
|
|
|
|
|
/// WebDAV/NextCloud handlers when emitting `ETag` headers and
|
|
|
|
|
|
/// also exposed in REST JSON so frontends can pass it back
|
|
|
|
|
|
/// through `If-Match` / `If-None-Match` on download / mutation
|
|
|
|
|
|
/// endpoints without a separate HEAD round-trip.
|
2026-03-15 14:14:24 -04:00
|
|
|
|
pub etag: String,
|
2025-03-17 21:28:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<File> for FileDto {
|
|
|
|
|
|
fn from(file: File) -> Self {
|
2026-06-06 16:46:19 +02:00
|
|
|
|
// Compute the HTTP ETag BEFORE consuming the entity —
|
|
|
|
|
|
// `File::etag()` derives from `blob_hash` + `modified_at`,
|
|
|
|
|
|
// so it must run against the live entity, not against
|
|
|
|
|
|
// already-extracted parts. `content_hash` is just the raw
|
|
|
|
|
|
// blob hash; `etag` is the cache token derived from it.
|
|
|
|
|
|
let etag = file.etag();
|
2026-06-06 15:37:27 +02:00
|
|
|
|
let content_hash = file.content_hash().to_string();
|
|
|
|
|
|
|
2026-03-06 22:37:31 +01:00
|
|
|
|
// Consume the entity by moving all fields — zero heap allocations
|
|
|
|
|
|
// for id, name, path, folder_id, owner_id (previously 5× .to_string()).
|
|
|
|
|
|
let parts = file.into_parts();
|
|
|
|
|
|
|
|
|
|
|
|
let icon_class = Arc::from(icon_class_for(&parts.name, &parts.mime_type));
|
|
|
|
|
|
let icon_special_class = Arc::from(icon_special_class_for(&parts.name, &parts.mime_type));
|
|
|
|
|
|
let category = Arc::from(category_for(&parts.name, &parts.mime_type));
|
|
|
|
|
|
let size_formatted = format_file_size(parts.size);
|
|
|
|
|
|
let mime_type = Arc::from(parts.mime_type.as_str());
|
2026-02-16 01:09:28 +01:00
|
|
|
|
|
2025-03-17 21:28:08 +01:00
|
|
|
|
Self {
|
2026-03-06 22:37:31 +01:00
|
|
|
|
id: parts.id,
|
|
|
|
|
|
name: parts.name,
|
|
|
|
|
|
path: parts.path_string,
|
|
|
|
|
|
size: parts.size,
|
|
|
|
|
|
mime_type,
|
|
|
|
|
|
folder_id: parts.folder_id,
|
|
|
|
|
|
created_at: parts.created_at,
|
|
|
|
|
|
modified_at: parts.modified_at,
|
|
|
|
|
|
icon_class,
|
|
|
|
|
|
icon_special_class,
|
|
|
|
|
|
category,
|
|
|
|
|
|
size_formatted,
|
2026-03-07 18:05:52 +01:00
|
|
|
|
owner_id: parts.owner_id.map(|u| u.to_string()),
|
2026-03-05 13:40:02 -05:00
|
|
|
|
sort_date: None,
|
2026-06-06 15:37:27 +02:00
|
|
|
|
content_hash,
|
|
|
|
|
|
etag,
|
2025-03-17 21:28:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-03-19 00:44:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-12 09:41:25 +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 {
|
2026-02-16 01:09:28 +01:00
|
|
|
|
// 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,
|
2026-03-03 15:55:15 +00:00
|
|
|
|
dto.mime_type.to_string(),
|
2025-03-19 00:44:27 +01:00
|
|
|
|
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 {
|
2026-05-24 01:46:51 +02:00
|
|
|
|
/// Returns a copy of this DTO with the `path` field cleared.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Used when a file is returned to a share recipient: `path` reveals the
|
|
|
|
|
|
/// full folder hierarchy above the file which the recipient may not have
|
|
|
|
|
|
/// access to. `folder_id` and `owner_id` are intentionally kept — the
|
|
|
|
|
|
/// former is needed for sub-folder navigation (covered by the cascade
|
|
|
|
|
|
/// grant), and the latter is harmless metadata.
|
|
|
|
|
|
#[must_use]
|
|
|
|
|
|
pub fn without_hierarchy_info(self) -> Self {
|
|
|
|
|
|
Self {
|
|
|
|
|
|
path: String::new(),
|
|
|
|
|
|
..self
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-20 09:22:31 +01:00
|
|
|
|
/// 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,
|
2026-03-03 15:55:15 +00:00
|
|
|
|
mime_type: Arc::from("application/octet-stream"),
|
2025-03-20 09:22:31 +01:00
|
|
|
|
folder_id: None,
|
|
|
|
|
|
created_at: 0,
|
|
|
|
|
|
modified_at: 0,
|
2026-03-03 15:55:15 +00:00
|
|
|
|
icon_class: Arc::from("fas fa-file"),
|
|
|
|
|
|
icon_special_class: Arc::from(""),
|
|
|
|
|
|
category: Arc::from("Document"),
|
2026-02-16 01:09:28 +01:00
|
|
|
|
size_formatted: "0 Bytes".to_string(),
|
2026-02-21 13:39:27 +01:00
|
|
|
|
owner_id: None,
|
2026-06-06 15:37:27 +02:00
|
|
|
|
content_hash: String::new(),
|
2026-03-15 14:14:24 -04:00
|
|
|
|
etag: String::new(),
|
2026-03-05 13:40:02 -05:00
|
|
|
|
sort_date: None,
|
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
|
|
|
|
}
|