2022-04-02 22:40:04 +00:00
|
|
|
use crate::{
|
2023-07-22 16:15:30 +00:00
|
|
|
concurrent_processor::ProcessMap,
|
2022-04-02 22:40:04 +00:00
|
|
|
details::Details,
|
2023-07-07 18:17:26 +00:00
|
|
|
error::{Error, UploadError},
|
2023-07-13 03:12:21 +00:00
|
|
|
ffmpeg::ThumbnailFormat,
|
|
|
|
formats::{InputProcessableFormat, InternalVideoFormat},
|
2022-04-02 22:40:04 +00:00
|
|
|
repo::{Alias, FullRepo},
|
|
|
|
store::Store,
|
|
|
|
};
|
|
|
|
use actix_web::web::Bytes;
|
2023-07-22 21:47:59 +00:00
|
|
|
use std::{path::PathBuf, time::Instant};
|
2022-04-02 22:40:04 +00:00
|
|
|
use tokio::io::AsyncReadExt;
|
2022-04-07 17:56:40 +00:00
|
|
|
use tracing::Instrument;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
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());
|
|
|
|
if self.armed {
|
|
|
|
metrics::increment_counter!("pict-rs.generate.failure");
|
|
|
|
} else {
|
|
|
|
metrics::increment_counter!("pict-rs.generate.success");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 01:02:46 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-10-02 03:47:52 +00:00
|
|
|
#[tracing::instrument(skip(repo, store, hash))]
|
2022-04-02 22:40:04 +00:00
|
|
|
pub(crate) async fn generate<R: FullRepo, S: Store + 'static>(
|
|
|
|
repo: &R,
|
|
|
|
store: &S,
|
2023-07-22 16:15:30 +00:00
|
|
|
process_map: &ProcessMap,
|
2023-07-13 03:12:21 +00:00
|
|
|
format: InputProcessableFormat,
|
2022-04-02 22:40:04 +00:00
|
|
|
alias: Alias,
|
|
|
|
thumbnail_path: PathBuf,
|
|
|
|
thumbnail_args: Vec<String>,
|
2023-07-13 03:12:21 +00:00
|
|
|
input_format: Option<InternalVideoFormat>,
|
2022-10-01 00:38:11 +00:00
|
|
|
thumbnail_format: Option<ThumbnailFormat>,
|
2023-07-22 17:31:01 +00:00
|
|
|
media: &crate::config::Media,
|
2022-04-02 22:40:04 +00:00
|
|
|
hash: R::Bytes,
|
|
|
|
) -> Result<(Details, Bytes), Error> {
|
|
|
|
let process_fut = process(
|
|
|
|
repo,
|
|
|
|
store,
|
|
|
|
format,
|
|
|
|
alias,
|
|
|
|
thumbnail_path.clone(),
|
|
|
|
thumbnail_args,
|
2022-10-01 00:38:11 +00:00
|
|
|
input_format,
|
|
|
|
thumbnail_format,
|
2023-07-18 21:18:01 +00:00
|
|
|
media,
|
2022-04-02 22:40:04 +00:00
|
|
|
hash.clone(),
|
|
|
|
);
|
|
|
|
|
2023-07-22 16:15:30 +00:00
|
|
|
let (details, bytes) = process_map
|
|
|
|
.process(hash.as_ref(), thumbnail_path, process_fut)
|
|
|
|
.await?;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
|
|
|
Ok((details, bytes))
|
|
|
|
}
|
|
|
|
|
2022-10-01 01:02:46 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-10-02 03:47:52 +00:00
|
|
|
#[tracing::instrument(skip(repo, store, hash))]
|
2022-04-02 22:40:04 +00:00
|
|
|
async fn process<R: FullRepo, S: Store + 'static>(
|
|
|
|
repo: &R,
|
|
|
|
store: &S,
|
2023-07-13 18:48:59 +00:00
|
|
|
output_format: InputProcessableFormat,
|
2022-04-02 22:40:04 +00:00
|
|
|
alias: Alias,
|
|
|
|
thumbnail_path: PathBuf,
|
|
|
|
thumbnail_args: Vec<String>,
|
2023-07-13 03:12:21 +00:00
|
|
|
input_format: Option<InternalVideoFormat>,
|
2022-10-01 00:38:11 +00:00
|
|
|
thumbnail_format: Option<ThumbnailFormat>,
|
2023-07-22 17:31:01 +00:00
|
|
|
media: &crate::config::Media,
|
2022-04-02 22:40:04 +00:00
|
|
|
hash: R::Bytes,
|
|
|
|
) -> 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;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
|
|
|
let identifier = if let Some(identifier) = repo
|
|
|
|
.still_identifier_from_alias::<S::Identifier>(&alias)
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
identifier
|
|
|
|
} else {
|
2023-07-07 18:17:26 +00:00
|
|
|
let Some(identifier) = repo.identifier(hash.clone()).await? else {
|
|
|
|
return Err(UploadError::MissingIdentifier.into());
|
|
|
|
};
|
|
|
|
|
2023-07-14 19:53:37 +00:00
|
|
|
let thumbnail_format = thumbnail_format.unwrap_or(ThumbnailFormat::Jpeg);
|
|
|
|
|
2022-09-24 22:18:53 +00:00
|
|
|
let reader = crate::ffmpeg::thumbnail(
|
2022-04-02 22:40:04 +00:00
|
|
|
store.clone(),
|
|
|
|
identifier,
|
2023-07-13 03:12:21 +00:00
|
|
|
input_format.unwrap_or(InternalVideoFormat::Mp4),
|
2023-07-14 19:53:37 +00:00
|
|
|
thumbnail_format,
|
2022-04-02 22:40:04 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2023-07-14 19:53:37 +00:00
|
|
|
|
|
|
|
let motion_identifier = store
|
|
|
|
.save_async_read(reader, thumbnail_format.media_type())
|
|
|
|
.await?;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
|
|
|
repo.relate_motion_identifier(hash.clone(), &motion_identifier)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
motion_identifier
|
|
|
|
};
|
|
|
|
|
2023-07-13 18:48:59 +00:00
|
|
|
let input_details = if let Some(details) = repo.details(&identifier).await? {
|
|
|
|
details
|
|
|
|
} else {
|
|
|
|
let details = Details::from_store(store, &identifier).await?;
|
|
|
|
|
|
|
|
repo.relate_details(&identifier, &details).await?;
|
|
|
|
|
|
|
|
details
|
|
|
|
};
|
|
|
|
|
2023-07-14 00:21:57 +00:00
|
|
|
let input_format = input_details
|
|
|
|
.internal_format()
|
|
|
|
.and_then(|format| format.processable_format())
|
|
|
|
.expect("Valid details should always have internal format");
|
2023-07-13 18:48:59 +00:00
|
|
|
|
2023-07-14 00:21:57 +00:00
|
|
|
let Some(format) = input_format.process_to(output_format) else {
|
|
|
|
return Err(UploadError::InvalidProcessExtension.into());
|
2023-07-13 18:48:59 +00:00
|
|
|
};
|
|
|
|
|
2023-07-18 21:18:01 +00:00
|
|
|
let quality = match format {
|
|
|
|
crate::formats::ProcessableFormat::Image(format) => media.image.quality_for(format),
|
|
|
|
crate::formats::ProcessableFormat::Animation(format) => media.animation.quality_for(format),
|
|
|
|
};
|
|
|
|
|
2023-07-14 00:21:57 +00:00
|
|
|
let mut processed_reader = crate::magick::process_image_store_read(
|
|
|
|
store,
|
|
|
|
&identifier,
|
|
|
|
thumbnail_args,
|
|
|
|
input_format,
|
|
|
|
format,
|
2023-07-18 21:18:01 +00:00
|
|
|
quality,
|
2023-07-14 00:21:57 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
|
|
|
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?;
|
2022-04-02 22:40:04 +00:00
|
|
|
let bytes = Bytes::from(vec);
|
|
|
|
|
|
|
|
drop(permit);
|
|
|
|
|
2023-07-13 18:48:59 +00:00
|
|
|
let details = Details::from_bytes(bytes.clone()).await?;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
2023-07-14 19:53:37 +00:00
|
|
|
let identifier = store
|
|
|
|
.save_bytes(bytes.clone(), details.media_type())
|
|
|
|
.await?;
|
2022-04-02 22:40:04 +00:00
|
|
|
repo.relate_details(&identifier, &details).await?;
|
|
|
|
repo.relate_variant_identifier(
|
|
|
|
hash,
|
|
|
|
thumbnail_path.to_string_lossy().to_string(),
|
|
|
|
&identifier,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2023-07-22 21:47:59 +00:00
|
|
|
guard.disarm();
|
|
|
|
|
2022-04-02 22:40:04 +00:00
|
|
|
Ok((details, bytes)) as Result<(Details, Bytes), Error>
|
|
|
|
}
|