Files
Oxicloud/src/application/services/file_management_service.rs
T

164 lines
5.3 KiB
Rust
Raw Normal View History

2025-03-19 19:52:12 +01:00
use async_trait::async_trait;
2026-02-14 01:29:34 +01:00
use std::sync::Arc;
2025-03-19 19:52:12 +01:00
use crate::application::dtos::file_dto::FileDto;
2026-02-14 01:29:34 +01:00
use crate::application::ports::file_ports::FileManagementUseCase;
use crate::application::ports::storage_ports::FileWritePort;
use crate::application::ports::trash_ports::TrashUseCase;
2025-03-19 19:52:12 +01:00
use crate::common::errors::DomainError;
use tracing::{error, info, warn};
2025-03-19 19:52:12 +01:00
/// Service for file management operations (move, delete).
///
/// Blob ref_count bookkeeping on deletion is handled by the PG trigger
/// `trg_files_decrement_blob_ref` (fires on DELETE FROM storage.files).
/// This service only orchestrates trash vs. permanent delete — it never
/// touches ref_count directly.
2025-03-19 19:52:12 +01:00
pub struct FileManagementService {
file_repository: Arc<dyn FileWritePort>,
trash_service: Option<Arc<dyn TrashUseCase>>,
2025-03-19 19:52:12 +01:00
}
impl FileManagementService {
/// Creates a new FileManagementService.
2025-03-19 19:52:12 +01:00
pub fn new(file_repository: Arc<dyn FileWritePort>) -> Self {
Self {
file_repository,
trash_service: None,
}
}
/// Creates a FileManagementService with a trash service.
pub fn with_trash(
file_repository: Arc<dyn FileWritePort>,
trash_service: Option<Arc<dyn TrashUseCase>>,
) -> Self {
Self {
file_repository,
trash_service,
}
2025-03-19 19:52:12 +01:00
}
}
#[async_trait]
impl FileManagementUseCase for FileManagementService {
async fn move_file(
&self,
file_id: &str,
folder_id: Option<String>,
) -> Result<FileDto, DomainError> {
2026-02-14 01:29:34 +01:00
info!(
"Moving file with ID: {} to folder: {:?}",
file_id, folder_id
);
2026-02-14 01:29:34 +01:00
let moved_file = self
.file_repository
.move_file(file_id, folder_id)
.await
.map_err(|e| {
error!("Error moving file (ID: {}): {}", file_id, e);
e
})?;
info!(
"File moved successfully: {} (ID: {}) to folder: {:?}",
moved_file.name(),
moved_file.id(),
moved_file.folder_id()
);
2025-03-19 19:52:12 +01:00
Ok(FileDto::from(moved_file))
}
2026-02-14 20:22:19 +01:00
async fn copy_file(
&self,
file_id: &str,
target_folder_id: Option<String>,
) -> Result<FileDto, DomainError> {
info!(
"Copying file with ID: {} to folder: {:?}",
file_id, target_folder_id
);
let copied_file = self
.file_repository
.copy_file(file_id, target_folder_id)
.await
.map_err(|e| {
error!("Error copying file (ID: {}): {}", file_id, e);
e
})?;
info!(
"File copied successfully: {} (ID: {}) to folder: {:?}",
copied_file.name(),
copied_file.id(),
copied_file.folder_id()
);
Ok(FileDto::from(copied_file))
}
2026-02-14 01:29:34 +01:00
async fn rename_file(&self, file_id: &str, new_name: &str) -> Result<FileDto, DomainError> {
2026-02-08 22:44:42 +01:00
info!("Renaming file with ID: {} to \"{}\"", file_id, new_name);
2026-02-14 01:29:34 +01:00
let renamed_file = self
.file_repository
.rename_file(file_id, new_name)
.await
.map_err(|e| {
error!("Error renaming file (ID: {}): {}", file_id, e);
e
})?;
2026-02-08 22:44:42 +01:00
info!(
"File renamed successfully: {} (ID: {})",
renamed_file.name(),
renamed_file.id()
);
Ok(FileDto::from(renamed_file))
}
2025-03-19 19:52:12 +01:00
async fn delete_file(&self, id: &str) -> Result<(), DomainError> {
self.file_repository.delete_file(id).await
}
/// Smart delete: trash-first with dedup reference cleanup.
///
/// Blob ref_count bookkeeping is handled entirely by the PG trigger
/// `trg_files_decrement_blob_ref` which fires on DELETE FROM storage.files.
/// We do NOT decrement here — trashing is a soft-delete (UPDATE, not DELETE)
/// so the blob must remain referenced until the file is permanently deleted.
2026-02-14 01:29:34 +01:00
async fn delete_with_cleanup(&self, id: &str, user_id: &str) -> Result<bool, DomainError> {
// Step 1: Try trash (soft delete — file row stays, blob stays referenced)
if let Some(trash) = &self.trash_service {
info!("Moving file to trash: {}", id);
match trash.move_to_trash(id, "file", user_id).await {
Ok(_) => {
info!("File successfully moved to trash: {}", id);
// Do NOT decrement blob ref here — the file row still exists
// (is_trashed = TRUE). The trigger will decrement when the
// row is actually DELETEd during trash emptying.
return Ok(true); // trashed
}
Err(err) => {
error!("Could not move file to trash: {:?}", err);
warn!("Falling back to permanent delete");
// fall through
}
}
} else {
warn!("Trash service not available, using permanent delete");
}
// Step 2: Permanent delete — trigger handles blob ref_count
warn!("Permanently deleting file: {}", id);
self.file_repository.delete_file(id).await?;
info!("File permanently deleted: {}", id);
Ok(false) // permanently deleted
}
2026-02-14 01:29:34 +01:00
}