pict-rs/src/discover.rs

87 lines
2.0 KiB
Rust
Raw Normal View History

mod exiftool;
mod ffmpeg;
mod magick;
use actix_web::web::Bytes;
use crate::{
formats::{InputFile, InternalFormat},
store::Store,
};
2023-07-13 19:34:40 +00:00
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Discovery {
pub(crate) input: InputFile,
pub(crate) width: u16,
pub(crate) height: u16,
pub(crate) frames: Option<u32>,
}
2023-07-13 19:34:40 +00:00
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct DiscoveryLite {
pub(crate) format: InternalFormat,
pub(crate) width: u16,
pub(crate) height: u16,
pub(crate) frames: Option<u32>,
}
#[derive(Debug, thiserror::Error)]
pub(crate) enum DiscoverError {
#[error("No frames in uploaded media")]
NoFrames,
#[error("Not all frames have same image format")]
FormatMismatch,
#[error("Input file type {0} is unsupported")]
UnsupportedFileType(String),
}
pub(crate) async fn discover_bytes_lite(
2023-08-05 17:41:06 +00:00
timeout: u64,
bytes: Bytes,
) -> Result<DiscoveryLite, crate::error::Error> {
2023-08-05 17:41:06 +00:00
if let Some(discovery) = ffmpeg::discover_bytes_lite(timeout, bytes.clone()).await? {
return Ok(discovery);
}
2023-08-05 17:41:06 +00:00
let discovery = magick::discover_bytes_lite(timeout, bytes).await?;
Ok(discovery)
}
pub(crate) async fn discover_store_lite<S>(
store: &S,
identifier: &S::Identifier,
2023-08-05 17:41:06 +00:00
timeout: u64,
) -> Result<DiscoveryLite, crate::error::Error>
where
2023-07-17 03:07:42 +00:00
S: Store,
{
if let Some(discovery) =
2023-08-05 17:41:06 +00:00
ffmpeg::discover_stream_lite(timeout, store.to_stream(identifier, None, None).await?)
.await?
{
return Ok(discovery);
}
let discovery =
2023-08-05 17:41:06 +00:00
magick::discover_stream_lite(timeout, store.to_stream(identifier, None, None).await?)
.await?;
Ok(discovery)
}
2023-08-05 17:41:06 +00:00
pub(crate) async fn discover_bytes(
timeout: u64,
bytes: Bytes,
) -> Result<Discovery, crate::error::Error> {
let discovery = ffmpeg::discover_bytes(timeout, bytes.clone()).await?;
2023-08-05 17:41:06 +00:00
let discovery = magick::confirm_bytes(discovery, timeout, bytes.clone()).await?;
2023-08-05 17:41:06 +00:00
let discovery = exiftool::check_reorient(discovery, timeout, bytes).await?;
Ok(discovery)
}