2025-03-19 19:52:12 +01:00
|
|
|
|
use bytes::Bytes;
|
|
|
|
|
|
use futures::Stream;
|
2026-02-14 01:29:34 +01:00
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
use std::sync::Arc;
|
2026-03-07 14:59:32 +01:00
|
|
|
|
use uuid::Uuid;
|
2025-03-19 19:52:12 +01:00
|
|
|
|
|
|
|
|
|
|
use crate::application::dtos::file_dto::FileDto;
|
2026-02-24 19:28:00 +01:00
|
|
|
|
use crate::application::ports::storage_ports::CopyFolderTreeResult;
|
2026-03-03 15:36:42 +00:00
|
|
|
|
use crate::application::services::file_management_service::FileManagementService;
|
|
|
|
|
|
use crate::application::services::file_retrieval_service::FileRetrievalService;
|
|
|
|
|
|
use crate::application::services::file_upload_service::FileUploadService;
|
2026-03-04 23:55:08 +01:00
|
|
|
|
use crate::common::errors::DomainError;
|
2026-05-21 11:07:04 +02:00
|
|
|
|
use crate::domain::services::authorization::Permission;
|
2025-03-19 19:52:12 +01:00
|
|
|
|
|
2026-02-08 13:40:23 +01:00
|
|
|
|
// ─────────────────────────────────────────────────────
|
|
|
|
|
|
// Upload port
|
|
|
|
|
|
// ─────────────────────────────────────────────────────
|
|
|
|
|
|
|
2026-06-11 13:06:33 +00:00
|
|
|
|
/// A blob already stored in the content-addressable chunk store, carrying
|
|
|
|
|
|
/// exactly ONE reference that the receiving method takes ownership of.
|
2026-02-15 17:53:25 +01:00
|
|
|
|
///
|
2026-06-11 13:06:33 +00:00
|
|
|
|
/// Produced by the upload-ingest layer (`interfaces::upload_ingest`), which
|
|
|
|
|
|
/// streams the request body straight into the CDC dedup store. Methods that
|
|
|
|
|
|
/// accept a `StoredBlob` either attach the reference to a file row or
|
|
|
|
|
|
/// release it on failure — callers never need to compensate themselves.
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
|
pub struct StoredBlob {
|
|
|
|
|
|
/// BLAKE3 of the full content (manifest / blob key).
|
|
|
|
|
|
pub hash: String,
|
|
|
|
|
|
/// Content size in bytes.
|
|
|
|
|
|
pub size: u64,
|
|
|
|
|
|
/// `false` when the content already existed (dedup hit) — forwarded to
|
|
|
|
|
|
/// lifecycle hooks so e.g. thumbnails aren't regenerated for known blobs.
|
|
|
|
|
|
pub is_new_blob: bool,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Primary port for file upload operations.
|
2026-02-25 23:41:16 +01:00
|
|
|
|
///
|
2026-06-11 13:06:33 +00:00
|
|
|
|
/// **All upload paths converge on streaming-into-the-chunk-store** — content
|
|
|
|
|
|
/// never passes through this port; it is ingested by the interface layer
|
|
|
|
|
|
/// (CDC chunking + hashing while the body arrives, no spool file) and only
|
|
|
|
|
|
/// the resulting [`StoredBlob`] reference travels through here.
|
2025-03-19 19:52:12 +01:00
|
|
|
|
pub trait FileUploadUseCase: Send + Sync + 'static {
|
2026-06-11 13:06:33 +00:00
|
|
|
|
/// Register a new file row pointing at an already-ingested blob.
|
2026-02-15 17:53:25 +01:00
|
|
|
|
///
|
2026-06-11 13:06:33 +00:00
|
|
|
|
/// Takes ownership of the blob's reference (released on failure).
|
2026-02-15 17:53:25 +01:00
|
|
|
|
async fn upload_file_streaming(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
name: String,
|
|
|
|
|
|
folder_id: Option<String>,
|
|
|
|
|
|
content_type: String,
|
2026-06-11 13:06:33 +00:00
|
|
|
|
blob: StoredBlob,
|
2026-03-15 14:14:24 -04:00
|
|
|
|
) -> Result<FileDto, DomainError>;
|
2026-02-22 23:28:03 +01:00
|
|
|
|
|
2026-06-11 13:06:33 +00:00
|
|
|
|
/// Replace the content of the file at `path` with an already-ingested
|
|
|
|
|
|
/// blob, or create the file when it doesn't exist (WebDAV/WOPI PUT).
|
2026-02-22 23:28:03 +01:00
|
|
|
|
///
|
2026-06-11 13:06:33 +00:00
|
|
|
|
/// Takes ownership of the blob's reference (released on failure).
|
2026-02-22 23:28:03 +01:00
|
|
|
|
async fn update_file_streaming(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
path: &str,
|
2026-06-11 13:06:33 +00:00
|
|
|
|
blob: StoredBlob,
|
2026-02-22 23:28:03 +01:00
|
|
|
|
content_type: &str,
|
2026-03-15 14:14:24 -04:00
|
|
|
|
modified_at: Option<i64>,
|
|
|
|
|
|
) -> Result<FileDto, DomainError>;
|
2026-02-08 13:40:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ─────────────────────────────────────────────────────
|
|
|
|
|
|
// Retrieval / download port
|
|
|
|
|
|
// ─────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
/// Optimized file content returned by the retrieval service.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// The handler only needs to map each variant to the appropriate HTTP
|
|
|
|
|
|
/// response; all caching / transcoding / mmap decisions happen in the
|
|
|
|
|
|
/// application layer.
|
|
|
|
|
|
pub enum OptimizedFileContent {
|
|
|
|
|
|
/// Small-file content (possibly transcoded / compressed) already in RAM.
|
|
|
|
|
|
Bytes {
|
|
|
|
|
|
data: Bytes,
|
2026-03-03 15:55:15 +00:00
|
|
|
|
mime_type: Arc<str>,
|
2026-02-08 13:40:23 +01:00
|
|
|
|
was_transcoded: bool,
|
|
|
|
|
|
},
|
|
|
|
|
|
/// Memory-mapped file (10–100 MB).
|
|
|
|
|
|
Mmap(Bytes),
|
|
|
|
|
|
/// Streaming download for very large files (≥100 MB).
|
|
|
|
|
|
Stream(Pin<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>>),
|
2025-03-19 19:52:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
|
/// Primary port for file retrieval operations
|
2025-03-19 19:52:12 +01:00
|
|
|
|
pub trait FileRetrievalUseCase: Send + Sync + 'static {
|
2026-03-04 17:18:39 +01:00
|
|
|
|
/// Gets a file by its ID (system/internal — no ownership check).
|
2025-03-19 19:52:12 +01:00
|
|
|
|
async fn get_file(&self, id: &str) -> Result<FileDto, DomainError>;
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
2026-03-04 17:18:39 +01:00
|
|
|
|
/// Gets a file by its ID, enforcing that `caller_id` is the owner.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Returns `NotFound` if the file does not exist **or** belongs to
|
|
|
|
|
|
/// another user. All user-facing handlers should use this method.
|
2026-05-21 11:07:04 +02:00
|
|
|
|
async fn get_file_with_perms(&self, id: &str, caller_id: Uuid) -> Result<FileDto, DomainError>;
|
2026-03-04 17:18:39 +01:00
|
|
|
|
|
2026-05-22 00:38:44 +02:00
|
|
|
|
async fn get_file_or_trashed_with_perms(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
id: &str,
|
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
|
) -> Result<FileDto, DomainError>;
|
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
|
/// Gets a file by its path (for WebDAV)
|
2026-02-08 13:40:23 +01:00
|
|
|
|
async fn get_file_by_path(&self, path: &str) -> Result<FileDto, DomainError>;
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
|
/// Lists files in a folder
|
2025-03-19 19:52:12 +01:00
|
|
|
|
async fn list_files(&self, folder_id: Option<&str>) -> Result<Vec<FileDto>, DomainError>;
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
2026-03-05 10:30:39 +01:00
|
|
|
|
/// Lists files in a folder, scoped to the authenticated user.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Uses SQL-level `AND user_id` filtering — no in-memory post-filter.
|
|
|
|
|
|
/// All user-facing list handlers should use this method.
|
2026-05-21 11:07:04 +02:00
|
|
|
|
async fn list_files_with_perms(
|
2026-03-05 10:30:39 +01:00
|
|
|
|
&self,
|
|
|
|
|
|
folder_id: Option<&str>,
|
2026-03-07 14:59:32 +01:00
|
|
|
|
owner_id: Uuid,
|
2026-03-05 10:30:39 +01:00
|
|
|
|
) -> Result<Vec<FileDto>, DomainError>;
|
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
|
/// Gets file content as a stream (for large files)
|
2026-02-14 01:29:34 +01:00
|
|
|
|
async fn get_file_stream(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
id: &str,
|
|
|
|
|
|
) -> Result<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>, DomainError>;
|
2026-02-08 13:40:23 +01:00
|
|
|
|
|
2026-03-05 10:30:39 +01:00
|
|
|
|
/// Gets file content as a stream, enforcing that `caller_id` is the owner.
|
2026-05-21 11:07:04 +02:00
|
|
|
|
async fn get_file_stream_with_perms(
|
2026-03-05 10:30:39 +01:00
|
|
|
|
&self,
|
|
|
|
|
|
id: &str,
|
2026-03-07 14:59:32 +01:00
|
|
|
|
caller_id: Uuid,
|
2026-03-05 10:30:39 +01:00
|
|
|
|
) -> Result<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>, DomainError>;
|
|
|
|
|
|
|
2026-02-08 13:40:23 +01:00
|
|
|
|
/// Optimized multi-tier download.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Internalises: write-behind lookup → content-cache → WebP transcode →
|
|
|
|
|
|
/// mmap → streaming, returning an `OptimizedFileContent` variant so the
|
|
|
|
|
|
/// handler only builds the HTTP response.
|
|
|
|
|
|
async fn get_file_optimized(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
id: &str,
|
|
|
|
|
|
accept_webp: bool,
|
|
|
|
|
|
prefer_original: bool,
|
|
|
|
|
|
) -> Result<(FileDto, OptimizedFileContent), DomainError>;
|
|
|
|
|
|
|
2026-03-04 17:18:39 +01:00
|
|
|
|
/// Ownership-scoped optimized download.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Verifies `caller_id` owns the file before returning content.
|
|
|
|
|
|
/// All user-facing download handlers should use this.
|
2026-05-21 11:07:04 +02:00
|
|
|
|
async fn get_file_optimized_with_perms(
|
2026-03-04 17:18:39 +01:00
|
|
|
|
&self,
|
|
|
|
|
|
id: &str,
|
2026-03-07 14:59:32 +01:00
|
|
|
|
caller_id: Uuid,
|
2026-03-04 17:18:39 +01:00
|
|
|
|
accept_webp: bool,
|
|
|
|
|
|
prefer_original: bool,
|
|
|
|
|
|
) -> Result<(FileDto, OptimizedFileContent), DomainError>;
|
|
|
|
|
|
|
2026-02-15 17:53:25 +01:00
|
|
|
|
/// Like `get_file_optimized` but accepts an already-fetched `FileDto`,
|
|
|
|
|
|
/// avoiding a redundant metadata query when the handler already has it.
|
|
|
|
|
|
async fn get_file_optimized_preloaded(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
id: &str,
|
|
|
|
|
|
file_dto: FileDto,
|
|
|
|
|
|
accept_webp: bool,
|
|
|
|
|
|
prefer_original: bool,
|
|
|
|
|
|
) -> Result<(FileDto, OptimizedFileContent), DomainError> {
|
|
|
|
|
|
// Default: ignore pre-fetched meta, re-fetch everything.
|
|
|
|
|
|
let _ = file_dto;
|
|
|
|
|
|
self.get_file_optimized(id, accept_webp, prefer_original)
|
|
|
|
|
|
.await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-08 13:40:23 +01:00
|
|
|
|
/// Range-based streaming for HTTP Range Requests (video seek, resumable DL).
|
|
|
|
|
|
async fn get_file_range_stream(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
id: &str,
|
|
|
|
|
|
start: u64,
|
|
|
|
|
|
end: Option<u64>,
|
|
|
|
|
|
) -> Result<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>, DomainError>;
|
2026-02-24 12:18:38 +01:00
|
|
|
|
|
2026-03-04 17:18:39 +01:00
|
|
|
|
/// Ownership-scoped range stream — verifies caller owns the file first.
|
2026-05-21 11:07:04 +02:00
|
|
|
|
async fn get_file_range_stream_with_perms(
|
2026-03-04 17:18:39 +01:00
|
|
|
|
&self,
|
|
|
|
|
|
id: &str,
|
2026-03-07 14:59:32 +01:00
|
|
|
|
caller_id: Uuid,
|
2026-03-04 17:18:39 +01:00
|
|
|
|
start: u64,
|
|
|
|
|
|
end: Option<u64>,
|
|
|
|
|
|
) -> Result<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>, DomainError>;
|
|
|
|
|
|
|
2026-02-26 00:07:10 +01:00
|
|
|
|
/// Streams every file in the subtree rooted at `folder_id`.
|
2026-02-24 12:18:38 +01:00
|
|
|
|
///
|
2026-02-26 00:07:10 +01:00
|
|
|
|
/// Returns a streaming cursor — RAM stays O(1) per row. Callers
|
|
|
|
|
|
/// consume incrementally (e.g. group into a HashMap by folder_id)
|
|
|
|
|
|
/// without materializing the full result set.
|
|
|
|
|
|
async fn stream_files_in_subtree(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
folder_id: &str,
|
|
|
|
|
|
) -> Result<Pin<Box<dyn Stream<Item = Result<FileDto, DomainError>> + Send>>, DomainError>;
|
2026-02-24 15:11:56 +01:00
|
|
|
|
|
|
|
|
|
|
/// Lists files in a folder with LIMIT/OFFSET pagination.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Used by streaming WebDAV PROPFIND to avoid loading all files at once.
|
|
|
|
|
|
/// Default: falls back to `list_files` (loads all, then slices in memory).
|
|
|
|
|
|
async fn list_files_batch(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
folder_id: Option<&str>,
|
|
|
|
|
|
offset: i64,
|
|
|
|
|
|
limit: i64,
|
|
|
|
|
|
) -> Result<Vec<FileDto>, DomainError> {
|
|
|
|
|
|
let all = self.list_files(folder_id).await?;
|
2026-02-25 10:28:34 +01:00
|
|
|
|
Ok(all
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
.skip(offset as usize)
|
|
|
|
|
|
.take(limit as usize)
|
|
|
|
|
|
.collect())
|
2026-02-24 15:11:56 +01:00
|
|
|
|
}
|
2026-03-05 10:30:39 +01:00
|
|
|
|
|
|
|
|
|
|
/// Like [`list_files_batch`], but scoped to a specific owner.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Used by streaming WebDAV PROPFIND so that each user only sees their
|
|
|
|
|
|
/// own files, even in shared folder_id namespaces.
|
2026-05-21 11:07:04 +02:00
|
|
|
|
async fn list_files_batch_with_perms(
|
2026-03-05 10:30:39 +01:00
|
|
|
|
&self,
|
|
|
|
|
|
folder_id: Option<&str>,
|
2026-03-07 14:59:32 +01:00
|
|
|
|
owner_id: Uuid,
|
2026-03-05 10:30:39 +01:00
|
|
|
|
offset: i64,
|
|
|
|
|
|
limit: i64,
|
|
|
|
|
|
) -> Result<Vec<FileDto>, DomainError> {
|
|
|
|
|
|
let all = self.list_files_batch(folder_id, offset, limit).await?;
|
2026-03-07 14:59:32 +01:00
|
|
|
|
let owner_str = owner_id.to_string();
|
2026-03-05 10:30:39 +01:00
|
|
|
|
Ok(all
|
|
|
|
|
|
.into_iter()
|
2026-03-07 14:59:32 +01:00
|
|
|
|
.filter(|f| f.owner_id.as_deref().is_some_and(|o| o == owner_str))
|
2026-03-05 10:30:39 +01:00
|
|
|
|
.collect())
|
|
|
|
|
|
}
|
2025-03-19 19:52:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
|
/// Primary port for file management operations
|
2025-03-19 19:52:12 +01:00
|
|
|
|
pub trait FileManagementUseCase: Send + Sync + 'static {
|
2026-05-21 21:50:42 +02:00
|
|
|
|
async fn require_permission(
|
2026-05-21 11:07:04 +02:00
|
|
|
|
&self,
|
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
|
permission: Permission,
|
|
|
|
|
|
file_id: &str,
|
|
|
|
|
|
) -> Result<(), DomainError>;
|
|
|
|
|
|
|
2026-03-04 17:18:39 +01:00
|
|
|
|
/// Moves a file, enforcing that `caller_id` is the owner.
|
2026-05-20 15:39:53 +02:00
|
|
|
|
async fn move_file_with_perms(
|
2026-03-04 17:18:39 +01:00
|
|
|
|
&self,
|
|
|
|
|
|
file_id: &str,
|
2026-03-07 14:59:32 +01:00
|
|
|
|
caller_id: Uuid,
|
2026-03-04 17:18:39 +01:00
|
|
|
|
folder_id: Option<String>,
|
|
|
|
|
|
) -> Result<FileDto, DomainError>;
|
|
|
|
|
|
|
2026-03-05 10:30:39 +01:00
|
|
|
|
/// Copies a file, enforcing that `caller_id` is the owner.
|
2026-05-20 15:39:53 +02:00
|
|
|
|
async fn copy_file_with_perms(
|
2026-03-05 10:30:39 +01:00
|
|
|
|
&self,
|
|
|
|
|
|
file_id: &str,
|
2026-03-07 14:59:32 +01:00
|
|
|
|
caller_id: Uuid,
|
2026-03-05 10:30:39 +01:00
|
|
|
|
target_folder_id: Option<String>,
|
|
|
|
|
|
) -> Result<FileDto, DomainError>;
|
|
|
|
|
|
|
2026-03-04 17:18:39 +01:00
|
|
|
|
/// Renames a file, enforcing that `caller_id` is the owner.
|
2026-05-20 15:39:53 +02:00
|
|
|
|
async fn rename_file_with_perms(
|
2026-03-04 17:18:39 +01:00
|
|
|
|
&self,
|
|
|
|
|
|
file_id: &str,
|
2026-03-07 14:59:32 +01:00
|
|
|
|
caller_id: Uuid,
|
2026-03-04 17:18:39 +01:00
|
|
|
|
new_name: &str,
|
|
|
|
|
|
) -> Result<FileDto, DomainError>;
|
|
|
|
|
|
|
2026-03-05 10:30:39 +01:00
|
|
|
|
/// Deletes a file, enforcing that `caller_id` is the owner.
|
2026-05-20 15:39:53 +02:00
|
|
|
|
async fn delete_file_with_perms(&self, id: &str, caller_id: Uuid) -> Result<(), DomainError>;
|
2026-03-05 10:30:39 +01:00
|
|
|
|
|
2026-02-08 13:40:23 +01:00
|
|
|
|
/// Smart delete: trash-first with dedup reference cleanup.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 1. Tries to move to trash (soft delete).
|
|
|
|
|
|
/// 2. Falls back to permanent delete if trash unavailable/failed.
|
|
|
|
|
|
/// 3. Decrements the dedup reference count for the content hash.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Returns `Ok(true)` when trashed, `Ok(false)` when permanently deleted.
|
2026-05-20 15:39:53 +02:00
|
|
|
|
async fn delete_and_cleanup_with_perms(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
id: &str,
|
|
|
|
|
|
user_id: Uuid,
|
|
|
|
|
|
) -> Result<bool, DomainError>;
|
2026-02-24 19:28:00 +01:00
|
|
|
|
|
|
|
|
|
|
/// Copies an entire folder subtree atomically (WebDAV COPY Depth: infinity).
|
2026-05-20 15:39:53 +02:00
|
|
|
|
/// enforcing that `caller_id` owns both the source folder
|
|
|
|
|
|
/// and the target parent folder.
|
2026-02-24 19:28:00 +01:00
|
|
|
|
///
|
|
|
|
|
|
/// Creates a copy of `source_folder_id` (with optional name override) under
|
|
|
|
|
|
/// `target_parent_id`, including ALL sub-folders and files. Files are
|
|
|
|
|
|
/// zero-copy (blob ref_counts incremented in batch).
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Default: returns error (only available with PostgreSQL backend).
|
2026-05-20 15:39:53 +02:00
|
|
|
|
async fn copy_folder_tree_with_perms(
|
2026-05-11 22:58:33 +02:00
|
|
|
|
&self,
|
|
|
|
|
|
source_folder_id: &str,
|
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
|
target_parent_id: Option<String>,
|
|
|
|
|
|
dest_name: Option<String>,
|
|
|
|
|
|
) -> Result<CopyFolderTreeResult, DomainError>;
|
2025-03-19 19:52:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
|
/// Factory for creating file use case implementations
|
2025-03-20 09:22:31 +01:00
|
|
|
|
pub trait FileUseCaseFactory: Send + Sync + 'static {
|
2026-03-03 15:36:42 +00:00
|
|
|
|
fn create_file_upload_use_case(&self) -> Arc<FileUploadService>;
|
|
|
|
|
|
fn create_file_retrieval_use_case(&self) -> Arc<FileRetrievalService>;
|
|
|
|
|
|
fn create_file_management_use_case(&self) -> Arc<FileManagementService>;
|
2026-02-14 01:29:34 +01:00
|
|
|
|
}
|