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-05-20 22:56:00 +02:00
|
|
|
use crate::application::ports::authorization_ports::AuthorizationEngine;
|
2026-05-22 13:10:47 +02:00
|
|
|
use crate::application::ports::file_lifecycle::FileLifecycleHook;
|
2026-02-14 01:29:34 +01:00
|
|
|
use crate::application::ports::file_ports::FileManagementUseCase;
|
2026-06-26 01:01:44 +02:00
|
|
|
use crate::application::ports::resource_access_hook::ResourceAccessHook;
|
2026-05-20 22:56:00 +02:00
|
|
|
use crate::application::ports::storage_ports::{CopyFolderTreeResult, FileWritePort};
|
2026-02-08 13:40:23 +01:00
|
|
|
use crate::application::ports::trash_ports::TrashUseCase;
|
2026-03-04 23:55:08 +01:00
|
|
|
use crate::application::services::trash_service::TrashService;
|
2025-03-19 19:52:12 +01:00
|
|
|
use crate::common::errors::DomainError;
|
2026-05-20 22:56:00 +02:00
|
|
|
use crate::domain::services::authorization::{Permission, Resource, Subject};
|
2026-05-09 00:06:30 +02:00
|
|
|
use crate::domain::services::path_service::validate_storage_name;
|
2026-03-04 17:18:39 +01:00
|
|
|
use crate::infrastructure::repositories::pg::file_blob_read_repository::FileBlobReadRepository;
|
2026-03-03 15:36:42 +00:00
|
|
|
use crate::infrastructure::repositories::pg::file_blob_write_repository::FileBlobWriteRepository;
|
2026-03-17 16:53:55 +08:00
|
|
|
use crate::infrastructure::repositories::pg::folder_db_repository::FolderDbRepository;
|
2026-04-11 17:39:17 +02:00
|
|
|
use crate::infrastructure::services::file_content_cache::FileContentCache;
|
2026-05-20 22:56:00 +02:00
|
|
|
use crate::infrastructure::services::pg_acl_engine::PgAclEngine;
|
2026-03-04 23:55:08 +01:00
|
|
|
use tracing::{error, info, warn};
|
2026-03-07 14:59:32 +01:00
|
|
|
use uuid::Uuid;
|
2025-03-19 19:52:12 +01:00
|
|
|
|
2026-02-08 13:40:23 +01:00
|
|
|
/// Service for file management operations (move, delete).
|
|
|
|
|
///
|
2026-02-22 22:07:46 +01:00
|
|
|
/// 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 {
|
2026-03-03 15:36:42 +00:00
|
|
|
file_repository: Arc<FileBlobWriteRepository>,
|
|
|
|
|
trash_service: Option<Arc<TrashService>>,
|
2026-04-11 17:39:17 +02:00
|
|
|
content_cache: Option<Arc<FileContentCache>>,
|
2026-05-20 22:56:00 +02:00
|
|
|
authz: Arc<PgAclEngine>,
|
2026-05-22 13:10:47 +02:00
|
|
|
/// Lifecycle hook dispatcher — fired on file created (copy) and deleted.
|
|
|
|
|
file_lifecycle_hook: Option<Arc<dyn FileLifecycleHook>>,
|
2026-06-26 01:01:44 +02:00
|
|
|
/// Read/write access hook — fired so Recent reflects "this is the file
|
|
|
|
|
/// I just copied / renamed / moved", same way the read paths surface
|
|
|
|
|
/// downloads. Distinct from the lifecycle hook because lifecycle hooks
|
|
|
|
|
/// don't carry the `caller_id` the recording side needs.
|
|
|
|
|
resource_access_hook: Option<Arc<dyn ResourceAccessHook>>,
|
2026-06-26 01:48:39 +02:00
|
|
|
/// Drive repository — used by D5's `forbid_cross_drive_move` gate
|
|
|
|
|
/// on `move_file_with_perms`. Optional so stubs / test factories
|
|
|
|
|
/// can build the service without wiring the full drive repo; in
|
|
|
|
|
/// that case the cross-drive move check is skipped (the policy
|
|
|
|
|
/// is silently off). Production DI wires it in.
|
|
|
|
|
drive_repo: Option<Arc<dyn crate::domain::repositories::drive_repository::DriveRepository>>,
|
2025-03-19 19:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FileManagementService {
|
2026-05-20 22:56:00 +02:00
|
|
|
/// Creates a FileManagementService with a trash service, content cache
|
|
|
|
|
/// and the ReBAC authorization engine. File/folder owner lookups (used
|
|
|
|
|
/// for owner short-circuit inside the engine) are now the engine's
|
|
|
|
|
/// responsibility — this service no longer holds direct repo references
|
|
|
|
|
/// for ownership.
|
2026-02-22 22:07:46 +01:00
|
|
|
pub fn with_trash(
|
2026-03-03 15:36:42 +00:00
|
|
|
file_repository: Arc<FileBlobWriteRepository>,
|
|
|
|
|
trash_service: Option<Arc<TrashService>>,
|
2026-05-20 22:56:00 +02:00
|
|
|
_file_read: Option<Arc<FileBlobReadRepository>>,
|
|
|
|
|
_folder_repo: Option<Arc<FolderDbRepository>>,
|
2026-04-11 17:39:17 +02:00
|
|
|
content_cache: Option<Arc<FileContentCache>>,
|
2026-05-20 22:56:00 +02:00
|
|
|
authz: Arc<PgAclEngine>,
|
2026-02-08 13:40:23 +01:00
|
|
|
) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
file_repository,
|
|
|
|
|
trash_service,
|
2026-04-11 17:39:17 +02:00
|
|
|
content_cache,
|
2026-05-20 22:56:00 +02:00
|
|
|
authz,
|
2026-05-22 13:10:47 +02:00
|
|
|
file_lifecycle_hook: None,
|
2026-06-26 01:01:44 +02:00
|
|
|
resource_access_hook: None,
|
2026-06-26 01:48:39 +02:00
|
|
|
drive_repo: None,
|
2026-02-08 13:40:23 +01:00
|
|
|
}
|
2025-03-19 19:52:12 +01:00
|
|
|
}
|
2026-03-04 17:18:39 +01:00
|
|
|
|
2026-05-22 13:10:47 +02:00
|
|
|
/// Sets the lifecycle hook dispatcher (thumbnails, audio metadata, …).
|
|
|
|
|
pub fn with_file_lifecycle_hook(mut self, hook: Arc<dyn FileLifecycleHook>) -> Self {
|
|
|
|
|
self.file_lifecycle_hook = Some(hook);
|
2026-05-14 00:03:03 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-26 01:01:44 +02:00
|
|
|
/// Registers the read/write access hook (Recent list recorder).
|
|
|
|
|
pub fn with_resource_access_hook(mut self, hook: Arc<dyn ResourceAccessHook>) -> Self {
|
|
|
|
|
self.resource_access_hook = Some(hook);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Internal helper: fire the access hook if registered.
|
|
|
|
|
fn notify_file_accessed(&self, caller_id: Uuid, file_id: &str) {
|
|
|
|
|
if let Some(hook) = &self.resource_access_hook {
|
|
|
|
|
hook.on_file_accessed(caller_id, file_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-26 01:48:39 +02:00
|
|
|
/// Wires the drive repository, enabling D5 `forbid_cross_drive_move`
|
|
|
|
|
/// enforcement on `move_file_with_perms`. Without it, the gate is
|
|
|
|
|
/// silently skipped.
|
|
|
|
|
pub fn with_drive_repo(
|
|
|
|
|
mut self,
|
|
|
|
|
drive_repo: Arc<dyn crate::domain::repositories::drive_repository::DriveRepository>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
self.drive_repo = Some(drive_repo);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 22:56:00 +02:00
|
|
|
/// Engine check for a file resource. Parses the id into a `Uuid` and
|
|
|
|
|
/// requires the specified permission.
|
|
|
|
|
async fn require_file_perm(
|
|
|
|
|
&self,
|
|
|
|
|
file_id: &str,
|
|
|
|
|
perm: Permission,
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
) -> Result<(), DomainError> {
|
|
|
|
|
let uuid = Uuid::parse_str(file_id).map_err(|_| DomainError::not_found("File", file_id))?;
|
|
|
|
|
self.authz
|
|
|
|
|
.require(Subject::User(caller_id), perm, Resource::File(uuid))
|
|
|
|
|
.await
|
2026-03-04 17:18:39 +01:00
|
|
|
}
|
2026-03-17 16:53:55 +08:00
|
|
|
|
2026-05-20 22:56:00 +02:00
|
|
|
/// Engine check for a target folder. `None` is allowed (root namespace,
|
|
|
|
|
/// implicitly owned by the caller).
|
|
|
|
|
async fn require_target_folder_perm(
|
2026-03-17 16:53:55 +08:00
|
|
|
&self,
|
2026-05-20 12:48:06 +02:00
|
|
|
folder_id: Option<&str>,
|
2026-05-20 22:56:00 +02:00
|
|
|
perm: Permission,
|
2026-03-17 16:53:55 +08:00
|
|
|
caller_id: Uuid,
|
|
|
|
|
) -> Result<(), DomainError> {
|
2026-05-20 12:48:06 +02:00
|
|
|
let Some(target) = folder_id else {
|
|
|
|
|
return Ok(());
|
2026-03-17 16:53:55 +08:00
|
|
|
};
|
2026-05-20 22:56:00 +02:00
|
|
|
let uuid = Uuid::parse_str(target).map_err(|_| DomainError::not_found("Folder", target))?;
|
|
|
|
|
self.authz
|
|
|
|
|
.require(Subject::User(caller_id), perm, Resource::Folder(uuid))
|
|
|
|
|
.await
|
2026-03-17 16:53:55 +08:00
|
|
|
}
|
2025-03-19 19:52:12 +01:00
|
|
|
|
2026-05-20 15:39:53 +02:00
|
|
|
//impl FileManagementPrivateUseCase for FileManagementService {
|
2026-02-08 13:40:23 +01:00
|
|
|
async fn move_file(
|
|
|
|
|
&self,
|
|
|
|
|
file_id: &str,
|
|
|
|
|
folder_id: Option<String>,
|
2026-06-19 10:51:13 +02:00
|
|
|
caller_id: Uuid,
|
2026-02-08 13:40:23 +01:00
|
|
|
) -> Result<FileDto, DomainError> {
|
2026-02-14 01:29:34 +01:00
|
|
|
info!(
|
|
|
|
|
"Moving file with ID: {} to folder: {:?}",
|
|
|
|
|
file_id, folder_id
|
|
|
|
|
);
|
2026-02-08 13:40:23 +01:00
|
|
|
|
2026-02-14 01:29:34 +01:00
|
|
|
let moved_file = self
|
|
|
|
|
.file_repository
|
2026-06-19 10:51:13 +02:00
|
|
|
.move_file(file_id, folder_id, caller_id)
|
2026-02-14 01:29:34 +01:00
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
error!("Error moving file (ID: {}): {}", file_id, e);
|
|
|
|
|
e
|
|
|
|
|
})?;
|
2026-02-08 13:40:23 +01:00
|
|
|
|
|
|
|
|
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-08 13:40:23 +01:00
|
|
|
|
2026-02-14 20:22:19 +01:00
|
|
|
async fn copy_file(
|
|
|
|
|
&self,
|
|
|
|
|
file_id: &str,
|
|
|
|
|
target_folder_id: Option<String>,
|
2026-06-17 01:43:13 +02:00
|
|
|
new_name: Option<&str>,
|
2026-06-19 10:51:13 +02:00
|
|
|
caller_id: Uuid,
|
2026-02-14 20:22:19 +01:00
|
|
|
) -> Result<FileDto, DomainError> {
|
|
|
|
|
info!(
|
2026-06-17 01:43:13 +02:00
|
|
|
"Copying file with ID: {} to folder: {:?} as {:?}",
|
|
|
|
|
file_id, target_folder_id, new_name
|
2026-02-14 20:22:19 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let copied_file = self
|
|
|
|
|
.file_repository
|
2026-06-19 10:51:13 +02:00
|
|
|
.copy_file(file_id, target_folder_id, new_name, caller_id)
|
2026-02-14 20:22:19 +01:00
|
|
|
.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()
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-22 13:10:47 +02:00
|
|
|
let dto = FileDto::from(copied_file);
|
|
|
|
|
if let Some(hook) = &self.file_lifecycle_hook {
|
2026-06-10 12:44:13 +00:00
|
|
|
hook.on_file_copied(&dto.id, &dto.content_hash, &dto.mime_type, file_id);
|
2026-05-22 13:10:47 +02:00
|
|
|
}
|
2026-06-26 01:01:44 +02:00
|
|
|
// The caller just spawned a fresh file — show it in their Recent
|
|
|
|
|
// list. The source file isn't recorded; only the visible target.
|
|
|
|
|
self.notify_file_accessed(caller_id, &dto.id);
|
2026-05-22 13:10:47 +02:00
|
|
|
Ok(dto)
|
2026-02-14 20:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
2026-06-19 10:51:13 +02:00
|
|
|
async fn rename_file(
|
|
|
|
|
&self,
|
|
|
|
|
file_id: &str,
|
|
|
|
|
new_name: &str,
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
) -> Result<FileDto, DomainError> {
|
2026-05-09 00:06:30 +02:00
|
|
|
if let Err(reason) = validate_storage_name(new_name) {
|
|
|
|
|
return Err(DomainError::validation_error(format!(
|
|
|
|
|
"Invalid file name '{new_name}': {reason}"
|
|
|
|
|
)));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2026-06-19 10:51:13 +02:00
|
|
|
.rename_file(file_id, new_name, caller_id)
|
2026-02-14 01:29:34 +01:00
|
|
|
.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> {
|
2026-05-20 15:39:53 +02:00
|
|
|
warn!("Permanently deleting file: {}", id);
|
2026-03-07 19:15:36 +01:00
|
|
|
self.file_repository.delete_file(id).await?;
|
2026-04-11 17:39:17 +02:00
|
|
|
if let Some(cc) = &self.content_cache {
|
|
|
|
|
cc.invalidate(id).await;
|
|
|
|
|
}
|
2026-05-22 13:10:47 +02:00
|
|
|
if let Some(hook) = &self.file_lifecycle_hook {
|
|
|
|
|
hook.on_file_deleted(id);
|
2026-03-07 19:15:36 +01:00
|
|
|
}
|
2026-05-20 15:39:53 +02:00
|
|
|
info!("File permanently deleted: {}", id);
|
2026-03-07 19:15:36 +01:00
|
|
|
Ok(())
|
2025-03-19 19:52:12 +01:00
|
|
|
}
|
2026-02-08 13:40:23 +01:00
|
|
|
|
2026-05-20 15:39:53 +02:00
|
|
|
async fn copy_folder_tree(
|
|
|
|
|
&self,
|
|
|
|
|
source_folder_id: &str,
|
|
|
|
|
target_parent_id: Option<String>,
|
|
|
|
|
dest_name: Option<String>,
|
|
|
|
|
) -> Result<CopyFolderTreeResult, DomainError> {
|
|
|
|
|
info!(
|
|
|
|
|
"Copying folder tree: source={}, target_parent={:?}, dest_name={:?}",
|
|
|
|
|
source_folder_id, target_parent_id, dest_name
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let result = self
|
|
|
|
|
.file_repository
|
|
|
|
|
.copy_folder_tree(source_folder_id, target_parent_id, dest_name)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
error!(
|
|
|
|
|
"Error copying folder tree (source: {}): {}",
|
|
|
|
|
source_folder_id, e
|
|
|
|
|
);
|
|
|
|
|
e
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
info!(
|
|
|
|
|
"Folder tree copied: {} folders, {} files (new root: {})",
|
|
|
|
|
result.folders_copied, result.files_copied, result.new_root_folder_id
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FileManagementUseCase for FileManagementService {
|
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> {
|
|
|
|
|
let uuid = Uuid::parse_str(file_id).map_err(|_| DomainError::not_found("File", file_id))?;
|
|
|
|
|
self.authz
|
|
|
|
|
.require(Subject::User(caller_id), permission, Resource::File(uuid))
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 15:39:53 +02:00
|
|
|
async fn move_file_with_perms(
|
|
|
|
|
&self,
|
|
|
|
|
file_id: &str,
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
folder_id: Option<String>,
|
|
|
|
|
) -> Result<FileDto, DomainError> {
|
2026-05-20 22:56:00 +02:00
|
|
|
// Move = Update on the file + Create on the target folder (if any).
|
|
|
|
|
self.require_file_perm(file_id, Permission::Update, caller_id)
|
|
|
|
|
.await?;
|
|
|
|
|
self.require_target_folder_perm(folder_id.as_deref(), Permission::Create, caller_id)
|
2026-05-20 15:39:53 +02:00
|
|
|
.await?;
|
2026-06-26 01:48:39 +02:00
|
|
|
|
2026-06-29 21:11:40 +02:00
|
|
|
// D5 `forbid_cross_drive_move` + D6 `resource.moved_between_drives` audit
|
|
|
|
|
// share the same src/dst drive_id lookup: the gate refuses
|
|
|
|
|
// before the move; the audit fires after a successful move
|
|
|
|
|
// when the two drives differ. Silently skipped if the drive
|
|
|
|
|
// repo isn't wired (stub builders) or the move target is None
|
|
|
|
|
// (root namespace — same-drive semantics).
|
|
|
|
|
let mut cross_drive: Option<(Uuid, Uuid)> = None;
|
2026-06-26 01:48:39 +02:00
|
|
|
if let Some(drive_repo) = &self.drive_repo
|
|
|
|
|
&& let Some(target_folder_id) = folder_id.as_deref()
|
|
|
|
|
{
|
|
|
|
|
let file_uuid =
|
|
|
|
|
Uuid::parse_str(file_id).map_err(|_| DomainError::not_found("File", file_id))?;
|
|
|
|
|
let dst_folder_uuid = Uuid::parse_str(target_folder_id)
|
|
|
|
|
.map_err(|_| DomainError::not_found("Folder", target_folder_id))?;
|
|
|
|
|
let (src_drive_id, src_policies) = drive_repo
|
|
|
|
|
.get_drive_id_and_policies_for_file(file_uuid)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
DomainError::internal_error("Drive", format!("source drive lookup: {e:?}"))
|
|
|
|
|
})?;
|
|
|
|
|
let dst_drive_id = drive_repo
|
|
|
|
|
.drive_id_for_folder(dst_folder_uuid)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
DomainError::internal_error("Drive", format!("destination drive lookup: {e:?}"))
|
|
|
|
|
})?;
|
|
|
|
|
if src_drive_id != dst_drive_id {
|
|
|
|
|
src_policies.refuse_cross_drive_move(
|
|
|
|
|
crate::domain::entities::drive::CrossDriveMoveGateContext {
|
|
|
|
|
caller_id,
|
|
|
|
|
resource_type: "file",
|
|
|
|
|
resource_id: file_uuid,
|
|
|
|
|
src_drive_id,
|
|
|
|
|
dst_drive_id,
|
|
|
|
|
},
|
|
|
|
|
)?;
|
2026-06-29 21:11:40 +02:00
|
|
|
cross_drive = Some((src_drive_id, dst_drive_id));
|
2026-06-26 01:48:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-29 21:11:40 +02:00
|
|
|
let dto = self.move_file(file_id, folder_id, caller_id).await?;
|
|
|
|
|
|
|
|
|
|
// D6 §11 audit: emit only when the move actually crossed a
|
|
|
|
|
// drive boundary. Same-drive moves are too noisy to audit at
|
|
|
|
|
// info — operators care about the cross-drive case for
|
|
|
|
|
// exfiltration / quota tracking.
|
|
|
|
|
if let Some((src_drive_id, dst_drive_id)) = cross_drive {
|
|
|
|
|
tracing::info!(
|
|
|
|
|
target: "audit",
|
|
|
|
|
event = "resource.moved_between_drives",
|
|
|
|
|
resource_type = "file",
|
|
|
|
|
resource_id = %dto.id,
|
|
|
|
|
src_drive_id = %src_drive_id,
|
|
|
|
|
dst_drive_id = %dst_drive_id,
|
|
|
|
|
by = %caller_id,
|
|
|
|
|
"📦 file moved between drives",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Ok(dto)
|
2026-05-20 15:39:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn copy_file_with_perms(
|
|
|
|
|
&self,
|
|
|
|
|
file_id: &str,
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
target_folder_id: Option<String>,
|
2026-06-17 01:43:13 +02:00
|
|
|
new_name: Option<String>,
|
2026-05-20 15:39:53 +02:00
|
|
|
) -> Result<FileDto, DomainError> {
|
2026-05-20 22:56:00 +02:00
|
|
|
// Copy = Read on the source file + Create on the target folder.
|
|
|
|
|
self.require_file_perm(file_id, Permission::Read, caller_id)
|
|
|
|
|
.await?;
|
|
|
|
|
self.require_target_folder_perm(target_folder_id.as_deref(), Permission::Create, caller_id)
|
2026-05-20 15:39:53 +02:00
|
|
|
.await?;
|
2026-06-19 10:51:13 +02:00
|
|
|
self.copy_file(file_id, target_folder_id, new_name.as_deref(), caller_id)
|
2026-06-17 01:43:13 +02:00
|
|
|
.await
|
2026-05-20 15:39:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn rename_file_with_perms(
|
|
|
|
|
&self,
|
|
|
|
|
file_id: &str,
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
new_name: &str,
|
|
|
|
|
) -> Result<FileDto, DomainError> {
|
2026-05-20 22:56:00 +02:00
|
|
|
self.require_file_perm(file_id, Permission::Update, caller_id)
|
|
|
|
|
.await?;
|
2026-06-19 10:51:13 +02:00
|
|
|
self.rename_file(file_id, new_name, caller_id).await
|
2026-05-20 15:39:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn delete_file_with_perms(&self, id: &str, caller_id: Uuid) -> Result<(), DomainError> {
|
2026-05-20 22:56:00 +02:00
|
|
|
self.require_file_perm(id, Permission::Delete, caller_id)
|
|
|
|
|
.await?;
|
2026-03-05 10:30:39 +01:00
|
|
|
self.delete_file(id).await
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 13:40:23 +01:00
|
|
|
/// Smart delete: trash-first with dedup reference cleanup.
|
2026-02-22 22:07:46 +01:00
|
|
|
///
|
|
|
|
|
/// 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-05-20 15:39:53 +02:00
|
|
|
async fn delete_and_cleanup_with_perms(
|
|
|
|
|
&self,
|
|
|
|
|
id: &str,
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
) -> Result<bool, DomainError> {
|
2026-05-20 22:56:00 +02:00
|
|
|
self.require_file_perm(id, Permission::Delete, caller_id)
|
|
|
|
|
.await?;
|
2026-02-22 22:07:46 +01:00
|
|
|
// Step 1: Try trash (soft delete — file row stays, blob stays referenced)
|
2026-02-08 13:40:23 +01:00
|
|
|
if let Some(trash) = &self.trash_service {
|
|
|
|
|
info!("Moving file to trash: {}", id);
|
2026-05-20 15:39:53 +02:00
|
|
|
match trash.move_to_trash(id, "file", caller_id).await {
|
2026-02-08 13:40:23 +01:00
|
|
|
Ok(_) => {
|
|
|
|
|
info!("File successfully moved to trash: {}", id);
|
2026-04-11 17:39:17 +02:00
|
|
|
// Invalidate content cache — trashed files must not be served.
|
|
|
|
|
if let Some(cc) = &self.content_cache {
|
|
|
|
|
cc.invalidate(id).await;
|
|
|
|
|
}
|
2026-02-22 22:07:46 +01:00
|
|
|
// 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.
|
2026-02-08 13:40:23 +01:00
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 22:07:46 +01:00
|
|
|
// Step 2: Permanent delete — trigger handles blob ref_count
|
2026-02-24 19:28:00 +01:00
|
|
|
|
2026-05-20 15:39:53 +02:00
|
|
|
self.delete_file(id).await?;
|
2026-02-24 19:28:00 +01:00
|
|
|
|
2026-05-20 15:39:53 +02:00
|
|
|
Ok(false) // permanently deleted
|
2026-02-24 19:28:00 +01:00
|
|
|
}
|
2026-05-11 22:58:33 +02:00
|
|
|
|
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> {
|
2026-05-20 22:56:00 +02:00
|
|
|
// copy_folder_tree = Read on the source folder + Create on the target parent.
|
|
|
|
|
self.require_target_folder_perm(Some(source_folder_id), Permission::Read, caller_id)
|
2026-05-20 12:48:06 +02:00
|
|
|
.await?;
|
2026-05-20 22:56:00 +02:00
|
|
|
self.require_target_folder_perm(target_parent_id.as_deref(), Permission::Create, caller_id)
|
2026-05-11 22:58:33 +02:00
|
|
|
.await?;
|
|
|
|
|
self.copy_folder_tree(source_folder_id, target_parent_id, dest_name)
|
|
|
|
|
.await
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
}
|