Files
Oxicloud/src/domain/repositories/file_repository.rs
T

227 lines
7.9 KiB
Rust
Raw Normal View History

2025-03-17 21:28:08 +01:00
use async_trait::async_trait;
use crate::domain::entities::file::File;
2025-03-19 00:44:27 +01:00
use crate::domain::services::path_service::StoragePath;
2025-03-20 09:22:31 +01:00
use crate::common::errors::DomainError;
2025-03-19 00:44:27 +01:00
use futures::Stream;
use bytes::Bytes;
2025-03-17 21:28:08 +01:00
2025-03-26 19:08:07 +01:00
/**
* Comprehensive error types for file repository operations.
*
* This enum represents all possible error conditions that can occur during file repository
* operations, providing detailed context for error handling across the application.
*/
2025-03-17 21:28:08 +01:00
#[derive(Debug, thiserror::Error)]
#[allow(dead_code)]
pub enum FileRepositoryError {
2025-03-26 19:08:07 +01:00
/// Returned when a requested file cannot be found by ID or path
2025-03-17 21:28:08 +01:00
#[error("File not found: {0}")]
NotFound(String),
2025-03-26 19:08:07 +01:00
/// Returned when attempting to create a file at a location where one already exists
2025-03-17 21:28:08 +01:00
#[error("File already exists: {0}")]
AlreadyExists(String),
2025-03-26 19:08:07 +01:00
/// Returned when a provided file path is invalid or malformed
2025-03-17 21:28:08 +01:00
#[error("Invalid file path: {0}")]
InvalidPath(String),
2025-03-26 19:08:07 +01:00
/// Returned when an operation is not supported by the current implementation
#[error("Operation not supported: {0}")]
OperationNotSupported(String),
2025-03-26 19:08:07 +01:00
/// Wraps standard I/O errors from the filesystem
2025-03-17 21:28:08 +01:00
#[error("IO Error: {0}")]
IoError(#[from] std::io::Error),
2025-03-26 19:08:07 +01:00
/// Indicates errors in the path-to-ID mapping system
2025-03-19 00:44:27 +01:00
#[error("Mapping error: {0}")]
MappingError(String),
2025-03-26 19:08:07 +01:00
/// Specific errors related to ID mapping operations
2025-03-20 09:22:31 +01:00
#[error("ID Mapping error: {0}")]
IdMappingError(String),
2025-03-26 19:08:07 +01:00
/// Returned when an operation exceeds its timeout threshold
2025-03-19 00:44:27 +01:00
#[error("Timeout error: {0}")]
Timeout(String),
2025-03-26 19:08:07 +01:00
/// Propagates domain model errors to the repository layer
2025-03-20 09:22:31 +01:00
#[error("Domain error: {0}")]
DomainError(#[from] DomainError),
2025-03-26 19:08:07 +01:00
/// Catch-all for other unspecified errors
2025-03-17 21:28:08 +01:00
#[error("Other error: {0}")]
Other(String),
}
2025-03-26 19:08:07 +01:00
/**
* Type alias for results of file repository operations.
*
* Provides a consistent return type for all repository methods, containing
* either a successful value or a FileRepositoryError.
*/
2025-03-17 21:28:08 +01:00
pub type FileRepositoryResult<T> = Result<T, FileRepositoryError>;
2025-03-26 19:08:07 +01:00
/**
* Repository interface defining all file storage operations.
*
* This trait represents the primary port for file operations in the domain model,
* following the hexagonal architecture pattern. It defines the contract that any
* file storage implementation must fulfill, abstracting away implementation details
* like filesystem specifics, cloud storage, or database operations.
*
* All implementations must be thread-safe (Send + Sync) and have a 'static lifetime
* to support the async operations in the system.
*/
2025-03-17 21:28:08 +01:00
#[async_trait]
pub trait FileRepository: Send + Sync + 'static {
2025-03-26 19:08:07 +01:00
/**
* Creates and saves a new file from binary content.
*
* This method handles new file creation with automatic ID generation,
* content storage, and metadata registration.
*
* @param name The filename with extension
* @param folder_id Optional ID of parent folder, None for root
* @param content_type MIME type of the file
* @param content Binary data of the file
* @return A File entity with generated metadata on success, error otherwise
*/
2025-03-17 21:28:08 +01:00
async fn save_file_from_bytes(
&self,
name: String,
folder_id: Option<String>,
content_type: String,
content: Vec<u8>,
) -> FileRepositoryResult<File>;
2025-03-26 19:08:07 +01:00
/**
* Saves a file with a predetermined ID.
*
* Similar to save_file_from_bytes but allows specifying the ID,
* useful for restoring files or migrations.
*
* @param id Predefined unique ID for the file
* @param name The filename with extension
* @param folder_id Optional ID of parent folder, None for root
* @param content_type MIME type of the file
* @param content Binary data of the file
* @return The created File entity on success, error otherwise
*/
2025-03-17 21:28:08 +01:00
#[allow(dead_code)]
async fn save_file_with_id(
&self,
id: String,
name: String,
folder_id: Option<String>,
content_type: String,
content: Vec<u8>,
) -> FileRepositoryResult<File>;
2025-03-26 19:08:07 +01:00
/**
* Retrieves a file entity by its unique ID.
*
* @param id The unique identifier of the file
* @return The File entity if found, NotFound error otherwise
*/
2025-03-17 21:28:08 +01:00
async fn get_file_by_id(&self, id: &str) -> FileRepositoryResult<File>;
2025-03-26 19:08:07 +01:00
/**
* Lists all files within a specified folder.
*
* @param folder_id Optional folder ID to list files from, None for root
* @return Vector of File entities in the folder
*/
2025-03-17 21:28:08 +01:00
async fn list_files(&self, folder_id: Option<&str>) -> FileRepositoryResult<Vec<File>>;
2025-03-26 19:08:07 +01:00
/**
* Deletes a file by ID.
*
* @param id The unique identifier of the file to delete
* @return Success or error
*/
2025-03-17 21:28:08 +01:00
async fn delete_file(&self, id: &str) -> FileRepositoryResult<()>;
2025-03-26 19:08:07 +01:00
/**
* Deletes a file and removes its mapping entries.
*
* More thorough than delete_file as it also purges ID mappings,
* useful for permanent deletions.
*
* @param id The unique identifier of the file to delete
* @return Success or error
*/
2025-03-17 21:28:08 +01:00
#[allow(dead_code)]
async fn delete_file_entry(&self, id: &str) -> FileRepositoryResult<()>;
2025-03-26 19:08:07 +01:00
/**
* Retrieves the complete file content as a byte vector.
*
* This method loads the entire file into memory, so it should
* only be used for reasonably sized files.
*
* @param id The unique identifier of the file
* @return The file's binary content
*/
2025-03-17 21:28:08 +01:00
async fn get_file_content(&self, id: &str) -> FileRepositoryResult<Vec<u8>>;
2025-03-26 19:08:07 +01:00
/**
* Retrieves file content as an asynchronous stream of bytes.
*
* Preferred for large files as it avoids loading everything into memory at once.
*
* @param id The unique identifier of the file
* @return A stream that yields chunks of file data
*/
2025-03-19 00:44:27 +01:00
#[allow(clippy::type_complexity)]
async fn get_file_stream(&self, id: &str) -> FileRepositoryResult<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>>;
2025-03-26 19:08:07 +01:00
/**
* Moves a file to a different folder.
*
* @param id The unique identifier of the file to move
* @param target_folder_id The destination folder ID, None for root
* @return The updated File entity after the move
*/
2025-03-19 00:44:27 +01:00
async fn move_file(&self, id: &str, target_folder_id: Option<String>) -> FileRepositoryResult<File>;
2025-03-26 19:08:07 +01:00
/**
* Retrieves the storage path for a file.
*
* @param id The unique identifier of the file
* @return The StoragePath object representing the file's location
*/
2025-03-19 00:44:27 +01:00
async fn get_file_path(&self, id: &str) -> FileRepositoryResult<StoragePath>;
2025-03-26 19:08:07 +01:00
/**
* Moves a file to the trash system.
*
* Instead of permanent deletion, this marks the file as trashed
* and relocates it to the trash storage area.
*
* @param file_id The unique identifier of the file to trash
* @return Success or error
*/
async fn move_to_trash(&self, file_id: &str) -> FileRepositoryResult<()>;
2025-03-26 19:08:07 +01:00
/**
* Restores a file from the trash to its original location.
*
* @param file_id The unique identifier of the file to restore
* @param original_path The original path where the file was located before trashing
* @return Success or error
*/
async fn restore_from_trash(&self, file_id: &str, original_path: &str) -> FileRepositoryResult<()>;
2025-03-26 19:08:07 +01:00
/**
* Permanently deletes a file from the trash system.
*
* This operation is not reversible and removes the file completely.
* Used primarily by the trash cleanup service.
*
* @param file_id The unique identifier of the file to permanently delete
* @return Success or error
*/
async fn delete_file_permanently(&self, file_id: &str) -> FileRepositoryResult<()>;
2025-03-17 21:28:08 +01:00
}