Skip to main content

FileManager

Struct FileManager 

Source
pub struct FileManager { /* private fields */ }
Expand description

Re-export FileManager when files feature is enabled. File manager - main interface for file operations

Implementations§

Source§

impl FileManager

Source

pub async fn new(config: FileConfig) -> Result<FileManager, FileError>

Create a new file manager with default (empty) hooks.

For hooks integration, use FileManager::new_with_hooks instead.

Source

pub async fn new_with_hooks( config: FileConfig, hooks: HookRegistry, ) -> Result<FileManager, FileError>

Create a new file manager with the given configuration and hooks.

§Arguments
  • config - File manager configuration
  • hooks - Hook registry containing storage, read, metadata, and cleanup hooks
§Example
use agent_diva_files::{FileManager, FileConfig, hooks::{HookRegistry, LoggingStorageHook}};

let mut hooks = HookRegistry::new();
hooks.register_storage_hook(Box::new(LoggingStorageHook));

let manager = FileManager::new_with_hooks(FileConfig::default(), hooks).await?;
Source

pub async fn default() -> Result<FileManager, FileError>

Create with default configuration

Source

pub async fn store( &self, data: &[u8], metadata: FileMetadata, ) -> Result<FileHandle, FileError>

Store file data and return a handle

If the file already exists (based on hash), returns an existing handle and increments the reference count.

§Hook Integration
  • Calls StorageHook::before_store hooks before storing
  • Calls StorageHook::after_store hooks after successful storage
  • Calls MetadataHook::extract_metadata for additional metadata
Source

pub async fn store_from_path( &self, source_path: &PathBuf, metadata: Option<FileMetadata>, ) -> Result<FileHandle, FileError>

Store file from path

Source

pub async fn get(&self, id: &str) -> Result<FileHandle, FileError>

Get a file handle by ID

Increments the reference count if found. Note: This only returns non-deleted files.

Source

pub async fn clone_ref( &self, handle: &FileHandle, ) -> Result<FileHandle, FileError>

Clone a file handle (increment reference count)

Source

pub async fn release(&self, handle: &FileHandle) -> Result<(), FileError>

Release a file handle (decrement reference count)

Does not actually delete the file - cleanup is done separately based on the cleanup strategy.

Source

pub async fn soft_delete( &self, id: &str, deleted_by: Option<&str>, ) -> Result<bool, FileError>

Soft delete a file - marks it as deleted but doesn’t remove physically

The file enters a “deleted” state but can be recovered within the retention period. After retention_days expire, the file becomes eligible for permanent deletion via FileManager::purge_expired.

§Arguments
  • id - File ID to delete
  • deleted_by - Optional identifier of who/what deleted the file
§Example
// Soft delete a file
manager.soft_delete(&file_id, Some("user@example.com")).await?;

// List deleted files
let deleted = manager.list_deleted().await?;

// Restore if needed
manager.restore(&file_id).await?;
Source

pub async fn restore(&self, id: &str) -> Result<bool, FileError>

Restore a soft-deleted file

Makes the file accessible again by clearing the deleted timestamp. Only works on files that were soft-deleted and haven’t expired yet.

§Arguments
  • id - File ID to restore
§Returns
  • Ok(true) - File was restored
  • Ok(false) - File wasn’t found in deleted state
Source

pub async fn list_deleted(&self) -> Result<Vec<FileIndexEntry>, FileError>

List all soft-deleted files

Returns files that have been soft-deleted but not yet purged. Files are sorted by deletion time (most recent first).

§Returns

List of deleted file entries with deletion metadata

Source

pub async fn hard_delete( &self, id: &str, ) -> Result<Option<FileIndexEntry>, FileError>

Permanently delete a specific file (bypass retention period)

WARNING: This immediately and permanently removes the file. Unlike soft delete, there is no way to recover a hard-deleted file.

§Arguments
  • id - File ID to permanently delete
§Returns

The deleted entry (for logging/audit purposes), or None if not found

Source

pub async fn purge_expired( &self, retention_days: u32, ) -> Result<usize, FileError>

Purge all soft-deleted files that have exceeded the retention period

This is the cleanup task for soft deletes. It finds all soft-deleted files where deleted_at is older than retention_days and permanently removes them.

§Arguments
  • retention_days - Files deleted more than this many days ago will be purged
§Returns

Number of files permanently deleted

Source

pub async fn is_deleted(&self, id: &str) -> Result<bool, FileError>

Check if a file is soft-deleted

§Arguments
  • id - File ID to check
§Returns

true if the file is soft-deleted, false otherwise

Source

pub async fn read(&self, handle: &FileHandle) -> Result<Vec<u8>, FileError>

Read file data by handle

§Hook Integration
  • Calls ReadHook::before_read hooks before reading (e.g., permission check)
  • Calls ReadHook::after_read hooks after reading (e.g., decryption)
Source

pub async fn read_string( &self, handle: &FileHandle, ) -> Result<String, FileError>

Read file as string (for text files)

Source

pub fn full_path(&self, handle: &FileHandle) -> PathBuf

Get the full path for a handle

Source

pub async fn exists(&self, id: &str) -> bool

Check if a file exists (non-deleted)

Source

pub async fn metadata(&self, id: &str) -> Result<FileMetadata, FileError>

Get file metadata

Returns metadata for non-deleted files only.

Source

pub async fn cleanup(&self) -> Result<usize, FileError>

Run cleanup - delete files with ref_count <= threshold

Returns the number of files deleted.

§Hook Integration
  • Calls CleanupHook::should_cleanup for each candidate
  • Calls CleanupHook::after_cleanup after each file is deleted
Source

pub async fn stats(&self) -> Result<StorageStats, FileError>

Get storage statistics

Source

pub fn config(&self) -> &FileConfig

Get a reference to the config

Source

pub fn hooks(&self) -> &HookRegistry

Get a reference to the hooks registry

Useful for inspecting registered hooks or adding hooks dynamically.

Source

pub fn hooks_mut(&mut self) -> &mut HookRegistry

Get mutable reference to the hooks registry

Allows adding new hooks at runtime.

Source

pub fn start_cleanup_task(self: Arc<FileManager>) -> JoinHandle<()>

Start background cleanup task

This spawns a task that periodically runs cleanup

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more