Skip to main content

modo_upload/storage/
types.rs

1use crate::file::UploadedFile;
2use crate::stream::BufferedUpload;
3
4/// Metadata returned after a file has been successfully stored.
5pub struct StoredFile {
6    /// Relative path within the storage backend (e.g. `"avatars/01HXK3Q1A2B3.jpg"`).
7    pub path: String,
8    /// File size in bytes.
9    pub size: u64,
10}
11
12/// Trait for persisting uploaded files to a storage backend.
13///
14/// Both in-memory ([`UploadedFile`]) and chunked ([`BufferedUpload`]) uploads
15/// are supported.  Implementors must be `Send + Sync + 'static` so they can be
16/// shared across async tasks behind an `Arc<dyn FileStorage>`.
17///
18/// Use the [`storage()`](crate::storage()) factory function to construct the
19/// backend configured by [`UploadConfig`](crate::UploadConfig), or instantiate
20/// a concrete backend directly (e.g. [`LocalStorage`](super::local::LocalStorage)).
21#[async_trait::async_trait]
22pub trait FileStorage: Send + Sync + 'static {
23    /// Store a buffered in-memory file under `prefix/`.
24    ///
25    /// A ULID-based unique filename is generated automatically.
26    /// Returns the stored path and size on success.
27    async fn store(&self, prefix: &str, file: &UploadedFile) -> Result<StoredFile, modo::Error>;
28
29    /// Store a chunked upload under `prefix/`.
30    ///
31    /// Chunks are consumed from `stream` sequentially.
32    /// Returns the stored path and size on success.
33    async fn store_stream(
34        &self,
35        prefix: &str,
36        stream: &mut BufferedUpload,
37    ) -> Result<StoredFile, modo::Error>;
38
39    /// Delete a file by its storage path (as returned by [`store`](Self::store)).
40    async fn delete(&self, path: &str) -> Result<(), modo::Error>;
41
42    /// Return `true` if a file exists at the given storage path.
43    async fn exists(&self, path: &str) -> Result<bool, modo::Error>;
44}