2023-09-24 16:54:16 +00:00
|
|
|
mod ffmpeg;
|
|
|
|
mod magick;
|
|
|
|
|
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-09-24 16:54:16 +00:00
|
|
|
formats::{ImageFormat, InputProcessableFormat, InternalVideoFormat, ProcessableFormat},
|
2023-12-11 01:11:36 +00:00
|
|
|
future::{WithMetrics, WithTimeout},
|
2024-02-03 19:31:54 +00:00
|
|
|
repo::{Hash, VariantAlreadyExists},
|
|
|
|
state::State,
|
2023-09-02 23:30:45 +00:00
|
|
|
store::Store,
|
2022-04-02 22:40:04 +00:00
|
|
|
};
|
|
|
|
use actix_web::web::Bytes;
|
2023-12-11 01:11:36 +00:00
|
|
|
use std::{
|
|
|
|
path::PathBuf,
|
|
|
|
sync::Arc,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2023-12-23 02:54:02 +00:00
|
|
|
|
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 {
|
2023-12-27 00:06:38 +00:00
|
|
|
metrics::counter!("pict-rs.generate.start").increment(1);
|
2023-07-22 21:47:59 +00:00
|
|
|
Self {
|
|
|
|
start: Instant::now(),
|
|
|
|
armed: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disarm(mut self) {
|
|
|
|
self.armed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for MetricsGuard {
|
|
|
|
fn drop(&mut self) {
|
2023-12-27 00:06:38 +00:00
|
|
|
metrics::histogram!("pict-rs.generate.duration", "completed" => (!self.armed).to_string())
|
|
|
|
.record(self.start.elapsed().as_secs_f64());
|
|
|
|
metrics::counter!("pict-rs.generate.end", "completed" => (!self.armed).to_string())
|
|
|
|
.increment(1);
|
2023-07-22 21:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(skip(state, process_map, hash))]
|
2023-08-16 00:19:03 +00:00
|
|
|
pub(crate) async fn generate<S: Store + 'static>(
|
2024-02-03 19:31:54 +00:00
|
|
|
state: &State<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
|
|
|
thumbnail_path: PathBuf,
|
|
|
|
thumbnail_args: Vec<String>,
|
2023-09-24 16:54:16 +00:00
|
|
|
original_details: &Details,
|
2023-08-14 19:25:19 +00:00
|
|
|
hash: Hash,
|
2022-04-02 22:40:04 +00:00
|
|
|
) -> Result<(Details, Bytes), Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.danger_dummy_mode {
|
|
|
|
let identifier = state
|
|
|
|
.repo
|
2023-11-11 20:22:12 +00:00
|
|
|
.identifier(hash)
|
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingIdentifier)?;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let bytes = state
|
|
|
|
.store
|
|
|
|
.to_bytes(&identifier, None, None)
|
|
|
|
.await?
|
|
|
|
.into_bytes();
|
2022-04-02 22:40:04 +00:00
|
|
|
|
2023-11-11 20:22:12 +00:00
|
|
|
Ok((original_details.clone(), bytes))
|
|
|
|
} else {
|
|
|
|
let process_fut = process(
|
2024-02-03 19:31:54 +00:00
|
|
|
state,
|
2023-11-11 20:22:12 +00:00
|
|
|
format,
|
|
|
|
thumbnail_path.clone(),
|
|
|
|
thumbnail_args,
|
|
|
|
original_details,
|
|
|
|
hash.clone(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (details, bytes) = process_map
|
|
|
|
.process(hash, thumbnail_path, process_fut)
|
2024-02-03 19:31:54 +00:00
|
|
|
.with_timeout(Duration::from_secs(state.config.media.process_timeout * 4))
|
2023-12-11 01:11:36 +00:00
|
|
|
.with_metrics("pict-rs.generate.process")
|
|
|
|
.await
|
|
|
|
.map_err(|_| UploadError::ProcessTimeout)??;
|
2023-11-11 20:22:12 +00:00
|
|
|
|
|
|
|
Ok((details, bytes))
|
|
|
|
}
|
2022-04-02 22:40:04 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 01:02:46 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(skip(state, hash))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn process<S: Store + 'static>(
|
2024-02-03 19:31:54 +00:00
|
|
|
state: &State<S>,
|
2023-07-13 18:48:59 +00:00
|
|
|
output_format: InputProcessableFormat,
|
2022-04-02 22:40:04 +00:00
|
|
|
thumbnail_path: PathBuf,
|
|
|
|
thumbnail_args: Vec<String>,
|
2023-09-24 16:54:16 +00:00
|
|
|
original_details: &Details,
|
2023-08-14 19:25:19 +00:00
|
|
|
hash: Hash,
|
2022-04-02 22:40:04 +00:00
|
|
|
) -> Result<(Details, Bytes), Error> {
|
2023-07-22 21:47:59 +00:00
|
|
|
let guard = MetricsGuard::guard();
|
2023-09-24 20:32:00 +00:00
|
|
|
let permit = crate::process_semaphore().acquire().await?;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let identifier = input_identifier(state, output_format, hash.clone(), original_details).await?;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let input_details = crate::ensure_details_identifier(state, &identifier).await?;
|
2023-07-13 18:48:59 +00:00
|
|
|
|
2023-07-14 00:21:57 +00:00
|
|
|
let input_format = input_details
|
|
|
|
.internal_format()
|
2023-08-16 17:36:18 +00:00
|
|
|
.processable_format()
|
|
|
|
.expect("Already verified format is processable");
|
2023-07-13 18:48:59 +00:00
|
|
|
|
2023-09-24 18:20:33 +00:00
|
|
|
let format = input_format.process_to(output_format);
|
2023-07-13 18:48:59 +00:00
|
|
|
|
2023-07-18 21:18:01 +00:00
|
|
|
let quality = match format {
|
2024-02-03 19:31:54 +00:00
|
|
|
ProcessableFormat::Image(format) => state.config.media.image.quality_for(format),
|
|
|
|
ProcessableFormat::Animation(format) => state.config.media.animation.quality_for(format),
|
2023-07-18 21:18:01 +00:00
|
|
|
};
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let stream = state.store.to_stream(&identifier, None, None).await?;
|
2023-12-23 17:58:20 +00:00
|
|
|
|
|
|
|
let vec = crate::magick::process_image_stream_read(
|
2024-02-03 19:31:54 +00:00
|
|
|
state,
|
2023-12-23 17:58:20 +00:00
|
|
|
stream,
|
2023-07-14 00:21:57 +00:00
|
|
|
thumbnail_args,
|
|
|
|
input_format,
|
|
|
|
format,
|
2023-07-18 21:18:01 +00:00
|
|
|
quality,
|
2023-07-14 00:21:57 +00:00
|
|
|
)
|
2023-12-23 02:52:58 +00:00
|
|
|
.await?
|
2023-12-23 17:58:20 +00:00
|
|
|
.into_vec()
|
2023-12-23 02:52:58 +00:00
|
|
|
.instrument(tracing::info_span!("Reading processed image to vec"))
|
2023-07-14 00:21:57 +00:00
|
|
|
.await?;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
|
|
|
let bytes = Bytes::from(vec);
|
|
|
|
|
|
|
|
drop(permit);
|
|
|
|
|
2024-02-04 00:18:13 +00:00
|
|
|
let details = Details::from_bytes(state, bytes.clone()).await?;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let identifier = state
|
|
|
|
.store
|
2023-07-14 19:53:37 +00:00
|
|
|
.save_bytes(bytes.clone(), details.media_type())
|
|
|
|
.await?;
|
2023-08-16 20:12:16 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
if let Err(VariantAlreadyExists) = state
|
|
|
|
.repo
|
2023-08-16 20:12:16 +00:00
|
|
|
.relate_variant_identifier(
|
|
|
|
hash,
|
|
|
|
thumbnail_path.to_string_lossy().to_string(),
|
|
|
|
&identifier,
|
|
|
|
)
|
|
|
|
.await?
|
|
|
|
{
|
2024-02-03 19:31:54 +00:00
|
|
|
state.store.remove(&identifier).await?;
|
2023-08-16 20:12:16 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
state.repo.relate_details(&identifier, &details).await?;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
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>
|
|
|
|
}
|
2023-09-24 16:54:16 +00:00
|
|
|
|
2024-02-01 03:32:01 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-09-24 16:54:16 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
async fn input_identifier<S>(
|
2024-02-03 19:31:54 +00:00
|
|
|
state: &State<S>,
|
2023-09-24 16:54:16 +00:00
|
|
|
output_format: InputProcessableFormat,
|
|
|
|
hash: Hash,
|
|
|
|
original_details: &Details,
|
|
|
|
) -> Result<Arc<str>, Error>
|
|
|
|
where
|
|
|
|
S: Store + 'static,
|
|
|
|
{
|
|
|
|
let should_thumbnail =
|
|
|
|
if let Some(input_format) = original_details.internal_format().processable_format() {
|
2023-09-24 18:20:33 +00:00
|
|
|
let output_format = input_format.process_to(output_format);
|
2023-09-24 16:54:16 +00:00
|
|
|
|
|
|
|
input_format.should_thumbnail(output_format)
|
|
|
|
} else {
|
|
|
|
// video case
|
|
|
|
true
|
|
|
|
};
|
|
|
|
|
|
|
|
if should_thumbnail {
|
2024-02-03 19:31:54 +00:00
|
|
|
if let Some(identifier) = state.repo.motion_identifier(hash.clone()).await? {
|
2023-09-24 16:54:16 +00:00
|
|
|
return Ok(identifier);
|
|
|
|
};
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let identifier = state
|
|
|
|
.repo
|
2023-09-24 16:54:16 +00:00
|
|
|
.identifier(hash.clone())
|
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingIdentifier)?;
|
|
|
|
|
|
|
|
let (reader, media_type) = if let Some(processable_format) =
|
|
|
|
original_details.internal_format().processable_format()
|
|
|
|
{
|
2024-02-03 19:31:54 +00:00
|
|
|
let thumbnail_format = state.config.media.image.format.unwrap_or(ImageFormat::Webp);
|
2023-09-24 16:54:16 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let stream = state.store.to_stream(&identifier, None, None).await?;
|
2023-12-23 17:58:20 +00:00
|
|
|
|
2024-02-04 00:18:13 +00:00
|
|
|
let reader =
|
|
|
|
magick::thumbnail(state, stream, processable_format, thumbnail_format).await?;
|
2023-09-24 16:54:16 +00:00
|
|
|
|
|
|
|
(reader, thumbnail_format.media_type())
|
|
|
|
} else {
|
2024-02-03 19:31:54 +00:00
|
|
|
let thumbnail_format = match state.config.media.image.format {
|
2023-09-24 17:37:43 +00:00
|
|
|
Some(ImageFormat::Webp | ImageFormat::Avif | ImageFormat::Jxl) => {
|
|
|
|
ffmpeg::ThumbnailFormat::Webp
|
|
|
|
}
|
|
|
|
Some(ImageFormat::Png) => ffmpeg::ThumbnailFormat::Png,
|
|
|
|
Some(ImageFormat::Jpeg) | None => ffmpeg::ThumbnailFormat::Jpeg,
|
|
|
|
};
|
2023-09-24 16:54:16 +00:00
|
|
|
|
|
|
|
let reader = ffmpeg::thumbnail(
|
2024-02-04 00:18:13 +00:00
|
|
|
&state,
|
2023-09-24 16:54:16 +00:00
|
|
|
identifier,
|
|
|
|
original_details
|
|
|
|
.video_format()
|
|
|
|
.unwrap_or(InternalVideoFormat::Mp4),
|
|
|
|
thumbnail_format,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
(reader, thumbnail_format.media_type())
|
|
|
|
};
|
|
|
|
|
2023-12-23 02:52:58 +00:00
|
|
|
let motion_identifier = reader
|
2024-02-03 19:31:54 +00:00
|
|
|
.with_stdout(|stdout| async { state.store.save_async_read(stdout, media_type).await })
|
2023-12-23 02:52:58 +00:00
|
|
|
.await??;
|
2023-09-24 16:54:16 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
state
|
|
|
|
.repo
|
|
|
|
.relate_motion_identifier(hash, &motion_identifier)
|
2023-09-24 16:54:16 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
return Ok(motion_identifier);
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
state
|
|
|
|
.repo
|
|
|
|
.identifier(hash)
|
2023-09-24 16:54:16 +00:00
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingIdentifier)
|
|
|
|
.map_err(From::from)
|
|
|
|
}
|