mirror of
https://git.asonix.dog/asonix/pict-rs
synced 2024-12-22 03:11:24 +00:00
Remove a couple redundant db accesses
This commit is contained in:
parent
d4d7ce9366
commit
89161d03c9
2 changed files with 21 additions and 16 deletions
|
@ -8,25 +8,19 @@ use crate::{
|
||||||
formats::ProcessableFormat,
|
formats::ProcessableFormat,
|
||||||
magick::{MagickError, MAGICK_CONFIGURE_PATH, MAGICK_TEMPORARY_PATH},
|
magick::{MagickError, MAGICK_CONFIGURE_PATH, MAGICK_TEMPORARY_PATH},
|
||||||
process::Process,
|
process::Process,
|
||||||
repo::Alias,
|
repo::Hash,
|
||||||
state::State,
|
state::State,
|
||||||
store::Store,
|
store::Store,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) async fn generate<S>(
|
pub(crate) async fn generate<S>(
|
||||||
state: &State<S>,
|
state: &State<S>,
|
||||||
alias: &Alias,
|
hash: Hash,
|
||||||
original_details: &Details,
|
original_details: &Details,
|
||||||
) -> Result<String, Error>
|
) -> Result<String, Error>
|
||||||
where
|
where
|
||||||
S: Store + 'static,
|
S: Store + 'static,
|
||||||
{
|
{
|
||||||
let hash = state
|
|
||||||
.repo
|
|
||||||
.hash(alias)
|
|
||||||
.await?
|
|
||||||
.ok_or(UploadError::MissingIdentifier)?;
|
|
||||||
|
|
||||||
let identifier = if original_details.is_video() {
|
let identifier = if original_details.is_video() {
|
||||||
crate::generate::ensure_motion_identifier(state, hash, original_details).await?
|
crate::generate::ensure_motion_identifier(state, hash, original_details).await?
|
||||||
} else {
|
} else {
|
||||||
|
|
27
src/lib.rs
27
src/lib.rs
|
@ -120,6 +120,17 @@ async fn ensure_details<S: Store + 'static>(
|
||||||
ensure_details_identifier(state, &identifier).await
|
ensure_details_identifier(state, &identifier).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn ensure_details_hash<S: Store + 'static>(
|
||||||
|
state: &State<S>,
|
||||||
|
hash: Hash,
|
||||||
|
) -> Result<Details, Error> {
|
||||||
|
let Some(identifier) = state.repo.identifier(hash).await? else {
|
||||||
|
return Err(UploadError::MissingIdentifier.into());
|
||||||
|
};
|
||||||
|
|
||||||
|
ensure_details_identifier(state, &identifier).await
|
||||||
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip(state))]
|
#[tracing::instrument(skip(state))]
|
||||||
async fn ensure_details_identifier<S: Store + 'static>(
|
async fn ensure_details_identifier<S: Store + 'static>(
|
||||||
state: &State<S>,
|
state: &State<S>,
|
||||||
|
@ -1117,14 +1128,14 @@ async fn do_serve<S: Store + 'static>(
|
||||||
alias: Alias,
|
alias: Alias,
|
||||||
state: web::Data<State<S>>,
|
state: web::Data<State<S>>,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
let (hash, alias, not_found) = if let Some(hash) = state.repo.hash(&alias).await? {
|
let (hash, not_found) = if let Some(hash) = state.repo.hash(&alias).await? {
|
||||||
(hash, alias, false)
|
(hash, false)
|
||||||
} else {
|
} else {
|
||||||
let Some((alias, hash)) = not_found_hash(&state.repo).await? else {
|
let Some((_, hash)) = not_found_hash(&state.repo).await? else {
|
||||||
return Ok(HttpResponse::NotFound().finish());
|
return Ok(HttpResponse::NotFound().finish());
|
||||||
};
|
};
|
||||||
|
|
||||||
(hash, alias, true)
|
(hash, true)
|
||||||
};
|
};
|
||||||
|
|
||||||
let Some(identifier) = state.repo.identifier(hash.clone()).await? else {
|
let Some(identifier) = state.repo.identifier(hash.clone()).await? else {
|
||||||
|
@ -1133,7 +1144,7 @@ async fn do_serve<S: Store + 'static>(
|
||||||
return Ok(HttpResponse::NotFound().finish());
|
return Ok(HttpResponse::NotFound().finish());
|
||||||
};
|
};
|
||||||
|
|
||||||
let details = ensure_details(&state, &alias).await?;
|
let details = ensure_details_identifier(&state, &identifier).await?;
|
||||||
|
|
||||||
if let Some(public_url) = state.store.public_url(&identifier) {
|
if let Some(public_url) = state.store.public_url(&identifier) {
|
||||||
return Ok(HttpResponse::SeeOther()
|
return Ok(HttpResponse::SeeOther()
|
||||||
|
@ -1174,7 +1185,7 @@ async fn do_serve_head<S: Store + 'static>(
|
||||||
return Ok(HttpResponse::NotFound().finish());
|
return Ok(HttpResponse::NotFound().finish());
|
||||||
};
|
};
|
||||||
|
|
||||||
let details = ensure_details(&state, &alias).await?;
|
let details = ensure_details_identifier(&state, &identifier).await?;
|
||||||
|
|
||||||
if let Some(public_url) = state.store.public_url(&identifier) {
|
if let Some(public_url) = state.store.public_url(&identifier) {
|
||||||
return Ok(HttpResponse::SeeOther()
|
return Ok(HttpResponse::SeeOther()
|
||||||
|
@ -1385,8 +1396,8 @@ async fn blurhash<S: Store + 'static>(
|
||||||
let blurhash = if let Some(blurhash) = state.repo.blurhash(hash.clone()).await? {
|
let blurhash = if let Some(blurhash) = state.repo.blurhash(hash.clone()).await? {
|
||||||
blurhash
|
blurhash
|
||||||
} else {
|
} else {
|
||||||
let details = ensure_details(&state, &alias).await?;
|
let details = ensure_details_hash(&state, hash.clone()).await?;
|
||||||
let blurhash = blurhash::generate(&state, &alias, &details).await?;
|
let blurhash = blurhash::generate(&state, hash.clone(), &details).await?;
|
||||||
let blurhash: Arc<str> = Arc::from(blurhash);
|
let blurhash: Arc<str> = Arc::from(blurhash);
|
||||||
state.repo.relate_blurhash(hash, blurhash.clone()).await?;
|
state.repo.relate_blurhash(hash, blurhash.clone()).await?;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue