Files
Oxicloud/src/application/services/blob_lifecycle_service.rs
T
Edouard Vanbelle 73f0b0fa47 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);
}
```
2026-05-22 13:40:58 +02:00

45 lines
1.2 KiB
Rust

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);
}
}
}