Files
Oxicloud/src/domain/entities/folder.rs
T

302 lines
8.5 KiB
Rust
Raw Normal View History

2025-03-19 00:44:27 +01:00
use crate::domain::services::path_service::StoragePath;
/// Error in the creation or manipulation of folder entities
2025-03-19 00:44:27 +01:00
#[derive(Debug, thiserror::Error)]
pub enum FolderError {
#[error("Invalid folder name: {0}")]
2025-03-19 00:44:27 +01:00
InvalidFolderName(String),
#[error("Validation error: {0}")]
2025-03-19 00:44:27 +01:00
ValidationError(String),
}
/// Result type for folder entity operations
2025-03-19 00:44:27 +01:00
pub type FolderResult<T> = Result<T, FolderError>;
2025-03-17 21:28:08 +01:00
/// Represents a folder entity in the domain
#[derive(Debug, Clone, PartialEq, Eq)]
2025-03-17 21:28:08 +01:00
pub struct Folder {
/// Unique identifier for the folder
2025-03-19 00:44:27 +01:00
id: String,
2025-03-17 21:28:08 +01:00
/// Name of the folder
2025-03-19 00:44:27 +01:00
name: String,
/// Path to the folder in the domain model
storage_path: StoragePath,
2025-03-17 21:28:08 +01:00
/// String representation of the path (for API compatibility)
2025-03-19 00:44:27 +01:00
path_string: String,
2025-03-17 21:28:08 +01:00
/// Parent folder ID (None if it's a root folder)
2025-03-19 00:44:27 +01:00
parent_id: Option<String>,
2025-03-17 21:28:08 +01:00
/// Creation timestamp
2025-03-19 00:44:27 +01:00
created_at: u64,
2025-03-17 21:28:08 +01:00
/// Last modification timestamp
2025-03-19 00:44:27 +01:00
modified_at: u64,
2025-03-17 21:28:08 +01:00
}
// We no longer need this module, now we use a String directly
2025-03-19 00:44:27 +01:00
2025-03-20 09:22:31 +01:00
impl Default for Folder {
fn default() -> Self {
Self {
id: "stub-id".to_string(),
name: "stub-folder".to_string(),
storage_path: StoragePath::from_string("/"),
path_string: "/".to_string(),
parent_id: None,
created_at: 0,
modified_at: 0,
}
}
}
2025-03-17 21:28:08 +01:00
impl Folder {
2025-03-19 00:44:27 +01:00
/// Creates a new folder with validation
pub fn new(
id: String,
name: String,
storage_path: StoragePath,
parent_id: Option<String>,
) -> FolderResult<Self> {
// Validate folder name
2025-03-19 00:44:27 +01:00
if name.is_empty() || name.contains('/') || name.contains('\\') {
return Err(FolderError::InvalidFolderName(name));
}
2025-03-17 21:28:08 +01:00
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
2025-03-19 00:44:27 +01:00
.unwrap_or_default()
2025-03-17 21:28:08 +01:00
.as_secs();
2025-03-19 00:44:27 +01:00
// Store the path string for serialization compatibility
2025-03-19 00:44:27 +01:00
let path_string = storage_path.to_string();
2025-03-17 21:28:08 +01:00
2025-03-19 00:44:27 +01:00
Ok(Self {
2025-03-17 21:28:08 +01:00
id,
name,
2025-03-19 00:44:27 +01:00
storage_path,
path_string,
2025-03-17 21:28:08 +01:00
parent_id,
created_at: now,
modified_at: now,
2025-03-19 00:44:27 +01:00
})
}
/// Creates a folder with specific timestamps (for reconstruction)
pub fn with_timestamps(
id: String,
name: String,
storage_path: StoragePath,
parent_id: Option<String>,
created_at: u64,
modified_at: u64,
) -> FolderResult<Self> {
// Validate folder name
2025-03-19 00:44:27 +01:00
if name.is_empty() || name.contains('/') || name.contains('\\') {
return Err(FolderError::InvalidFolderName(name));
2025-03-17 21:28:08 +01:00
}
2025-03-19 00:44:27 +01:00
// Store the path string for serialization compatibility
2025-03-19 00:44:27 +01:00
let path_string = storage_path.to_string();
Ok(Self {
id,
name,
storage_path,
path_string,
parent_id,
created_at,
modified_at,
})
2025-03-17 21:28:08 +01:00
}
2025-03-19 00:44:27 +01:00
// Getters
pub fn id(&self) -> &str {
&self.id
2025-03-17 21:28:08 +01:00
}
2025-03-19 00:44:27 +01:00
pub fn name(&self) -> &str {
&self.name
}
pub fn storage_path(&self) -> &StoragePath {
&self.storage_path
}
pub fn path_string(&self) -> &str {
&self.path_string
}
pub fn parent_id(&self) -> Option<&str> {
self.parent_id.as_deref()
}
pub fn created_at(&self) -> u64 {
self.created_at
}
pub fn modified_at(&self) -> u64 {
self.modified_at
}
/// Creates a new Folder instance from a DTO
/// This function is primarily for conversions in batch handlers
2025-03-19 00:44:27 +01:00
pub fn from_dto(
id: String,
name: String,
path: String,
parent_id: Option<String>,
created_at: u64,
modified_at: u64,
) -> Self {
// Create storage_path from the string
2025-03-19 00:44:27 +01:00
let storage_path = StoragePath::from_string(&path);
// Create directly without validation to avoid errors in DTO conversions
2025-03-19 00:44:27 +01:00
Self {
id,
name,
storage_path,
path_string: path,
parent_id,
created_at,
modified_at,
}
}
// Methods to create new versions of the folder (immutable)
2025-03-19 00:44:27 +01:00
/// Creates a new version of the folder with updated name
pub fn with_name(&self, new_name: String) -> FolderResult<Self> {
// Validate folder name
2025-03-19 00:44:27 +01:00
if new_name.is_empty() || new_name.contains('/') || new_name.contains('\\') {
return Err(FolderError::InvalidFolderName(new_name));
}
// Update path based on the name
2025-03-19 00:44:27 +01:00
let parent_path = self.storage_path.parent();
let new_storage_path = match parent_path {
Some(parent) => parent.join(&new_name),
None => StoragePath::from_string(&new_name),
};
// Update string representation
2025-03-19 00:44:27 +01:00
let new_path_string = new_storage_path.to_string();
let now = std::time::SystemTime::now()
2025-03-17 21:28:08 +01:00
.duration_since(std::time::UNIX_EPOCH)
2025-03-19 00:44:27 +01:00
.unwrap_or_default()
2025-03-17 21:28:08 +01:00
.as_secs();
2025-03-19 00:44:27 +01:00
Ok(Self {
id: self.id.clone(),
name: new_name,
storage_path: new_storage_path,
path_string: new_path_string,
parent_id: self.parent_id.clone(),
created_at: self.created_at,
modified_at: now,
})
}
/// Creates a new version of the folder with updated parent
pub fn with_parent(&self, parent_id: Option<String>, parent_path: Option<StoragePath>) -> FolderResult<Self> {
// We need a folder path to update the path
2025-03-19 00:44:27 +01:00
let new_storage_path = match parent_path {
Some(path) => path.join(&self.name),
None => StoragePath::from_string(&self.name), // Root
2025-03-19 00:44:27 +01:00
};
// Update string representation
2025-03-19 00:44:27 +01:00
let new_path_string = new_storage_path.to_string();
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
Ok(Self {
id: self.id.clone(),
name: self.name.clone(),
storage_path: new_storage_path,
path_string: new_path_string,
parent_id,
created_at: self.created_at,
modified_at: now,
})
}
/// Returns an absolute path for this folder
pub fn get_absolute_path<P: AsRef<std::path::Path>>(&self, root_path: P) -> std::path::PathBuf {
let mut result = std::path::PathBuf::from(root_path.as_ref());
// Skip leading '/' from path_string to avoid creating absolute path incorrectly
let relative_path = if self.path_string.starts_with('/') {
&self.path_string[1..]
} else {
&self.path_string
};
if !relative_path.is_empty() {
result.push(relative_path);
}
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_folder_creation_with_valid_name() {
let storage_path = StoragePath::from_string("/test/folder");
let folder = Folder::new(
"123".to_string(),
"my_folder".to_string(),
storage_path,
None,
);
assert!(folder.is_ok());
}
#[test]
fn test_folder_creation_with_invalid_name() {
let storage_path = StoragePath::from_string("/test/invalid/folder");
let folder = Folder::new(
"123".to_string(),
"folder/with/slash".to_string(), // Invalid name
2025-03-19 00:44:27 +01:00
storage_path,
None,
);
assert!(folder.is_err());
match folder {
Err(FolderError::InvalidFolderName(_)) => (),
_ => panic!("Expected InvalidFolderName error"),
}
}
#[test]
fn test_folder_with_name() {
let storage_path = StoragePath::from_string("/test/folder");
let folder = Folder::new(
"123".to_string(),
"old_name".to_string(),
storage_path,
None,
).unwrap();
let renamed = folder.with_name("new_name".to_string());
assert!(renamed.is_ok());
let renamed = renamed.unwrap();
assert_eq!(renamed.name(), "new_name");
assert_eq!(renamed.id(), "123"); // The ID doesn't change
2025-03-17 21:28:08 +01:00
}
}