2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-20 11:21:14 +00:00
pict-rs/src/generate.rs

188 lines
5.1 KiB
Rust
Raw Normal View History

use crate::{
2023-07-22 16:15:30 +00:00
concurrent_processor::ProcessMap,
details::Details,
error::{Error, UploadError},
ffmpeg::ThumbnailFormat,
formats::{InputProcessableFormat, InternalVideoFormat},
2023-08-16 20:12:16 +00:00
repo::{Alias, ArcRepo, Hash, VariantAlreadyExists},
store::Store,
};
use actix_web::web::Bytes;
2023-07-22 21:47:59 +00:00
use std::{path::PathBuf, time::Instant};
use tokio::io::AsyncReadExt;
2022-04-07 17:56:40 +00:00
use tracing::Instrument;
2023-07-22 21:47:59 +00:00
struct MetricsGuard {
start: Instant,
armed: bool,
}
impl MetricsGuard {
fn guard() -> Self {
metrics::increment_counter!("pict-rs.generate.start");
Self {
start: Instant::now(),
armed: true,
}
}
fn disarm(mut self) {
self.armed = false;
}
}
impl Drop for MetricsGuard {
fn drop(&mut self) {
metrics::histogram!("pict-rs.generate.duration", self.start.elapsed().as_secs_f64(), "completed" => (!self.armed).to_string());
2023-07-23 02:11:28 +00:00
metrics::increment_counter!("pict-rs.generate.end", "completed" => (!self.armed).to_string());
2023-07-22 21:47:59 +00:00
}
}
2022-10-01 01:02:46 +00:00
#[allow(clippy::too_many_arguments)]
#[tracing::instrument(skip(repo, store, hash, process_map, media))]
pub(crate) async fn generate<S: Store + 'static>(
repo: &ArcRepo,
store: &S,
2023-07-22 16:15:30 +00:00
process_map: &ProcessMap,
format: InputProcessableFormat,
alias: Alias,
thumbnail_path: PathBuf,
thumbnail_args: Vec<String>,
input_format: Option<InternalVideoFormat>,
thumbnail_format: Option<ThumbnailFormat>,
media: &crate::config::Media,
2023-08-14 19:25:19 +00:00
hash: Hash,
) -> Result<(Details, Bytes), Error> {
let process_fut = process(
repo,
store,
format,
alias,
thumbnail_path.clone(),
thumbnail_args,
input_format,
thumbnail_format,
media,
hash.clone(),
);
2023-07-22 16:15:30 +00:00
let (details, bytes) = process_map
2023-08-14 19:25:19 +00:00
.process(hash, thumbnail_path, process_fut)
2023-07-22 16:15:30 +00:00
.await?;
Ok((details, bytes))
}
2022-10-01 01:02:46 +00:00
#[allow(clippy::too_many_arguments)]
#[tracing::instrument(skip(repo, store, hash, media))]
async fn process<S: Store + 'static>(
repo: &ArcRepo,
store: &S,
output_format: InputProcessableFormat,
alias: Alias,
thumbnail_path: PathBuf,
thumbnail_args: Vec<String>,
input_format: Option<InternalVideoFormat>,
thumbnail_format: Option<ThumbnailFormat>,
media: &crate::config::Media,
2023-08-14 19:25:19 +00:00
hash: Hash,
) -> Result<(Details, Bytes), Error> {
2023-07-22 21:47:59 +00:00
let guard = MetricsGuard::guard();
2022-04-07 17:56:40 +00:00
let permit = crate::PROCESS_SEMAPHORE.acquire().await;
let identifier = if let Some(identifier) = repo.still_identifier_from_alias(&alias).await? {
identifier
} else {
let Some(identifier) = repo.identifier(hash.clone()).await? else {
return Err(UploadError::MissingIdentifier.into());
};
let thumbnail_format = thumbnail_format.unwrap_or(ThumbnailFormat::Jpeg);
2022-09-24 22:18:53 +00:00
let reader = crate::ffmpeg::thumbnail(
store.clone(),
identifier,
input_format.unwrap_or(InternalVideoFormat::Mp4),
thumbnail_format,
2023-08-05 17:41:06 +00:00
media.process_timeout,
)
.await?;
let motion_identifier = store
.save_async_read(reader, thumbnail_format.media_type())
.await?;
repo.relate_motion_identifier(hash.clone(), &motion_identifier)
.await?;
motion_identifier
};
let input_details = if let Some(details) = repo.details(&identifier).await? {
details
} else {
2023-08-05 17:41:06 +00:00
let details = Details::from_store(store, &identifier, media.process_timeout).await?;
repo.relate_details(&identifier, &details).await?;
details
};
let input_format = input_details
.internal_format()
2023-08-16 17:36:18 +00:00
.processable_format()
.expect("Already verified format is processable");
let Some(format) = input_format.process_to(output_format) else {
return Err(UploadError::InvalidProcessExtension.into());
};
let quality = match format {
crate::formats::ProcessableFormat::Image(format) => media.image.quality_for(format),
crate::formats::ProcessableFormat::Animation(format) => media.animation.quality_for(format),
};
let mut processed_reader = crate::magick::process_image_store_read(
store,
&identifier,
thumbnail_args,
input_format,
format,
quality,
2023-08-05 17:41:06 +00:00
media.process_timeout,
)
.await?;
let mut vec = Vec::new();
2022-04-07 17:56:40 +00:00
processed_reader
.read_to_end(&mut vec)
.instrument(tracing::info_span!("Reading processed image to vec"))
.await?;
let bytes = Bytes::from(vec);
drop(permit);
2023-08-05 17:41:06 +00:00
let details = Details::from_bytes(media.process_timeout, bytes.clone()).await?;
let identifier = store
.save_bytes(bytes.clone(), details.media_type())
.await?;
2023-08-16 20:12:16 +00:00
if let Err(VariantAlreadyExists) = repo
.relate_variant_identifier(
hash,
thumbnail_path.to_string_lossy().to_string(),
&identifier,
)
.await?
{
store.remove(&identifier).await?;
}
repo.relate_details(&identifier, &details).await?;
2023-07-22 21:47:59 +00:00
guard.disarm();
Ok((details, bytes)) as Result<(Details, Bytes), Error>
}