2022-04-01 16:51:46 +00:00
|
|
|
use crate::{
|
2023-07-23 00:41:50 +00:00
|
|
|
config::Configuration,
|
2022-04-02 21:44:03 +00:00
|
|
|
error::{Error, UploadError},
|
2022-10-02 03:47:52 +00:00
|
|
|
queue::{Base64Bytes, Cleanup, LocalBoxFuture},
|
2023-07-23 20:45:52 +00:00
|
|
|
repo::{
|
|
|
|
Alias, AliasAccessRepo, AliasRepo, DeleteToken, FullRepo, HashRepo, IdentifierRepo,
|
|
|
|
VariantAccessRepo,
|
|
|
|
},
|
2022-04-02 21:44:03 +00:00
|
|
|
serde_str::Serde,
|
2022-04-01 16:51:46 +00:00
|
|
|
store::{Identifier, Store},
|
|
|
|
};
|
2022-04-11 21:56:39 +00:00
|
|
|
use futures_util::StreamExt;
|
2022-04-01 16:51:46 +00:00
|
|
|
|
2022-04-01 21:51:12 +00:00
|
|
|
pub(super) fn perform<'a, R, S>(
|
|
|
|
repo: &'a R,
|
|
|
|
store: &'a S,
|
2023-07-23 00:41:50 +00:00
|
|
|
configuration: &'a Configuration,
|
2022-04-01 21:51:12 +00:00
|
|
|
job: &'a [u8],
|
|
|
|
) -> LocalBoxFuture<'a, Result<(), Error>>
|
|
|
|
where
|
2022-04-02 21:44:03 +00:00
|
|
|
R: FullRepo,
|
2022-04-01 21:51:12 +00:00
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
Box::pin(async move {
|
|
|
|
match serde_json::from_slice(job) {
|
|
|
|
Ok(job) => match job {
|
2022-10-02 03:47:52 +00:00
|
|
|
Cleanup::Hash {
|
|
|
|
hash: Base64Bytes(in_hash),
|
|
|
|
} => hash::<R, S>(repo, in_hash).await?,
|
2022-04-02 21:44:03 +00:00
|
|
|
Cleanup::Identifier {
|
2022-10-02 03:47:52 +00:00
|
|
|
identifier: Base64Bytes(in_identifier),
|
2022-04-11 21:56:39 +00:00
|
|
|
} => identifier(repo, store, in_identifier).await?,
|
2022-04-02 21:44:03 +00:00
|
|
|
Cleanup::Alias {
|
|
|
|
alias: stored_alias,
|
|
|
|
token,
|
|
|
|
} => {
|
|
|
|
alias(
|
|
|
|
repo,
|
|
|
|
Serde::into_inner(stored_alias),
|
|
|
|
Serde::into_inner(token),
|
|
|
|
)
|
|
|
|
.await?
|
|
|
|
}
|
2022-10-02 03:47:52 +00:00
|
|
|
Cleanup::Variant {
|
|
|
|
hash: Base64Bytes(hash),
|
2023-07-23 00:41:50 +00:00
|
|
|
variant,
|
|
|
|
} => hash_variant::<R, S>(repo, hash, variant).await?,
|
2022-04-11 21:56:39 +00:00
|
|
|
Cleanup::AllVariants => all_variants::<R, S>(repo).await?,
|
2023-07-23 00:41:50 +00:00
|
|
|
Cleanup::OutdatedVariants => outdated_variants::<R, S>(repo, configuration).await?,
|
2023-07-23 20:45:52 +00:00
|
|
|
Cleanup::OutdatedProxies => outdated_proxies::<R, S>(repo, configuration).await?,
|
2022-04-01 21:51:12 +00:00
|
|
|
},
|
|
|
|
Err(e) => {
|
2023-01-29 17:57:59 +00:00
|
|
|
tracing::warn!("Invalid job: {}", format!("{e}"));
|
2022-04-01 21:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-02 03:47:52 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2022-04-01 21:51:12 +00:00
|
|
|
async fn identifier<R, S>(repo: &R, store: &S, identifier: Vec<u8>) -> Result<(), Error>
|
2022-04-01 16:51:46 +00:00
|
|
|
where
|
2022-04-02 21:44:03 +00:00
|
|
|
R: FullRepo,
|
2022-04-01 16:51:46 +00:00
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
let identifier = S::Identifier::from_bytes(identifier)?;
|
|
|
|
|
|
|
|
let mut errors = Vec::new();
|
|
|
|
|
|
|
|
if let Err(e) = store.remove(&identifier).await {
|
|
|
|
errors.push(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(e) = IdentifierRepo::cleanup(repo, &identifier).await {
|
|
|
|
errors.push(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !errors.is_empty() {
|
|
|
|
let span = tracing::error_span!("Error deleting files");
|
|
|
|
span.in_scope(|| {
|
|
|
|
for error in errors {
|
2023-01-29 17:57:59 +00:00
|
|
|
tracing::error!("{}", format!("{error}"));
|
2022-04-01 16:51:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-02 03:47:52 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2022-04-01 21:51:12 +00:00
|
|
|
async fn hash<R, S>(repo: &R, hash: Vec<u8>) -> Result<(), Error>
|
2022-04-01 16:51:46 +00:00
|
|
|
where
|
2022-04-02 21:44:03 +00:00
|
|
|
R: FullRepo,
|
2022-04-01 16:51:46 +00:00
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
let hash: R::Bytes = hash.into();
|
|
|
|
|
|
|
|
let aliases = repo.aliases(hash.clone()).await?;
|
|
|
|
|
|
|
|
if !aliases.is_empty() {
|
2022-04-06 01:29:30 +00:00
|
|
|
for alias in aliases {
|
2023-07-07 18:17:26 +00:00
|
|
|
// TODO: decide if it is okay to skip aliases without tokens
|
|
|
|
if let Some(token) = repo.delete_token(&alias).await? {
|
|
|
|
super::cleanup_alias(repo, alias, token).await?;
|
2023-07-14 00:56:56 +00:00
|
|
|
} else {
|
|
|
|
tracing::warn!("Not cleaning alias!");
|
2023-07-07 18:17:26 +00:00
|
|
|
}
|
2022-04-06 01:29:30 +00:00
|
|
|
}
|
|
|
|
// Return after queueing cleanup alias, since we will be requeued when the last alias is cleaned
|
2022-04-01 16:51:46 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut idents = repo
|
|
|
|
.variants::<S::Identifier>(hash.clone())
|
|
|
|
.await?
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, v)| v)
|
|
|
|
.collect::<Vec<_>>();
|
2023-07-07 18:17:26 +00:00
|
|
|
idents.extend(repo.identifier(hash.clone()).await?);
|
2022-04-01 16:51:46 +00:00
|
|
|
idents.extend(repo.motion_identifier(hash.clone()).await?);
|
|
|
|
|
|
|
|
for identifier in idents {
|
2022-04-11 21:56:39 +00:00
|
|
|
let _ = super::cleanup_identifier(repo, identifier).await;
|
2022-04-01 16:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HashRepo::cleanup(repo, hash).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-02 21:44:03 +00:00
|
|
|
|
2022-10-02 03:47:52 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2022-04-02 21:44:03 +00:00
|
|
|
async fn alias<R>(repo: &R, alias: Alias, token: DeleteToken) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
R: FullRepo,
|
|
|
|
{
|
|
|
|
let saved_delete_token = repo.delete_token(&alias).await?;
|
2023-07-07 18:17:26 +00:00
|
|
|
|
|
|
|
if saved_delete_token.is_some() && saved_delete_token != Some(token) {
|
2022-04-02 21:44:03 +00:00
|
|
|
return Err(UploadError::InvalidToken.into());
|
|
|
|
}
|
|
|
|
|
2023-07-14 00:58:31 +00:00
|
|
|
let hash = repo.hash(&alias).await?;
|
|
|
|
|
|
|
|
AliasRepo::cleanup(repo, &alias).await?;
|
2023-07-23 20:45:52 +00:00
|
|
|
repo.remove_relation(alias.clone()).await?;
|
|
|
|
AliasAccessRepo::remove_access(repo, alias.clone()).await?;
|
2023-07-14 00:58:31 +00:00
|
|
|
|
|
|
|
let Some(hash) = hash else {
|
2023-07-05 21:46:44 +00:00
|
|
|
// hash doesn't exist, nothing to do
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
|
2022-04-02 21:44:03 +00:00
|
|
|
repo.remove_alias(hash.clone(), &alias).await?;
|
|
|
|
|
|
|
|
if repo.aliases(hash.clone()).await?.is_empty() {
|
2022-04-11 21:56:39 +00:00
|
|
|
super::cleanup_hash(repo, hash).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-07-23 00:41:50 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2022-04-11 21:56:39 +00:00
|
|
|
async fn all_variants<R, S>(repo: &R) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
R: FullRepo,
|
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
let mut hash_stream = Box::pin(repo.hashes().await);
|
|
|
|
|
|
|
|
while let Some(res) = hash_stream.next().await {
|
|
|
|
let hash = res?;
|
2023-07-23 00:41:50 +00:00
|
|
|
super::cleanup_variants(repo, hash, None).await?;
|
2022-04-11 21:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-07-23 00:41:50 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
async fn outdated_variants<R, S>(repo: &R, config: &Configuration) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
R: FullRepo,
|
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
let now = time::OffsetDateTime::now_utc();
|
|
|
|
let since = now.saturating_sub(config.media.retention.variants.to_duration());
|
|
|
|
|
|
|
|
let mut variant_stream = Box::pin(repo.older_variants(since).await?);
|
|
|
|
|
|
|
|
while let Some(res) = variant_stream.next().await {
|
|
|
|
let (hash, variant) = res?;
|
|
|
|
super::cleanup_variants(repo, hash, Some(variant)).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2023-07-23 20:45:52 +00:00
|
|
|
async fn outdated_proxies<R, S>(repo: &R, config: &Configuration) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
R: FullRepo,
|
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
let now = time::OffsetDateTime::now_utc();
|
|
|
|
let since = now.saturating_sub(config.media.retention.proxy.to_duration());
|
|
|
|
|
|
|
|
let mut alias_stream = Box::pin(repo.older_aliases(since).await?);
|
|
|
|
|
|
|
|
while let Some(res) = alias_stream.next().await {
|
|
|
|
let alias = res?;
|
|
|
|
if let Some(token) = repo.delete_token(&alias).await? {
|
|
|
|
super::cleanup_alias(repo, alias, token).await?;
|
|
|
|
} else {
|
|
|
|
tracing::warn!("Skipping alias cleanup - no delete token");
|
|
|
|
repo.remove_relation(alias.clone()).await?;
|
|
|
|
AliasAccessRepo::remove_access(repo, alias).await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2023-07-23 00:41:50 +00:00
|
|
|
async fn hash_variant<R, S>(
|
|
|
|
repo: &R,
|
|
|
|
hash: Vec<u8>,
|
|
|
|
target_variant: Option<String>,
|
|
|
|
) -> Result<(), Error>
|
2022-04-11 21:56:39 +00:00
|
|
|
where
|
|
|
|
R: FullRepo,
|
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
let hash: R::Bytes = hash.into();
|
|
|
|
|
2023-07-23 00:41:50 +00:00
|
|
|
if let Some(target_variant) = target_variant {
|
|
|
|
if let Some(identifier) = repo
|
|
|
|
.variant_identifier::<S::Identifier>(hash.clone(), target_variant.clone())
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
super::cleanup_identifier(repo, identifier).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.remove_variant(hash.clone(), target_variant.clone())
|
|
|
|
.await?;
|
|
|
|
VariantAccessRepo::remove_access(repo, hash, target_variant).await?;
|
|
|
|
} else {
|
|
|
|
for (variant, identifier) in repo.variants::<S::Identifier>(hash.clone()).await? {
|
|
|
|
repo.remove_variant(hash.clone(), variant.clone()).await?;
|
|
|
|
VariantAccessRepo::remove_access(repo, hash.clone(), variant).await?;
|
|
|
|
super::cleanup_identifier(repo, identifier).await?;
|
|
|
|
}
|
2022-04-02 21:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|