use std::{
collections::BTreeMap,
fmt,
future::Future,
path::{Path, PathBuf},
sync::Arc,
};
use futures_util::future::BoxFuture;
use uuid::Uuid;
use crate::{
error::S3Error,
s3::types::{BucketName, ContentLength, ObjectKey, RequestId},
storage::{StagedObject, StoreError},
};
pub type SharedUploadProcessor = Arc<dyn UploadProcessor>;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UploadProcessorOperation {
PutObject,
CompleteMultipartUpload,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UploadProcessorContext {
pub request_id: RequestId,
pub bucket: BucketName,
pub key: ObjectKey,
pub operation: UploadProcessorOperation,
pub original_size: ContentLength,
pub content_type: Option<String>,
pub user_metadata: BTreeMap<String, String>,
}
pub struct UploadProcessorRequest<'a> {
pub context: &'a UploadProcessorContext,
pub current_path: &'a Path,
pub replacement_path: &'a Path,
}
#[derive(Clone, Debug)]
pub struct UploadProcessorHandle {
context: UploadProcessorContext,
current_path: PathBuf,
replacement_path: PathBuf,
}
impl UploadProcessorHandle {
fn from_request(request: UploadProcessorRequest<'_>) -> Self {
Self {
context: request.context.clone(),
current_path: request.current_path.to_path_buf(),
replacement_path: request.replacement_path.to_path_buf(),
}
}
pub fn context(&self) -> &UploadProcessorContext {
&self.context
}
pub fn current_path(&self) -> &Path {
&self.current_path
}
pub fn replacement_path(&self) -> &Path {
&self.replacement_path
}
pub async fn read_current(&self) -> Result<Vec<u8>, UploadProcessorError> {
tokio::fs::read(&self.current_path)
.await
.map_err(UploadProcessorError::from)
}
pub async fn write_replacement(
&self,
bytes: impl AsRef<[u8]>,
) -> Result<(), UploadProcessorError> {
tokio::fs::write(&self.replacement_path, bytes)
.await
.map_err(UploadProcessorError::from)
}
pub fn keep(&self) -> UploadProcessorAction {
UploadProcessorAction::Keep
}
pub fn replace(&self) -> UploadProcessorAction {
UploadProcessorAction::Replace
}
pub fn reject(
&self,
message: impl Into<String>,
) -> Result<UploadProcessorAction, UploadProcessorError> {
Err(UploadProcessorError::Rejected(message.into()))
}
pub fn fail(
&self,
message: impl Into<String>,
) -> Result<UploadProcessorAction, UploadProcessorError> {
Err(UploadProcessorError::Failed(message.into()))
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UploadProcessorAction {
Keep,
Replace,
}
#[derive(Debug)]
pub enum UploadProcessorError {
Rejected(String),
Failed(String),
}
impl fmt::Display for UploadProcessorError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Rejected(message) | Self::Failed(message) => formatter.write_str(message),
}
}
}
impl std::error::Error for UploadProcessorError {}
pub trait UploadProcessor: Send + Sync + 'static {
fn process<'a>(
&'a self,
request: UploadProcessorRequest<'a>,
) -> BoxFuture<'a, Result<UploadProcessorAction, UploadProcessorError>>;
}
pub struct UploadProcessorFn<F> {
name: String,
handler: F,
}
impl<F> UploadProcessorFn<F> {
pub fn new(name: impl Into<String>, handler: F) -> Self {
Self {
name: name.into(),
handler,
}
}
pub fn name(&self) -> &str {
&self.name
}
}
impl<F, Fut> UploadProcessor for UploadProcessorFn<F>
where
F: Fn(UploadProcessorHandle) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<UploadProcessorAction, UploadProcessorError>> + Send + 'static,
{
fn process<'a>(
&'a self,
request: UploadProcessorRequest<'a>,
) -> BoxFuture<'a, Result<UploadProcessorAction, UploadProcessorError>> {
let handle = UploadProcessorHandle::from_request(request);
Box::pin((self.handler)(handle))
}
}
pub(crate) async fn process_staged_upload(
processors: &[SharedUploadProcessor],
mut staged: StagedObject,
context: UploadProcessorContext,
) -> Result<StagedObject, S3Error> {
for processor in processors {
let replacement_path = staged.replacement_path(Uuid::new_v4());
let action = match processor
.process(UploadProcessorRequest {
context: &context,
current_path: staged.path(),
replacement_path: &replacement_path,
})
.await
{
Ok(action) => action,
Err(error) => {
remove_unused_replacement(&replacement_path).await?;
let _ = staged.discard().await;
return Err(map_processor_error(error));
}
};
match action {
UploadProcessorAction::Keep => remove_unused_replacement(&replacement_path).await?,
UploadProcessorAction::Replace => {
match staged.replace_with(replacement_path).await {
Ok(()) => {}
Err(error) => {
let _ = staged.discard().await;
return Err(S3Error::internal(format!(
"failed to replace staged upload: {error}"
)));
}
};
}
}
}
Ok(staged)
}
fn map_processor_error(error: UploadProcessorError) -> S3Error {
match error {
UploadProcessorError::Rejected(message) => S3Error::access_denied(message),
UploadProcessorError::Failed(message) => {
S3Error::internal(format!("upload processor failed: {message}"))
}
}
}
async fn remove_unused_replacement(path: &Path) -> Result<(), S3Error> {
match tokio::fs::remove_file(path).await {
Ok(()) => Ok(()),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(error) => Err(S3Error::internal(format!(
"failed to remove unused upload processor replacement file: {error}"
))),
}
}
impl From<StoreError> for UploadProcessorError {
fn from(error: StoreError) -> Self {
Self::Failed(error.to_string())
}
}
impl From<std::io::Error> for UploadProcessorError {
fn from(error: std::io::Error) -> Self {
Self::Failed(error.to_string())
}
}
pub(crate) fn processors_are_empty(processors: &[SharedUploadProcessor]) -> bool {
processors.is_empty()
}