2026-03-03 15:55:15 +00:00
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
2026-05-28 00:44:20 +02:00
|
|
|
|
use crate::application::dtos::cursor::{CursorListResponse, CursorQuery, PageCursor};
|
|
|
|
|
|
use crate::application::dtos::grant_dto::{ResourceContentDto, ResourceTypeDto};
|
2025-03-17 21:28:08 +01:00
|
|
|
|
use crate::domain::entities::folder::Folder;
|
2026-05-28 00:44:20 +02:00
|
|
|
|
use crate::domain::services::authorization::ResourceKind;
|
|
|
|
|
|
use chrono::{DateTime, Utc};
|
2026-02-14 01:29:34 +01:00
|
|
|
|
use serde::{Deserialize, Serialize};
|
2026-05-28 00:44:20 +02:00
|
|
|
|
use utoipa::{IntoParams, ToSchema};
|
|
|
|
|
|
use uuid::Uuid;
|
2025-03-17 21:28:08 +01:00
|
|
|
|
|
|
|
|
|
|
/// DTO for folder creation requests
|
2026-03-29 18:49:10 +02:00
|
|
|
|
#[derive(Debug, Deserialize, ToSchema)]
|
2025-03-17 21:28:08 +01:00
|
|
|
|
pub struct CreateFolderDto {
|
|
|
|
|
|
/// Name of the folder to create
|
|
|
|
|
|
pub name: String,
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
2025-03-17 21:28:08 +01:00
|
|
|
|
/// Parent folder ID (None for root level)
|
|
|
|
|
|
pub parent_id: Option<String>,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// DTO for folder rename requests
|
2026-03-29 18:49:10 +02:00
|
|
|
|
#[derive(Debug, Deserialize, ToSchema)]
|
2025-03-17 21:28:08 +01:00
|
|
|
|
pub struct RenameFolderDto {
|
|
|
|
|
|
/// New name for the folder
|
|
|
|
|
|
pub name: String,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// DTO for folder move requests
|
2026-03-29 18:49:10 +02:00
|
|
|
|
#[derive(Debug, Deserialize, ToSchema)]
|
2025-03-17 21:28:08 +01:00
|
|
|
|
pub struct MoveFolderDto {
|
|
|
|
|
|
/// New parent folder ID (None for root level)
|
|
|
|
|
|
pub parent_id: Option<String>,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// DTO for folder 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 FolderDto {
|
|
|
|
|
|
/// Folder ID
|
|
|
|
|
|
pub id: String,
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
2025-03-17 21:28:08 +01:00
|
|
|
|
/// Folder name
|
|
|
|
|
|
pub name: String,
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
2025-03-17 21:28:08 +01:00
|
|
|
|
/// Path to the folder (relative)
|
|
|
|
|
|
pub path: String,
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
2025-03-17 21:28:08 +01:00
|
|
|
|
/// Parent folder ID
|
|
|
|
|
|
pub parent_id: Option<String>,
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
2026-02-15 23:45:11 +01:00
|
|
|
|
/// Owner user ID (scopes visibility per user)
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
|
pub owner_id: Option<String>,
|
|
|
|
|
|
|
2026-06-19 07:49:33 +02:00
|
|
|
|
/// Drive that owns this folder. The scope axis for path-based
|
|
|
|
|
|
/// lookups across REST / WebDAV / NextCloud / CalDAV / CardDAV.
|
|
|
|
|
|
/// Post-D0 `storage.folders.drive_id` is `NOT NULL`; stub /
|
|
|
|
|
|
/// DTO-reconstructed folders carry `Uuid::nil()`.
|
|
|
|
|
|
pub drive_id: Uuid,
|
|
|
|
|
|
|
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-14 01:29:34 +01:00
|
|
|
|
|
2025-03-17 21:28:08 +01:00
|
|
|
|
/// Whether this is a root folder
|
|
|
|
|
|
pub is_root: bool,
|
2026-02-16 01:09:28 +01:00
|
|
|
|
|
2026-03-03 15:55:15 +00:00
|
|
|
|
// ── Pre-computed display fields (Arc<str>: always identical values) ──
|
2026-02-16 01:09:28 +01:00
|
|
|
|
/// FontAwesome icon CSS class (always "fas fa-folder")
|
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 (always "folder-icon")
|
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 category (always "Folder")
|
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-06-06 15:37:27 +02:00
|
|
|
|
|
|
|
|
|
|
/// Opaque ETag for HTTP responses. Populated from `Folder::etag()`
|
|
|
|
|
|
/// at conversion time so every WebDAV / NextCloud handler emits
|
|
|
|
|
|
/// the same value, and exposed in REST JSON so the frontend can
|
|
|
|
|
|
/// pass it back through `If-Match` on rename / move endpoints
|
|
|
|
|
|
/// without a separate HEAD round-trip.
|
|
|
|
|
|
pub etag: String,
|
2025-03-17 21:28:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<Folder> for FolderDto {
|
|
|
|
|
|
fn from(folder: Folder) -> Self {
|
2025-03-19 00:44:27 +01:00
|
|
|
|
let is_root = folder.parent_id().is_none();
|
2026-06-06 15:37:27 +02:00
|
|
|
|
let etag = folder.etag().to_string();
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
2025-03-17 21:28:08 +01:00
|
|
|
|
Self {
|
2025-03-19 00:44:27 +01:00
|
|
|
|
id: folder.id().to_string(),
|
|
|
|
|
|
name: folder.name().to_string(),
|
|
|
|
|
|
path: folder.path_string().to_string(),
|
|
|
|
|
|
parent_id: folder.parent_id().map(String::from),
|
2026-03-07 18:05:52 +01:00
|
|
|
|
owner_id: folder.owner_id().map(|u| u.to_string()),
|
2026-06-19 07:49:33 +02:00
|
|
|
|
drive_id: folder.drive_id(),
|
2025-03-19 00:44:27 +01:00
|
|
|
|
created_at: folder.created_at(),
|
|
|
|
|
|
modified_at: folder.modified_at(),
|
2025-03-17 21:28:08 +01:00
|
|
|
|
is_root,
|
2026-03-03 15:55:15 +00:00
|
|
|
|
icon_class: Arc::from("fas fa-folder"),
|
|
|
|
|
|
icon_special_class: Arc::from("folder-icon"),
|
|
|
|
|
|
category: Arc::from("Folder"),
|
2026-06-06 15:37:27 +02:00
|
|
|
|
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 FolderDto to Folder for batch handlers
|
2025-03-19 00:44:27 +01:00
|
|
|
|
impl From<FolderDto> for Folder {
|
|
|
|
|
|
fn from(dto: FolderDto) -> Self {
|
2026-02-16 01:09:28 +01:00
|
|
|
|
// Display fields (icon_class, icon_special_class, category)
|
|
|
|
|
|
// are not part of the domain entity and are ignored.
|
2025-03-19 00:44:27 +01:00
|
|
|
|
Folder::from_dto(
|
|
|
|
|
|
dto.id,
|
|
|
|
|
|
dto.name,
|
|
|
|
|
|
dto.path,
|
|
|
|
|
|
dto.parent_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 FolderDto {
|
2026-05-24 01:46:51 +02:00
|
|
|
|
/// Returns a copy of this DTO with the `path` field cleared.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Used when a folder is returned to a share recipient: `path` reveals the
|
|
|
|
|
|
/// full folder hierarchy above the shared folder which the recipient may
|
|
|
|
|
|
/// not have access to. `parent_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 folder DTO for stub implementations
|
|
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
|
|
Self {
|
|
|
|
|
|
id: "stub-id".to_string(),
|
|
|
|
|
|
name: "stub-folder".to_string(),
|
|
|
|
|
|
path: "/stub/path".to_string(),
|
|
|
|
|
|
parent_id: None,
|
2026-02-15 23:45:11 +01:00
|
|
|
|
owner_id: None,
|
2026-06-19 07:49:33 +02:00
|
|
|
|
drive_id: Uuid::nil(),
|
2025-03-20 09:22:31 +01:00
|
|
|
|
created_at: 0,
|
|
|
|
|
|
modified_at: 0,
|
|
|
|
|
|
is_root: true,
|
2026-03-03 15:55:15 +00:00
|
|
|
|
icon_class: Arc::from("fas fa-folder"),
|
|
|
|
|
|
icon_special_class: Arc::from("folder-icon"),
|
|
|
|
|
|
category: Arc::from("Folder"),
|
2026-06-06 15:37:27 +02:00
|
|
|
|
etag: String::new(),
|
2025-03-20 09:22:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Default for FolderDto {
|
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
|
Self::empty()
|
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
}
|
2026-05-28 00:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|
|
|
|
|
// Cursor-paginated folder resources (GET /api/folders/{id}/resources)
|
|
|
|
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|
|
|
|
|
|
|
|
|
|
|
/// Raw row returned by the UNION ALL query that combines `storage.folders` and
|
|
|
|
|
|
/// `storage.files` for a given parent folder. Used internally between the
|
|
|
|
|
|
/// repository and service/handler layers — never serialised directly.
|
|
|
|
|
|
pub struct FolderResourceRow {
|
|
|
|
|
|
pub resource_type: String, // "folder" | "file"
|
|
|
|
|
|
pub id: Uuid,
|
|
|
|
|
|
pub name: String,
|
|
|
|
|
|
/// Parent folder UUID (for both resource types).
|
|
|
|
|
|
pub parent_id: Option<Uuid>,
|
|
|
|
|
|
/// `None` for folders.
|
|
|
|
|
|
pub mime_type: Option<String>,
|
|
|
|
|
|
/// `-1` sentinel for folders (no physical size).
|
|
|
|
|
|
pub size: i64,
|
|
|
|
|
|
pub created_at: DateTime<Utc>,
|
|
|
|
|
|
pub modified_at: DateTime<Utc>,
|
|
|
|
|
|
pub owner_id: Uuid,
|
2026-06-06 19:51:51 +02:00
|
|
|
|
/// Raw BLAKE3 content hash. `Some(_)` for file rows, `None` for
|
|
|
|
|
|
/// folder rows. Populates `FileDto::content_hash` + `FileDto::etag`
|
|
|
|
|
|
/// on the REST `/api/folders/{id}/resources` listing so API
|
|
|
|
|
|
/// consumers can issue conditional requests against listed files.
|
|
|
|
|
|
pub blob_hash: Option<String>,
|
2026-05-28 00:44:20 +02:00
|
|
|
|
// Pre-computed sort fields — returned by the SQL for cursor construction.
|
|
|
|
|
|
/// `LOWER(name)` used by `name`/`type` sorts.
|
|
|
|
|
|
pub sort_str: String,
|
|
|
|
|
|
/// `category_order` for files, `0` for folders.
|
|
|
|
|
|
pub type_order: i64,
|
|
|
|
|
|
/// `0` for folders, `1` for files (used by `name` sort to keep folders first).
|
|
|
|
|
|
pub folder_first: i32,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Opaque keyset-pagination cursor for `/api/folders/{id}/resources`.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Encoded as base64url-JSON (same scheme as [`GrantCursor`]).
|
|
|
|
|
|
/// Fields are sparse: only the sort-relevant ones are serialised.
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
|
|
pub struct FolderResourceCursor {
|
|
|
|
|
|
/// Sort dimension active when this cursor was produced.
|
|
|
|
|
|
#[serde(default = "FolderResourceCursor::default_order")]
|
|
|
|
|
|
pub order_by: String,
|
|
|
|
|
|
/// UUID of the last item on the previous page (tie-breaker).
|
|
|
|
|
|
pub resource_id: Uuid,
|
|
|
|
|
|
/// `LOWER(name)` for `name`/`type` sorts.
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
|
pub sort_str: Option<String>,
|
|
|
|
|
|
/// Multipurpose integer sort key:
|
|
|
|
|
|
/// - `name`: `folder_first` (0 = folder, 1 = file)
|
|
|
|
|
|
/// - `type`: `category_order` (0 = Folder, 100 = Image …)
|
|
|
|
|
|
/// - `size`: file size in bytes, -1 for folders
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
|
pub sort_int: Option<i64>,
|
|
|
|
|
|
/// Timestamp for `modified_at` / `created_at` sorts.
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
|
pub sort_ts: Option<DateTime<Utc>>,
|
|
|
|
|
|
/// Whether the result set was reversed when this cursor was produced.
|
|
|
|
|
|
/// Must be passed unchanged on subsequent page requests.
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
|
pub reverse: bool,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl FolderResourceCursor {
|
|
|
|
|
|
fn default_order() -> String {
|
|
|
|
|
|
"name".to_owned()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl PageCursor for FolderResourceCursor {}
|
|
|
|
|
|
|
|
|
|
|
|
/// Query parameters for `GET /api/folders/{id}/resources`.
|
|
|
|
|
|
#[derive(Debug, Deserialize, IntoParams)]
|
|
|
|
|
|
pub struct FolderResourcesQuery {
|
|
|
|
|
|
/// Maximum items per page (1–200, default 50).
|
|
|
|
|
|
#[serde(default = "CursorQuery::default_limit")]
|
|
|
|
|
|
pub limit: u32,
|
|
|
|
|
|
/// Opaque cursor from a previous response. Omit to start from the top.
|
|
|
|
|
|
pub cursor: Option<String>,
|
|
|
|
|
|
/// Sort / group-by dimension. Supported: `"name"` (default), `"type"`,
|
|
|
|
|
|
/// `"modified_at"`, `"created_at"`, `"size"`.
|
|
|
|
|
|
pub order_by: Option<String>,
|
|
|
|
|
|
/// Comma-separated resource types to include, e.g. `"file,folder"`.
|
|
|
|
|
|
/// Omit to include both.
|
|
|
|
|
|
pub resource_types: Option<String>,
|
|
|
|
|
|
/// Reverse the sort order. Default `false` (normal order).
|
|
|
|
|
|
/// Must be the same on all pages of the same result set — the cursor
|
|
|
|
|
|
/// carries this flag so the server can validate consistency.
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
|
pub reverse: bool,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl FolderResourcesQuery {
|
|
|
|
|
|
/// Returns `limit` clamped to `[1, 200]`.
|
|
|
|
|
|
pub fn limit_clamped(&self) -> usize {
|
|
|
|
|
|
self.limit.clamp(1, 200) as usize
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Decode the optional cursor string. Invalid cursor → start from top.
|
|
|
|
|
|
pub fn decode_cursor(&self) -> Option<FolderResourceCursor> {
|
|
|
|
|
|
self.cursor
|
|
|
|
|
|
.as_deref()
|
|
|
|
|
|
.and_then(FolderResourceCursor::decode)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Parse `resource_types` into a `Vec<ResourceKind>`.
|
|
|
|
|
|
/// Returns `None` when the field is absent (= include all types).
|
|
|
|
|
|
pub fn resource_kinds(&self) -> Option<Vec<ResourceKind>> {
|
|
|
|
|
|
self.resource_types.as_deref().map(|s| {
|
|
|
|
|
|
s.split(',')
|
|
|
|
|
|
.filter_map(|t| ResourceKind::parse(t.trim()))
|
|
|
|
|
|
.collect()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Options for [`FolderService::list_resources_paged_with_perms`].
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Groups the optional parameters so the function stays within clippy's
|
|
|
|
|
|
/// `too_many_arguments` limit while remaining easy to extend.
|
|
|
|
|
|
pub struct ListResourcesOptions<'a> {
|
|
|
|
|
|
pub limit: usize,
|
|
|
|
|
|
pub cursor: Option<FolderResourceCursor>,
|
|
|
|
|
|
pub order_by: &'a str,
|
|
|
|
|
|
pub kinds: Option<&'a [ResourceKind]>,
|
|
|
|
|
|
pub reverse: bool,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// One item in a `/resources` page — a file or folder with a `resource_type` tag.
|
|
|
|
|
|
/// Re-uses [`ResourceContentDto`] so the shape is identical to `SharedWithMeItemDto.resource`.
|
|
|
|
|
|
#[derive(Debug, Serialize, ToSchema)]
|
|
|
|
|
|
pub struct FolderResourceItemDto {
|
|
|
|
|
|
pub resource_type: ResourceTypeDto,
|
|
|
|
|
|
/// Full resource details. Shape is determined by `resource_type`.
|
|
|
|
|
|
pub resource: ResourceContentDto,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Response envelope for `GET /api/folders/{id}/resources`.
|
|
|
|
|
|
pub type FolderResourcesDto = CursorListResponse<FolderResourceItemDto>;
|