refactor(lifecycle hooks): simplify integration of new services
* make more coherent lifecycles
* remove specific implementation on different handlers (they do not need to know existence of ThumbnailSerice nor AudioMetadataService)
* reduce risk of orphean objects
* ensure additional services are correctly wired (ex: Thumbnail generation was not covering all upload cases)
* more details on docs/architecture/file-and-blob-lifecycle.md :
```rust
// application/ports/file_lifecycle.rs
pub trait FileLifecycleHook {
fn on_file_created(file_id, blob_hash, content_type, is_new_blob);
fn on_file_updated(file_id, blob_hash, content_type);
fn on_file_copied(file_id, blob_hash, content_type, source_id)
fn on_file_deleted(file_id);
}
// application/ports/blob_lifecycle.rs
pub trait BlobLifecycleHook {
fn on_blob_created(blob_hash, content_type);
fn on_blob_deleted(blob_hash);
}
```
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::application::ports::blob_lifecycle::BlobLifecycleHook;
|
||||
|
||||
/// Composite dispatcher for blob lifecycle events.
|
||||
///
|
||||
/// Aggregates all [`BlobLifecycleHook`] implementations and fans out each
|
||||
/// event to every registered handler. Services hold a single
|
||||
/// `Arc<BlobLifecycleService>` — new handlers are added once, in DI, without
|
||||
/// touching the services themselves.
|
||||
pub struct BlobLifecycleService {
|
||||
hooks: Vec<Arc<dyn BlobLifecycleHook>>,
|
||||
}
|
||||
|
||||
impl Default for BlobLifecycleService {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl BlobLifecycleService {
|
||||
pub fn new() -> Self {
|
||||
Self { hooks: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn with_hook(mut self, hook: Arc<dyn BlobLifecycleHook>) -> Self {
|
||||
self.hooks.push(hook);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl BlobLifecycleHook for BlobLifecycleService {
|
||||
fn on_blob_created(&self, blob_hash: &str, content_type: Option<&str>) {
|
||||
for hook in &self.hooks {
|
||||
hook.on_blob_created(blob_hash, content_type);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_blob_deleted(&self, blob_hash: &str) {
|
||||
for hook in &self.hooks {
|
||||
hook.on_blob_deleted(blob_hash);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,15 @@
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::application::ports::file_lifecycle::FileDeletedHook;
|
||||
use crate::application::ports::file_lifecycle::FileLifecycleHook;
|
||||
|
||||
/// Composite dispatcher for file lifecycle events.
|
||||
///
|
||||
/// Aggregates all `FileDeletedHook` implementations and fans out each event to
|
||||
/// every registered handler. Services hold a single `Arc<dyn FileDeletedHook>`
|
||||
/// pointing here — new handlers are added once, in DI, without touching the
|
||||
/// services themselves.
|
||||
/// Aggregates all [`FileLifecycleHook`] implementations and fans out each
|
||||
/// event to every registered handler. Services hold a single
|
||||
/// `Arc<FileLifecycleService>` — new handlers are added once, in DI, without
|
||||
/// touching the services themselves.
|
||||
pub struct FileLifecycleService {
|
||||
deleted: Vec<Arc<dyn FileDeletedHook>>,
|
||||
hooks: Vec<Arc<dyn FileLifecycleHook>>,
|
||||
}
|
||||
|
||||
impl Default for FileLifecycleService {
|
||||
@@ -22,26 +20,49 @@ impl Default for FileLifecycleService {
|
||||
|
||||
impl FileLifecycleService {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
deleted: Vec::new(),
|
||||
}
|
||||
Self { hooks: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn with_deleted_hook(mut self, hook: Arc<dyn FileDeletedHook>) -> Self {
|
||||
self.deleted.push(hook);
|
||||
pub fn with_hook(mut self, hook: Arc<dyn FileLifecycleHook>) -> Self {
|
||||
self.hooks.push(hook);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl FileDeletedHook for FileLifecycleService {
|
||||
fn on_file_deleted<'a>(
|
||||
&'a self,
|
||||
file_id: &'a str,
|
||||
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
|
||||
Box::pin(async move {
|
||||
for hook in &self.deleted {
|
||||
hook.on_file_deleted(file_id).await;
|
||||
}
|
||||
})
|
||||
impl FileLifecycleHook for FileLifecycleService {
|
||||
fn on_file_created(
|
||||
&self,
|
||||
file_id: &str,
|
||||
blob_hash: &str,
|
||||
content_type: &str,
|
||||
is_new_blob: bool,
|
||||
) {
|
||||
for hook in &self.hooks {
|
||||
hook.on_file_created(file_id, blob_hash, content_type, is_new_blob);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_file_copied(
|
||||
&self,
|
||||
file_id: &str,
|
||||
blob_hash: &str,
|
||||
content_type: &str,
|
||||
source_file_id: &str,
|
||||
) {
|
||||
for hook in &self.hooks {
|
||||
hook.on_file_copied(file_id, blob_hash, content_type, source_file_id);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_file_updated(&self, file_id: &str, blob_hash: &str, content_type: &str) {
|
||||
for hook in &self.hooks {
|
||||
hook.on_file_updated(file_id, blob_hash, content_type);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_file_deleted(&self, file_id: &str) {
|
||||
for hook in &self.hooks {
|
||||
hook.on_file_deleted(file_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::sync::Arc;
|
||||
|
||||
use crate::application::dtos::file_dto::FileDto;
|
||||
use crate::application::ports::authorization_ports::AuthorizationEngine;
|
||||
use crate::application::ports::file_lifecycle::FileDeletedHook;
|
||||
use crate::application::ports::file_lifecycle::FileLifecycleHook;
|
||||
use crate::application::ports::file_ports::FileManagementUseCase;
|
||||
use crate::application::ports::storage_ports::{CopyFolderTreeResult, FileWritePort};
|
||||
use crate::application::ports::trash_ports::TrashUseCase;
|
||||
@@ -29,8 +29,8 @@ pub struct FileManagementService {
|
||||
trash_service: Option<Arc<TrashService>>,
|
||||
content_cache: Option<Arc<FileContentCache>>,
|
||||
authz: Arc<PgAclEngine>,
|
||||
/// Hook fired after a file is permanently deleted (typically the FileLifecycleService composite).
|
||||
file_deleted_hook: Option<Arc<dyn FileDeletedHook>>,
|
||||
/// Lifecycle hook dispatcher — fired on file created (copy) and deleted.
|
||||
file_lifecycle_hook: Option<Arc<dyn FileLifecycleHook>>,
|
||||
}
|
||||
|
||||
impl FileManagementService {
|
||||
@@ -52,13 +52,13 @@ impl FileManagementService {
|
||||
trash_service,
|
||||
content_cache,
|
||||
authz,
|
||||
file_deleted_hook: None,
|
||||
file_lifecycle_hook: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the lifecycle hook fired after a file is permanently deleted.
|
||||
pub fn with_file_deleted_hook(mut self, hook: Arc<dyn FileDeletedHook>) -> Self {
|
||||
self.file_deleted_hook = Some(hook);
|
||||
/// 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);
|
||||
self
|
||||
}
|
||||
|
||||
@@ -149,7 +149,11 @@ impl FileManagementService {
|
||||
copied_file.folder_id()
|
||||
);
|
||||
|
||||
Ok(FileDto::from(copied_file))
|
||||
let dto = FileDto::from(copied_file);
|
||||
if let Some(hook) = &self.file_lifecycle_hook {
|
||||
hook.on_file_copied(&dto.id, &dto.etag, &dto.mime_type, file_id);
|
||||
}
|
||||
Ok(dto)
|
||||
}
|
||||
|
||||
async fn rename_file(&self, file_id: &str, new_name: &str) -> Result<FileDto, DomainError> {
|
||||
@@ -185,8 +189,8 @@ impl FileManagementService {
|
||||
if let Some(cc) = &self.content_cache {
|
||||
cc.invalidate(id).await;
|
||||
}
|
||||
if let Some(hook) = &self.file_deleted_hook {
|
||||
hook.on_file_deleted(id).await;
|
||||
if let Some(hook) = &self.file_lifecycle_hook {
|
||||
hook.on_file_deleted(id);
|
||||
}
|
||||
info!("File permanently deleted: {}", id);
|
||||
Ok(())
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::application::dtos::file_dto::FileDto;
|
||||
use crate::application::ports::file_lifecycle::{FileCreatedHook, FileUpdatedHook};
|
||||
use crate::application::ports::file_lifecycle::FileLifecycleHook;
|
||||
use crate::application::ports::file_ports::FileUploadUseCase;
|
||||
use crate::application::ports::storage_ports::{FileReadPort, FileWritePort};
|
||||
use crate::application::services::storage_usage_service::StorageUsageService;
|
||||
@@ -53,10 +53,8 @@ pub struct FileUploadService {
|
||||
storage_usage_service: Option<Arc<StorageUsageService>>,
|
||||
/// Content cache — invalidated on file update so stale content is never served.
|
||||
content_cache: Option<Arc<FileContentCache>>,
|
||||
/// Hooks fired after a new file record is created.
|
||||
file_created_hooks: Vec<Arc<dyn FileCreatedHook>>,
|
||||
/// Hooks fired after a file's blob is replaced (e.g. thumbnail refresh).
|
||||
file_updated_hooks: Vec<Arc<dyn FileUpdatedHook>>,
|
||||
/// Single lifecycle dispatcher — fires on_file_created / on_file_updated.
|
||||
file_lifecycle_hook: Option<Arc<dyn FileLifecycleHook>>,
|
||||
}
|
||||
|
||||
impl FileUploadService {
|
||||
@@ -67,8 +65,7 @@ impl FileUploadService {
|
||||
file_read: None,
|
||||
storage_usage_service: None,
|
||||
content_cache: None,
|
||||
file_created_hooks: Vec::new(),
|
||||
file_updated_hooks: Vec::new(),
|
||||
file_lifecycle_hook: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,8 +79,7 @@ impl FileUploadService {
|
||||
file_read: Some(file_read),
|
||||
storage_usage_service: None,
|
||||
content_cache: None,
|
||||
file_created_hooks: Vec::new(),
|
||||
file_updated_hooks: Vec::new(),
|
||||
file_lifecycle_hook: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,15 +89,9 @@ impl FileUploadService {
|
||||
self
|
||||
}
|
||||
|
||||
/// Registers a hook to fire after a new file record is created.
|
||||
pub fn with_file_created_hook(mut self, hook: Arc<dyn FileCreatedHook>) -> Self {
|
||||
self.file_created_hooks.push(hook);
|
||||
self
|
||||
}
|
||||
|
||||
/// Registers a hook to fire after a file's blob is replaced.
|
||||
pub fn with_file_updated_hook(mut self, hook: Arc<dyn FileUpdatedHook>) -> Self {
|
||||
self.file_updated_hooks.push(hook);
|
||||
/// Registers 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);
|
||||
self
|
||||
}
|
||||
|
||||
@@ -153,9 +143,9 @@ impl FileUploadUseCase for FileUploadService {
|
||||
size: u64,
|
||||
pre_computed_hash: Option<String>,
|
||||
) -> Result<FileDto, DomainError> {
|
||||
let file = self
|
||||
let (file, is_new_blob) = self
|
||||
.file_write
|
||||
.save_file_from_temp(
|
||||
.save_file_from_temp_with_dedup(
|
||||
name.clone(),
|
||||
folder_id,
|
||||
content_type,
|
||||
@@ -170,9 +160,8 @@ impl FileUploadUseCase for FileUploadService {
|
||||
name, size, dto.id
|
||||
);
|
||||
self.maybe_update_storage_usage(&dto);
|
||||
for hook in &self.file_created_hooks {
|
||||
hook.on_file_created(&dto.id, &dto.etag, &dto.mime_type)
|
||||
.await;
|
||||
if let Some(hook) = &self.file_lifecycle_hook {
|
||||
hook.on_file_created(&dto.id, &dto.etag, &dto.mime_type, is_new_blob);
|
||||
}
|
||||
Ok(dto)
|
||||
}
|
||||
@@ -222,7 +211,6 @@ impl FileUploadUseCase for FileUploadService {
|
||||
// Look up the folder ID by folder path
|
||||
let parent_id = if !parent_path.is_empty() {
|
||||
if let Some(file_read) = &self.file_read {
|
||||
// Use get_folder_id_by_path to look up the folder directly
|
||||
file_read.get_folder_id_by_path(parent_path).await.ok()
|
||||
} else {
|
||||
None
|
||||
@@ -241,9 +229,9 @@ impl FileUploadUseCase for FileUploadService {
|
||||
.await
|
||||
.map_err(|e| DomainError::internal_error("FileUpload", format!("hash: {e}")))?;
|
||||
|
||||
let file = self
|
||||
let (file, is_new_blob) = self
|
||||
.file_write
|
||||
.save_file_from_temp(
|
||||
.save_file_from_temp_with_dedup(
|
||||
filename.to_string(),
|
||||
parent_id,
|
||||
content_type.to_string(),
|
||||
@@ -254,6 +242,9 @@ impl FileUploadUseCase for FileUploadService {
|
||||
.await?;
|
||||
let dto = FileDto::from(file);
|
||||
self.maybe_update_storage_usage(&dto);
|
||||
if let Some(hook) = &self.file_lifecycle_hook {
|
||||
hook.on_file_created(&dto.id, &dto.etag, &dto.mime_type, is_new_blob);
|
||||
}
|
||||
Ok(dto)
|
||||
}
|
||||
|
||||
@@ -327,9 +318,8 @@ impl FileUploadUseCase for FileUploadService {
|
||||
// Re-read to get fresh DTO with updated etag and timestamps.
|
||||
let updated = file_read.get_file(&file_id).await?;
|
||||
let dto = FileDto::from(updated);
|
||||
for hook in &self.file_updated_hooks {
|
||||
hook.on_file_updated(&file_id, &dto.etag, content_type)
|
||||
.await;
|
||||
if let Some(hook) = &self.file_lifecycle_hook {
|
||||
hook.on_file_updated(&file_id, &dto.etag, content_type);
|
||||
}
|
||||
return Ok(dto);
|
||||
}
|
||||
@@ -354,9 +344,9 @@ impl FileUploadUseCase for FileUploadService {
|
||||
None
|
||||
};
|
||||
|
||||
let created = self
|
||||
let (created, is_new_blob) = self
|
||||
.file_write
|
||||
.save_file_from_temp(
|
||||
.save_file_from_temp_with_dedup(
|
||||
filename.to_string(),
|
||||
parent_id,
|
||||
content_type.to_string(),
|
||||
@@ -365,6 +355,10 @@ impl FileUploadUseCase for FileUploadService {
|
||||
pre_computed_hash,
|
||||
)
|
||||
.await?;
|
||||
Ok(FileDto::from(created))
|
||||
let dto = FileDto::from(created);
|
||||
if let Some(hook) = &self.file_lifecycle_hook {
|
||||
hook.on_file_created(&dto.id, &dto.etag, content_type, is_new_blob);
|
||||
}
|
||||
Ok(dto)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ pub mod admin_settings_service;
|
||||
pub mod app_password_service;
|
||||
pub mod auth_application_service;
|
||||
pub mod batch_operations;
|
||||
pub mod blob_lifecycle_service;
|
||||
pub mod calendar_service;
|
||||
pub mod contact_service;
|
||||
pub mod device_auth_service;
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::application::dtos::display_helpers::{
|
||||
};
|
||||
use crate::application::dtos::trash_dto::TrashedItemDto;
|
||||
use crate::application::ports::authorization_ports::AuthorizationEngine;
|
||||
use crate::application::ports::file_lifecycle::FileDeletedHook;
|
||||
use crate::application::ports::file_lifecycle::FileLifecycleHook;
|
||||
use crate::application::ports::storage_ports::{FileReadPort, FileWritePort};
|
||||
use crate::application::ports::trash_ports::TrashUseCase;
|
||||
use crate::common::errors::{DomainError, ErrorKind, Result};
|
||||
@@ -53,8 +53,8 @@ pub struct TrashService {
|
||||
/// orphaned blob files and thumbnails that the PG trigger cannot reach.
|
||||
dedup_service: Arc<DedupService>,
|
||||
|
||||
/// Hook fired after a file is permanently deleted (typically the FileLifecycleService composite).
|
||||
file_deleted_hook: Option<Arc<dyn FileDeletedHook>>,
|
||||
/// Lifecycle hook dispatcher — fired on file permanently deleted.
|
||||
file_deleted_hook: Option<Arc<dyn FileLifecycleHook>>,
|
||||
|
||||
/// Content cache — invalidated when files are permanently deleted from trash.
|
||||
content_cache: Option<Arc<FileContentCache>>,
|
||||
@@ -91,8 +91,8 @@ impl TrashService {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the lifecycle hook fired after a file is permanently deleted.
|
||||
pub fn with_file_deleted_hook(mut self, hook: Arc<dyn FileDeletedHook>) -> Self {
|
||||
/// Sets the lifecycle hook dispatcher (thumbnails, audio metadata, …).
|
||||
pub fn with_file_deleted_hook(mut self, hook: Arc<dyn FileLifecycleHook>) -> Self {
|
||||
self.file_deleted_hook = Some(hook);
|
||||
self
|
||||
}
|
||||
@@ -583,7 +583,7 @@ impl TrashUseCase for TrashService {
|
||||
}
|
||||
|
||||
if let Some(hook) = &self.file_deleted_hook {
|
||||
hook.on_file_deleted(&file_id).await;
|
||||
hook.on_file_deleted(&file_id);
|
||||
}
|
||||
}
|
||||
TrashedItemType::Folder => {
|
||||
@@ -723,7 +723,7 @@ impl TrashUseCase for TrashService {
|
||||
|
||||
if let Some(hook) = &self.file_deleted_hook {
|
||||
for file_id in &trashed_file_ids {
|
||||
hook.on_file_deleted(file_id).await;
|
||||
hook.on_file_deleted(file_id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user