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:
@@ -1,35 +1,26 @@
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
/// Observer notified by [`DedupService`] when a blob is stored for the first
|
||||
/// time or permanently removed (ref_count reaches zero).
|
||||
///
|
||||
/// Register with [`BlobLifecycleService`] during DI wiring; it fans out to all
|
||||
/// registered hooks. Every implementor **must** provide both methods —
|
||||
/// use an explicit one-liner noop for events the implementor does not care about.
|
||||
/// This forces conscious acknowledgement of every lifecycle event rather than
|
||||
/// silent omission.
|
||||
///
|
||||
/// All methods are synchronous. Background work must be spawned inside the
|
||||
/// implementor via `tokio::spawn`; the calling service never awaits hook
|
||||
/// completion.
|
||||
pub trait BlobLifecycleHook: Send + Sync {
|
||||
/// Called after a genuinely new blob has been written to storage (no dedup
|
||||
/// hit — first time this content hash is seen).
|
||||
///
|
||||
/// `blob_hash` — BLAKE3 hex identifying the blob.
|
||||
/// `content_type` — MIME type if known at write time, `None` otherwise.
|
||||
fn on_blob_created(&self, blob_hash: &str, content_type: Option<&str>);
|
||||
|
||||
/// Observer notified by [`DedupService`] when a genuinely new blob is stored
|
||||
/// for the first time (no dedup hit).
|
||||
///
|
||||
/// Register with [`DedupService::add_blob_creation_hook`] during DI wiring.
|
||||
pub trait BlobCreationHook: Send + Sync {
|
||||
/// Called after the new blob's chunks and manifest have been written.
|
||||
/// `blob_hash` is the BLAKE3 hex, `content_type` is the MIME type if known.
|
||||
/// Must be best-effort — must not propagate errors.
|
||||
fn on_blob_created<'a>(
|
||||
&'a self,
|
||||
blob_hash: &'a str,
|
||||
content_type: Option<&'a str>,
|
||||
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
|
||||
}
|
||||
/// Observer notified by [`DedupService`] when a blob's ref_count reaches zero
|
||||
/// and it is permanently removed from storage.
|
||||
///
|
||||
/// Implement this trait on any service that needs to react to blob deletion
|
||||
/// (e.g. thumbnail cleanup, CDN invalidation, audit logging). Register with
|
||||
/// [`DedupService::add_blob_hook`] during DI wiring.
|
||||
///
|
||||
/// The boxed-future return keeps the trait dyn-compatible so multiple
|
||||
/// implementations can be stored as `Vec<Arc<dyn BlobDeletionHook>>`.
|
||||
pub trait BlobDeletionHook: Send + Sync {
|
||||
/// Called after the blob file has been removed from disk.
|
||||
/// `blob_hash` is the BLAKE3 hex string identifying the blob.
|
||||
/// Must be best-effort — must not propagate errors.
|
||||
fn on_blob_deleted<'a>(
|
||||
&'a self,
|
||||
blob_hash: &'a str,
|
||||
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
|
||||
/// Called after a blob's ref_count reaches zero and it has been permanently
|
||||
/// removed from storage.
|
||||
///
|
||||
/// `blob_hash` — BLAKE3 hex identifying the (now deleted) blob.
|
||||
fn on_blob_deleted(&self, blob_hash: &str);
|
||||
}
|
||||
|
||||
@@ -1,57 +1,71 @@
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
/// Observer notified by [`FileUploadService`] when a new file record is created
|
||||
/// (including dedup hits where the blob already exists).
|
||||
/// Observer notified by file services when a file record is created, copied,
|
||||
/// updated, or permanently deleted.
|
||||
///
|
||||
/// Register with [`FileUploadService::with_file_created_hook`] during DI wiring.
|
||||
pub trait FileCreatedHook: Send + Sync {
|
||||
/// Called after the file record has been persisted.
|
||||
/// `file_id` — opaque file UUID string.
|
||||
/// `blob_hash` — BLAKE3 hex of the blob (may already exist on disk for dedup hits).
|
||||
/// `content_type` — MIME type of the content.
|
||||
/// Must be best-effort — must not propagate errors.
|
||||
fn on_file_created<'a>(
|
||||
&'a self,
|
||||
file_id: &'a str,
|
||||
blob_hash: &'a str,
|
||||
content_type: &'a str,
|
||||
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
|
||||
}
|
||||
|
||||
/// Observer notified by [`FileUploadService`] when an existing file's blob is
|
||||
/// replaced (WebDAV PUT overwrite, WOPI PutFile, Nextcloud chunked upload).
|
||||
/// Register with [`FileLifecycleService`] during DI wiring; it fans out to all
|
||||
/// registered hooks. Every implementor **must** provide all four methods —
|
||||
/// use an explicit one-liner noop for events the implementor does not care about.
|
||||
/// This forces conscious acknowledgement of every lifecycle event rather than
|
||||
/// silent omission.
|
||||
///
|
||||
/// Implement this trait on any service that needs to react to a content swap
|
||||
/// (e.g. thumbnail invalidation + regeneration, search index update).
|
||||
/// Register with [`FileUploadService::with_file_updated_hook`] during DI wiring.
|
||||
///
|
||||
/// The boxed-future return keeps the trait dyn-compatible so multiple
|
||||
/// implementations can be stored as `Vec<Arc<dyn FileUpdatedHook>>`.
|
||||
pub trait FileUpdatedHook: Send + Sync {
|
||||
/// Called after the new blob has been stored and the file record updated.
|
||||
/// All methods are synchronous. Background work must be spawned inside the
|
||||
/// implementor via `tokio::spawn`; the calling service never awaits hook
|
||||
/// completion.
|
||||
pub trait FileLifecycleHook: Send + Sync {
|
||||
/// Called after a new file record has been persisted.
|
||||
///
|
||||
/// `file_id` is an opaque file UUID string, `blob_hash` is the BLAKE3 hex
|
||||
/// of the new blob, `content_type` is the MIME type of the new content.
|
||||
/// Must be best-effort — must not propagate errors.
|
||||
fn on_file_updated<'a>(
|
||||
&'a self,
|
||||
file_id: &'a str,
|
||||
blob_hash: &'a str,
|
||||
content_type: &'a str,
|
||||
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
|
||||
}
|
||||
|
||||
/// Observer notified by [`FileManagementService`] when a file is permanently
|
||||
/// deleted (either directly or after being emptied from trash).
|
||||
///
|
||||
/// Register with [`FileManagementService::with_file_deleted_hook`] during DI wiring.
|
||||
pub trait FileDeletedHook: Send + Sync {
|
||||
/// Called after the file record has been removed.
|
||||
/// `file_id` — opaque file UUID string.
|
||||
/// Must be best-effort — must not propagate errors.
|
||||
fn on_file_deleted<'a>(
|
||||
&'a self,
|
||||
file_id: &'a str,
|
||||
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
|
||||
/// `blob_hash` — BLAKE3 hex of the content blob.
|
||||
/// `content_type` — MIME type.
|
||||
/// `is_new_blob` — `true` if the blob was stored for the first time (no
|
||||
/// dedup hit); `false` if the blob already existed (re-upload of identical
|
||||
/// content). Implementors can use this to skip re-generating artefacts that
|
||||
/// are keyed by `blob_hash` and already exist on disk.
|
||||
///
|
||||
/// For explicit file copies use [`on_file_copied`] instead — it supplies
|
||||
/// the source file id so per-file metadata can be cloned directly.
|
||||
fn on_file_created(
|
||||
&self,
|
||||
file_id: &str,
|
||||
blob_hash: &str,
|
||||
content_type: &str,
|
||||
is_new_blob: bool,
|
||||
);
|
||||
|
||||
/// Called after a file has been created as an explicit copy of an existing file.
|
||||
///
|
||||
/// `file_id` — opaque file UUID string of the **new** copy.
|
||||
/// `blob_hash` — BLAKE3 hex of the shared content blob.
|
||||
/// `content_type` — MIME type.
|
||||
/// `source_file_id` — opaque file UUID string of the **original** file.
|
||||
///
|
||||
/// Implementors may use `source_file_id` to efficiently clone per-file
|
||||
/// metadata (audio tags, etc.) from the original rather than re-deriving
|
||||
/// it from the blob. If the original has not yet been processed, fall back
|
||||
/// to a blob-hash-based lookup or schedule a retry — the implementor owns
|
||||
/// race handling.
|
||||
fn on_file_copied(
|
||||
&self,
|
||||
file_id: &str,
|
||||
blob_hash: &str,
|
||||
content_type: &str,
|
||||
source_file_id: &str,
|
||||
);
|
||||
|
||||
/// Called after an existing file's blob has been replaced (WebDAV PUT
|
||||
/// overwrite, WOPI PutFile, Nextcloud chunked upload finalization).
|
||||
///
|
||||
/// `file_id` — opaque file UUID string.
|
||||
/// `blob_hash` — BLAKE3 hex of the **new** blob.
|
||||
/// `content_type` — MIME type of the new content.
|
||||
fn on_file_updated(&self, file_id: &str, blob_hash: &str, content_type: &str);
|
||||
|
||||
/// Called after a file record has been permanently removed (direct delete
|
||||
/// or emptied from trash).
|
||||
///
|
||||
/// NOTE: due to deduplication the blob may still exist if other files
|
||||
/// reference it. Use [`BlobLifecycleHook::on_blob_deleted`] when your
|
||||
/// side-effect is content-addressed (e.g. removing blob-keyed thumbnails).
|
||||
///
|
||||
/// `file_id` — opaque file UUID string.
|
||||
fn on_file_deleted(&self, file_id: &str);
|
||||
}
|
||||
|
||||
@@ -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