fileloft_core/store.rs
1use crate::{error::TusError, info::{UploadId, UploadInfo}};
2
3/// Operations on a single upload resource.
4#[trait_variant::make(SendUpload: Send)]
5pub trait Upload {
6 /// Write chunk data starting at `offset`, streaming from `reader`.
7 /// Returns the number of bytes written.
8 async fn write_chunk(
9 &mut self,
10 offset: u64,
11 reader: &mut (dyn tokio::io::AsyncRead + Unpin + Send),
12 ) -> Result<u64, TusError>;
13
14 /// Retrieve current metadata and offset.
15 async fn get_info(&self) -> Result<UploadInfo, TusError>;
16
17 /// Called once all bytes have been received (offset == size).
18 async fn finalize(&mut self) -> Result<(), TusError>;
19
20 /// Delete this upload and free all associated resources.
21 /// Called by the termination extension (DELETE). Return `Err` if unsupported.
22 async fn delete(self) -> Result<(), TusError>;
23
24 /// Set the definitive `Upload-Length` on a deferred-length upload.
25 /// Called when the client provides `Upload-Length` on a PATCH request.
26 async fn declare_length(&mut self, length: u64) -> Result<(), TusError>;
27
28 /// Assemble fully-uploaded partials (in order) into this final upload.
29 /// Called by the concatenation extension.
30 async fn concatenate(&mut self, partials: &[UploadInfo]) -> Result<(), TusError>;
31}
32
33/// Core storage abstraction.
34///
35/// Implement this trait to plug in any persistence backend (filesystem, S3, etc.).
36/// The associated `UploadType` must implement all upload operations; return
37/// `TusError::ExtensionNotEnabled` from extension methods your store doesn't support.
38#[trait_variant::make(SendDataStore: Send)]
39pub trait DataStore {
40 type UploadType: SendUpload;
41
42 /// Create a new upload slot and return a handle to it.
43 async fn create_upload(&self, info: UploadInfo) -> Result<Self::UploadType, TusError>;
44
45 /// Retrieve an existing upload by ID.
46 async fn get_upload(&self, id: &UploadId) -> Result<Self::UploadType, TusError>;
47}