2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-10 06:25:00 +00:00
pict-rs/src/details.rs

124 lines
3.4 KiB
Rust
Raw Normal View History

use crate::{
discover::DiscoveryLite,
error::Error,
formats::{InternalFormat, InternalVideoFormat},
serde_str::Serde,
store::Store,
};
2022-03-24 22:09:15 +00:00
use actix_web::web;
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
2022-03-24 22:09:15 +00:00
#[derive(Copy, Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(untagged)]
pub(crate) enum MaybeHumanDate {
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 {
width: u16,
height: u16,
frames: Option<u32>,
2022-03-24 22:09:15 +00:00
content_type: Serde<mime::Mime>,
created_at: MaybeHumanDate,
#[serde(skip_serializing_if = "Option::is_none")]
format: Option<InternalFormat>,
2022-03-24 22:09:15 +00:00
}
impl Details {
pub(crate) fn is_video(&self) -> bool {
2022-03-24 22:09:15 +00:00
self.content_type.type_() == "video"
}
pub(crate) async fn from_bytes(input: web::Bytes) -> Result<Self, Error> {
let DiscoveryLite {
format,
width,
height,
frames,
} = crate::discover::discover_bytes_lite(input).await?;
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>(
store: &S,
identifier: &S::Identifier,
) -> Result<Self, Error> {
let DiscoveryLite {
format,
2022-03-24 22:09:15 +00:00
width,
height,
2022-09-25 22:36:07 +00:00
frames,
} = crate::discover::discover_store_lite(store, identifier).await?;
Ok(Details::from_parts(format, width, height, frames))
}
pub(crate) fn internal_format(&self) -> Option<InternalFormat> {
if let Some(format) = self.format {
return Some(format);
2022-03-24 22:09:15 +00:00
}
InternalFormat::maybe_from_media_type(&self.content_type, self.frames.is_some())
2022-03-24 22:09:15 +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()
}
pub(crate) fn video_format(&self) -> Option<InternalVideoFormat> {
if *self.content_type == crate::formats::mimes::video_mp4() {
return Some(InternalVideoFormat::Mp4);
}
if *self.content_type == crate::formats::mimes::video_webm() {
return Some(InternalVideoFormat::Webm);
}
None
}
pub(crate) fn from_parts(
format: InternalFormat,
width: u16,
height: u16,
frames: Option<u32>,
) -> Self {
Self {
width,
height,
frames,
content_type: Serde::new(format.media_type()),
created_at: MaybeHumanDate::HumanDate(OffsetDateTime::now_utc()),
format: Some(format),
}
}
2022-03-24 22:09:15 +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(),
}
}
}
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)
}
}
}
}