2022-04-02 21:44:03 +00:00
|
|
|
use crate::{
|
|
|
|
error::{Error, UploadError},
|
|
|
|
magick::ValidInputType,
|
|
|
|
repo::{Alias, AliasRepo, DeleteToken, FullRepo, HashRepo},
|
|
|
|
store::Store,
|
|
|
|
CONFIG,
|
|
|
|
};
|
|
|
|
use actix_web::web::{Bytes, BytesMut};
|
|
|
|
use futures_util::{Stream, StreamExt};
|
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
|
|
|
|
mod hasher;
|
|
|
|
use hasher::Hasher;
|
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
#[derive(Debug)]
|
2022-04-02 21:44:03 +00:00
|
|
|
pub(crate) struct Session<R, S>
|
|
|
|
where
|
|
|
|
R: FullRepo + 'static,
|
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
repo: R,
|
|
|
|
hash: Option<Vec<u8>>,
|
|
|
|
alias: Option<Alias>,
|
|
|
|
identifier: Option<S::Identifier>,
|
|
|
|
}
|
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
#[tracing::instrument(name = "Aggregate", skip(stream))]
|
|
|
|
async fn aggregate<S>(stream: S) -> Result<Bytes, Error>
|
|
|
|
where
|
|
|
|
S: Stream<Item = Result<Bytes, Error>>,
|
|
|
|
{
|
|
|
|
futures_util::pin_mut!(stream);
|
|
|
|
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
tracing::debug!("Reading stream to memory");
|
|
|
|
while let Some(res) = stream.next().await {
|
|
|
|
let bytes = res?;
|
|
|
|
buf.push(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
let total_len = buf.iter().fold(0, |acc, item| acc + item.len());
|
|
|
|
|
|
|
|
let bytes_mut = buf
|
|
|
|
.iter()
|
|
|
|
.fold(BytesMut::with_capacity(total_len), |mut acc, item| {
|
|
|
|
acc.extend_from_slice(item);
|
|
|
|
acc
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(bytes_mut.freeze())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(name = "Ingest", skip(stream))]
|
2022-04-02 21:44:03 +00:00
|
|
|
pub(crate) async fn ingest<R, S>(
|
|
|
|
repo: &R,
|
|
|
|
store: &S,
|
|
|
|
stream: impl Stream<Item = Result<Bytes, Error>>,
|
|
|
|
declared_alias: Option<Alias>,
|
|
|
|
should_validate: bool,
|
|
|
|
) -> Result<Session<R, S>, Error>
|
|
|
|
where
|
|
|
|
R: FullRepo + 'static,
|
|
|
|
S: Store,
|
|
|
|
{
|
2022-04-07 02:40:49 +00:00
|
|
|
let permit = tracing::trace_span!(parent: None, "Aquire semaphore")
|
|
|
|
.in_scope(|| crate::PROCESS_SEMAPHORE.acquire())
|
|
|
|
.await;
|
2022-04-02 21:44:03 +00:00
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
let bytes = aggregate(stream).await?;
|
2022-04-02 21:44:03 +00:00
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
tracing::debug!("Validating bytes");
|
2022-04-02 21:44:03 +00:00
|
|
|
let (input_type, validated_reader) = crate::validate::validate_image_bytes(
|
2022-04-07 02:40:49 +00:00
|
|
|
bytes,
|
2022-04-02 21:44:03 +00:00
|
|
|
CONFIG.media.format,
|
|
|
|
CONFIG.media.enable_silent_video,
|
|
|
|
should_validate,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let mut hasher_reader = Hasher::new(validated_reader, Sha256::new());
|
|
|
|
|
|
|
|
let identifier = store.save_async_read(&mut hasher_reader).await?;
|
|
|
|
|
|
|
|
drop(permit);
|
|
|
|
|
|
|
|
let mut session = Session {
|
|
|
|
repo: repo.clone(),
|
|
|
|
hash: None,
|
|
|
|
alias: None,
|
|
|
|
identifier: Some(identifier.clone()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let hash = hasher_reader.finalize_reset().await?;
|
|
|
|
|
|
|
|
session.hash = Some(hash.clone());
|
|
|
|
|
|
|
|
save_upload(repo, store, &hash, &identifier).await?;
|
|
|
|
|
|
|
|
if let Some(alias) = declared_alias {
|
|
|
|
session.add_existing_alias(&hash, alias).await?
|
|
|
|
} else {
|
|
|
|
session.create_alias(&hash, input_type).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(session)
|
|
|
|
}
|
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
#[tracing::instrument]
|
2022-04-02 21:44:03 +00:00
|
|
|
async fn save_upload<R, S>(
|
|
|
|
repo: &R,
|
|
|
|
store: &S,
|
|
|
|
hash: &[u8],
|
|
|
|
identifier: &S::Identifier,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
S: Store,
|
|
|
|
R: FullRepo,
|
|
|
|
{
|
|
|
|
if HashRepo::create(repo, hash.to_vec().into()).await?.is_err() {
|
|
|
|
store.remove(identifier).await?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.relate_identifier(hash.to_vec().into(), identifier)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R, S> Session<R, S>
|
|
|
|
where
|
|
|
|
R: FullRepo + 'static,
|
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
pub(crate) fn disarm(&mut self) {
|
2022-04-06 02:47:35 +00:00
|
|
|
let _ = self.hash.take();
|
2022-04-02 21:44:03 +00:00
|
|
|
let _ = self.alias.take();
|
|
|
|
let _ = self.identifier.take();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn alias(&self) -> Option<&Alias> {
|
|
|
|
self.alias.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
#[tracing::instrument]
|
2022-04-02 21:44:03 +00:00
|
|
|
pub(crate) async fn delete_token(&self) -> Result<DeleteToken, Error> {
|
|
|
|
let alias = self.alias.clone().ok_or(UploadError::MissingAlias)?;
|
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
tracing::debug!("Generating delete token");
|
2022-04-02 21:44:03 +00:00
|
|
|
let delete_token = DeleteToken::generate();
|
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
tracing::debug!("Saving delete token");
|
2022-04-02 21:44:03 +00:00
|
|
|
let res = self.repo.relate_delete_token(&alias, &delete_token).await?;
|
|
|
|
|
|
|
|
if res.is_err() {
|
|
|
|
let delete_token = self.repo.delete_token(&alias).await?;
|
2022-04-07 02:40:49 +00:00
|
|
|
tracing::debug!("Returning existing delete token, {:?}", delete_token);
|
2022-04-02 21:44:03 +00:00
|
|
|
return Ok(delete_token);
|
|
|
|
}
|
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
tracing::debug!("Returning new delete token, {:?}", delete_token);
|
2022-04-02 21:44:03 +00:00
|
|
|
Ok(delete_token)
|
|
|
|
}
|
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
#[tracing::instrument]
|
2022-04-02 21:44:03 +00:00
|
|
|
async fn add_existing_alias(&mut self, hash: &[u8], alias: Alias) -> Result<(), Error> {
|
|
|
|
AliasRepo::create(&self.repo, &alias)
|
|
|
|
.await?
|
|
|
|
.map_err(|_| UploadError::DuplicateAlias)?;
|
|
|
|
|
|
|
|
self.alias = Some(alias.clone());
|
|
|
|
|
|
|
|
self.repo.relate_hash(&alias, hash.to_vec().into()).await?;
|
|
|
|
self.repo.relate_alias(hash.to_vec().into(), &alias).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
#[tracing::instrument]
|
2022-04-02 21:44:03 +00:00
|
|
|
async fn create_alias(&mut self, hash: &[u8], input_type: ValidInputType) -> Result<(), Error> {
|
2022-04-07 02:40:49 +00:00
|
|
|
tracing::debug!("Alias gen loop");
|
2022-04-02 21:44:03 +00:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let alias = Alias::generate(input_type.as_ext().to_string());
|
|
|
|
|
|
|
|
if AliasRepo::create(&self.repo, &alias).await?.is_ok() {
|
|
|
|
self.alias = Some(alias.clone());
|
|
|
|
|
|
|
|
self.repo.relate_hash(&alias, hash.to_vec().into()).await?;
|
|
|
|
self.repo.relate_alias(hash.to_vec().into(), &alias).await?;
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
tracing::debug!("Alias exists, regenerating");
|
2022-04-02 21:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R, S> Drop for Session<R, S>
|
|
|
|
where
|
|
|
|
R: FullRepo + 'static,
|
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(hash) = self.hash.take() {
|
|
|
|
let repo = self.repo.clone();
|
|
|
|
actix_rt::spawn(async move {
|
|
|
|
let _ = crate::queue::cleanup_hash(&repo, hash.into()).await;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(alias) = self.alias.take() {
|
|
|
|
let repo = self.repo.clone();
|
|
|
|
|
|
|
|
actix_rt::spawn(async move {
|
|
|
|
if let Ok(token) = repo.delete_token(&alias).await {
|
|
|
|
let _ = crate::queue::cleanup_alias(&repo, alias, token).await;
|
|
|
|
} else {
|
|
|
|
let token = DeleteToken::generate();
|
|
|
|
if let Ok(Ok(())) = repo.relate_delete_token(&alias, &token).await {
|
|
|
|
let _ = crate::queue::cleanup_alias(&repo, alias, token).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(identifier) = self.identifier.take() {
|
|
|
|
let repo = self.repo.clone();
|
|
|
|
|
|
|
|
actix_rt::spawn(async move {
|
|
|
|
let _ = crate::queue::cleanup_identifier(&repo, identifier).await;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|