2022-10-01 00:38:11 +00:00
|
|
|
use crate::{
|
2023-07-13 18:48:59 +00:00
|
|
|
discover::DiscoveryLite,
|
2022-10-01 00:38:11 +00:00
|
|
|
error::Error,
|
2023-07-13 03:12:21 +00:00
|
|
|
formats::{InternalFormat, InternalVideoFormat},
|
2022-10-01 00:38:11 +00:00
|
|
|
serde_str::Serde,
|
|
|
|
store::Store,
|
|
|
|
};
|
2022-03-24 22:09:15 +00:00
|
|
|
use actix_web::web;
|
2023-07-13 03:12:21 +00:00
|
|
|
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
|
2022-03-24 22:09:15 +00:00
|
|
|
|
2022-04-08 18:36:06 +00:00
|
|
|
#[derive(Copy, Clone, Debug, serde::Deserialize, serde::Serialize)]
|
|
|
|
#[serde(untagged)]
|
2023-07-08 22:35:57 +00:00
|
|
|
pub(crate) enum MaybeHumanDate {
|
2022-04-08 18:36:06 +00:00
|
|
|
HumanDate(#[serde(with = "time::serde::rfc3339")] time::OffsetDateTime),
|
|
|
|
OldDate(#[serde(serialize_with = "time::serde::rfc3339::serialize")] time::OffsetDateTime),
|
|
|
|
}
|
|
|
|
|
2022-03-24 22:09:15 +00:00
|
|
|
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
|
|
|
pub(crate) struct Details {
|
2023-07-13 18:48:59 +00:00
|
|
|
width: u16,
|
|
|
|
height: u16,
|
|
|
|
frames: Option<u32>,
|
2022-03-24 22:09:15 +00:00
|
|
|
content_type: Serde<mime::Mime>,
|
2022-04-08 18:36:06 +00:00
|
|
|
created_at: MaybeHumanDate,
|
2023-08-16 17:36:18 +00:00
|
|
|
format: InternalFormat,
|
2022-03-24 22:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Details {
|
2023-07-13 18:48:59 +00:00
|
|
|
pub(crate) fn is_video(&self) -> bool {
|
2022-03-24 22:09:15 +00:00
|
|
|
self.content_type.type_() == "video"
|
|
|
|
}
|
|
|
|
|
2023-08-28 23:43:24 +00:00
|
|
|
pub(crate) fn created_at(&self) -> time::OffsetDateTime {
|
|
|
|
match self.created_at {
|
|
|
|
MaybeHumanDate::OldDate(timestamp) => timestamp,
|
|
|
|
MaybeHumanDate::HumanDate(timestamp) => timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-05 17:41:06 +00:00
|
|
|
pub(crate) async fn from_bytes(timeout: u64, input: web::Bytes) -> Result<Self, Error> {
|
2023-07-13 18:48:59 +00:00
|
|
|
let DiscoveryLite {
|
|
|
|
format,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
frames,
|
2023-08-05 17:41:06 +00:00
|
|
|
} = crate::discover::discover_bytes_lite(timeout, input).await?;
|
2023-07-13 18:48:59 +00:00
|
|
|
|
|
|
|
Ok(Details::from_parts(format, width, height, frames))
|
2022-03-24 22:09:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 03:07:42 +00:00
|
|
|
pub(crate) async fn from_store<S: Store>(
|
2023-07-13 18:48:59 +00:00
|
|
|
store: &S,
|
|
|
|
identifier: &S::Identifier,
|
2023-08-05 17:41:06 +00:00
|
|
|
timeout: u64,
|
2022-03-27 01:45:12 +00:00
|
|
|
) -> Result<Self, Error> {
|
2023-07-13 18:48:59 +00:00
|
|
|
let DiscoveryLite {
|
|
|
|
format,
|
2022-03-24 22:09:15 +00:00
|
|
|
width,
|
|
|
|
height,
|
2022-09-25 22:36:07 +00:00
|
|
|
frames,
|
2023-08-05 17:41:06 +00:00
|
|
|
} = crate::discover::discover_store_lite(store, identifier, timeout).await?;
|
2023-07-13 18:48:59 +00:00
|
|
|
|
|
|
|
Ok(Details::from_parts(format, width, height, frames))
|
|
|
|
}
|
|
|
|
|
2023-08-16 17:36:18 +00:00
|
|
|
pub(crate) fn internal_format(&self) -> InternalFormat {
|
|
|
|
self.format
|
2022-03-24 22:09:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 19:53:37 +00:00
|
|
|
pub(crate) fn media_type(&self) -> mime::Mime {
|
2022-03-24 22:09:15 +00:00
|
|
|
(*self.content_type).clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn system_time(&self) -> std::time::SystemTime {
|
|
|
|
self.created_at.into()
|
|
|
|
}
|
2022-10-01 00:38:11 +00:00
|
|
|
|
2023-07-13 18:48:59 +00:00
|
|
|
pub(crate) fn video_format(&self) -> Option<InternalVideoFormat> {
|
2023-08-16 17:36:18 +00:00
|
|
|
match self.format {
|
|
|
|
InternalFormat::Video(format) => Some(format),
|
|
|
|
_ => None,
|
2022-10-01 00:38:11 +00:00
|
|
|
}
|
2023-08-16 17:36:18 +00:00
|
|
|
}
|
2022-10-01 00:38:11 +00:00
|
|
|
|
2023-08-16 17:36:18 +00:00
|
|
|
pub(crate) fn from_parts_full(
|
|
|
|
format: InternalFormat,
|
|
|
|
width: u16,
|
|
|
|
height: u16,
|
|
|
|
frames: Option<u32>,
|
|
|
|
created_at: MaybeHumanDate,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
frames,
|
|
|
|
content_type: Serde::new(format.media_type()),
|
|
|
|
created_at,
|
|
|
|
format,
|
2022-10-01 00:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-13 03:12:21 +00:00
|
|
|
|
|
|
|
pub(crate) fn from_parts(
|
|
|
|
format: InternalFormat,
|
|
|
|
width: u16,
|
|
|
|
height: u16,
|
|
|
|
frames: Option<u32>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
2023-07-13 18:48:59 +00:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
frames,
|
2023-07-13 03:12:21 +00:00
|
|
|
content_type: Serde::new(format.media_type()),
|
|
|
|
created_at: MaybeHumanDate::HumanDate(OffsetDateTime::now_utc()),
|
2023-08-16 17:36:18 +00:00
|
|
|
format,
|
2023-07-13 03:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-24 22:09:15 +00:00
|
|
|
}
|
2022-04-08 18:36:06 +00:00
|
|
|
|
|
|
|
impl From<MaybeHumanDate> for std::time::SystemTime {
|
|
|
|
fn from(this: MaybeHumanDate) -> Self {
|
|
|
|
match this {
|
|
|
|
MaybeHumanDate::OldDate(old) => old.into(),
|
|
|
|
MaybeHumanDate::HumanDate(human) => human.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-08 22:35:57 +00:00
|
|
|
|
|
|
|
impl std::fmt::Display for MaybeHumanDate {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::OldDate(date) | Self::HumanDate(date) => {
|
|
|
|
let s = date.format(&Rfc3339).map_err(|_| std::fmt::Error)?;
|
|
|
|
|
|
|
|
f.write_str(&s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|