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},
|
|
|
|
repo::{ArcRepo, Hash, VariantAlreadyExists},
|
2023-09-02 23:30:45 +00:00
|
|
|
store::Store,
|
2022-04-02 22:40:04 +00:00
|
|
|
};
|
|
|
|
use actix_web::web::Bytes;
|
2023-09-24 16:54:16 +00:00
|
|
|
use std::{path::PathBuf, sync::Arc, 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());
|
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)]
|
2023-07-23 00:41:50 +00:00
|
|
|
#[tracing::instrument(skip(repo, store, hash, process_map, media))]
|
2023-08-16 00:19:03 +00:00
|
|
|
pub(crate) async fn generate<S: Store + 'static>(
|
|
|
|
repo: &ArcRepo,
|
2022-04-02 22:40:04 +00:00
|
|
|
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
|
|
|
thumbnail_path: PathBuf,
|
|
|
|
thumbnail_args: Vec<String>,
|
2023-09-24 16:54:16 +00:00
|
|
|
original_details: &Details,
|
2023-07-22 17:31:01 +00:00
|
|
|
media: &crate::config::Media,
|
2023-08-14 19:25:19 +00:00
|
|
|
hash: Hash,
|
2022-04-02 22:40:04 +00:00
|
|
|
) -> Result<(Details, Bytes), Error> {
|
|
|
|
let process_fut = process(
|
|
|
|
repo,
|
|
|
|
store,
|
|
|
|
format,
|
|
|
|
thumbnail_path.clone(),
|
|
|
|
thumbnail_args,
|
2023-09-24 16:54:16 +00:00
|
|
|
original_details,
|
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
|
2023-08-14 19:25:19 +00:00
|
|
|
.process(hash, thumbnail_path, process_fut)
|
2023-07-22 16:15:30 +00:00
|
|
|
.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)]
|
2023-07-23 00:41:50 +00:00
|
|
|
#[tracing::instrument(skip(repo, store, hash, media))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn process<S: Store + 'static>(
|
|
|
|
repo: &ArcRepo,
|
2022-04-02 22:40:04 +00:00
|
|
|
store: &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-07-22 17:31:01 +00:00
|
|
|
media: &crate::config::Media,
|
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();
|
2022-04-07 17:56:40 +00:00
|
|
|
let permit = crate::PROCESS_SEMAPHORE.acquire().await;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
2023-09-24 16:54:16 +00:00
|
|
|
let identifier = input_identifier(
|
|
|
|
repo,
|
|
|
|
store,
|
|
|
|
output_format,
|
|
|
|
hash.clone(),
|
|
|
|
original_details,
|
|
|
|
media,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-04-02 22:40:04 +00:00
|
|
|
|
2023-07-13 18:48:59 +00:00
|
|
|
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?;
|
2023-07-13 18:48:59 +00:00
|
|
|
|
|
|
|
repo.relate_details(&identifier, &details).await?;
|
|
|
|
|
|
|
|
details
|
|
|
|
};
|
|
|
|
|
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 16:54:16 +00:00
|
|
|
let format = input_format
|
|
|
|
.process_to(output_format)
|
|
|
|
.ok_or(UploadError::InvalidProcessExtension)?;
|
2023-07-13 18:48:59 +00:00
|
|
|
|
2023-07-18 21:18:01 +00:00
|
|
|
let quality = match format {
|
2023-09-24 16:54:16 +00:00
|
|
|
ProcessableFormat::Image(format) => media.image.quality_for(format),
|
|
|
|
ProcessableFormat::Animation(format) => media.animation.quality_for(format),
|
2023-07-18 21:18:01 +00:00
|
|
|
};
|
|
|
|
|
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-08-05 17:41:06 +00:00
|
|
|
media.process_timeout,
|
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-08-05 17:41:06 +00:00
|
|
|
let details = Details::from_bytes(media.process_timeout, 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?;
|
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?;
|
|
|
|
}
|
|
|
|
|
2022-04-02 22:40:04 +00:00
|
|
|
repo.relate_details(&identifier, &details).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>
|
|
|
|
}
|
2023-09-24 16:54:16 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
async fn input_identifier<S>(
|
|
|
|
repo: &ArcRepo,
|
|
|
|
store: &S,
|
|
|
|
output_format: InputProcessableFormat,
|
|
|
|
hash: Hash,
|
|
|
|
original_details: &Details,
|
|
|
|
media: &crate::config::Media,
|
|
|
|
) -> Result<Arc<str>, Error>
|
|
|
|
where
|
|
|
|
S: Store + 'static,
|
|
|
|
{
|
|
|
|
let should_thumbnail =
|
|
|
|
if let Some(input_format) = original_details.internal_format().processable_format() {
|
|
|
|
let output_format = input_format
|
|
|
|
.process_to(output_format)
|
|
|
|
.ok_or(UploadError::InvalidProcessExtension)?;
|
|
|
|
|
|
|
|
input_format.should_thumbnail(output_format)
|
|
|
|
} else {
|
|
|
|
// video case
|
|
|
|
true
|
|
|
|
};
|
|
|
|
|
|
|
|
if should_thumbnail {
|
|
|
|
if let Some(identifier) = repo.motion_identifier(hash.clone()).await? {
|
|
|
|
return Ok(identifier);
|
|
|
|
};
|
|
|
|
|
|
|
|
let identifier = repo
|
|
|
|
.identifier(hash.clone())
|
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingIdentifier)?;
|
|
|
|
|
|
|
|
let (reader, media_type) = if let Some(processable_format) =
|
|
|
|
original_details.internal_format().processable_format()
|
|
|
|
{
|
|
|
|
let thumbnail_format = ImageFormat::Jpeg;
|
|
|
|
|
|
|
|
let reader = magick::thumbnail(
|
|
|
|
store,
|
|
|
|
&identifier,
|
|
|
|
processable_format,
|
|
|
|
ProcessableFormat::Image(thumbnail_format),
|
|
|
|
media.image.quality_for(thumbnail_format),
|
|
|
|
media.process_timeout,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
(reader, thumbnail_format.media_type())
|
|
|
|
} else {
|
|
|
|
let thumbnail_format = ffmpeg::ThumbnailFormat::Jpeg;
|
|
|
|
|
|
|
|
let reader = ffmpeg::thumbnail(
|
|
|
|
store.clone(),
|
|
|
|
identifier,
|
|
|
|
original_details
|
|
|
|
.video_format()
|
|
|
|
.unwrap_or(InternalVideoFormat::Mp4),
|
|
|
|
thumbnail_format,
|
|
|
|
media.process_timeout,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
(reader, thumbnail_format.media_type())
|
|
|
|
};
|
|
|
|
|
|
|
|
let motion_identifier = store.save_async_read(reader, media_type).await?;
|
|
|
|
|
|
|
|
repo.relate_motion_identifier(hash, &motion_identifier)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
return Ok(motion_identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.identifier(hash)
|
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingIdentifier)
|
|
|
|
.map_err(From::from)
|
|
|
|
}
|