forked from jmug/stoatchat
feat(services/january): image/video embeds
This commit is contained in:
@@ -122,6 +122,17 @@ pub fn image_size(f: &NamedTempFile) -> Option<(usize, usize)> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine size of image with buffer
|
||||
pub fn image_size_vec(v: &[u8]) -> Option<(usize, usize)> {
|
||||
if let Ok(size) = imagesize::blob_size(v)
|
||||
.inspect_err(|err| tracing::error!("Failed to generate image size! {err:?}"))
|
||||
{
|
||||
Some((size.width, size.height))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine size of video at temp file
|
||||
pub fn video_size(f: &NamedTempFile) -> Option<(i64, i64)> {
|
||||
if let Ok(data) = ffprobe::ffprobe(f.path())
|
||||
|
||||
@@ -14,9 +14,9 @@ auto_derived!(
|
||||
/// URL to the original image
|
||||
pub url: String,
|
||||
/// Width of the image
|
||||
pub width: isize,
|
||||
pub width: usize,
|
||||
/// Height of the image
|
||||
pub height: isize,
|
||||
pub height: usize,
|
||||
/// Positioning and size
|
||||
pub size: ImageSize,
|
||||
}
|
||||
@@ -26,9 +26,9 @@ auto_derived!(
|
||||
/// URL to the original video
|
||||
pub url: String,
|
||||
/// Width of the video
|
||||
pub width: isize,
|
||||
pub width: usize,
|
||||
/// Height of the video
|
||||
pub height: isize,
|
||||
pub height: usize,
|
||||
}
|
||||
|
||||
/// Type of remote Twitch content
|
||||
@@ -86,7 +86,7 @@ auto_derived!(
|
||||
},
|
||||
AppleMusic {
|
||||
album_id: String,
|
||||
|
||||
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
track_id: Option<String>,
|
||||
},
|
||||
|
||||
@@ -91,7 +91,7 @@ async fn proxy(Query(UrlQuery { url }): Query<UrlQuery>) -> Result<impl IntoResp
|
||||
)]
|
||||
async fn embed(
|
||||
Query(UrlQuery { url }): Query<UrlQuery>,
|
||||
TypedHeader(Authorization(_bearer)): TypedHeader<Authorization<Bearer>>,
|
||||
// TypedHeader(Authorization(_bearer)): TypedHeader<Authorization<Bearer>>,
|
||||
) -> Result<Json<Embed>> {
|
||||
Request::generate_embed(&url).await.map(Json)
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ use lazy_static::lazy_static;
|
||||
use mime::Mime;
|
||||
use reqwest::{header::CONTENT_TYPE, redirect, Client, Response};
|
||||
use revolt_config::report_internal_error;
|
||||
use revolt_files::{create_thumbnail, decode_image, is_valid_image, video_size};
|
||||
use revolt_models::v0::Embed;
|
||||
use revolt_files::{create_thumbnail, decode_image, image_size_vec, is_valid_image, video_size};
|
||||
use revolt_models::v0::{Embed, Image, ImageSize, Video};
|
||||
use revolt_result::{create_error, Result};
|
||||
|
||||
lazy_static! {
|
||||
@@ -79,6 +79,7 @@ impl Request {
|
||||
} else {
|
||||
let mut file = report_internal_error!(tempfile::NamedTempFile::new())?;
|
||||
report_internal_error!(file.write_all(&bytes))?;
|
||||
|
||||
if video_size(&file).is_some() {
|
||||
Ok((mime.to_string(), bytes.to_vec()))
|
||||
} else {
|
||||
@@ -92,8 +93,7 @@ impl Request {
|
||||
PROXY_CACHE.insert(url.to_owned(), result.clone()).await;
|
||||
result
|
||||
} else {
|
||||
// Err(create_error!())
|
||||
todo!() // no proxy
|
||||
Err(create_error!(LabelMe))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,7 +103,52 @@ impl Request {
|
||||
if let Some(hit) = EMBED_CACHE.get(url).await {
|
||||
hit
|
||||
} else {
|
||||
todo!()
|
||||
let Request { response, mime } = Request::new(url).await?;
|
||||
|
||||
match (mime.type_(), mime.subtype()) {
|
||||
(_, mime::HTML) => {
|
||||
// let mut metadata = Metadata::from(resp, url).await?;
|
||||
// metadata.resolve_external().await;
|
||||
|
||||
// if metadata.is_none() {
|
||||
// return Ok(Embed::None);
|
||||
// }
|
||||
|
||||
// Ok(Embed::Website(metadata))
|
||||
todo!()
|
||||
}
|
||||
(mime::IMAGE, _) => {
|
||||
if let Some((width, height)) =
|
||||
image_size_vec(&report_internal_error!(response.bytes().await)?)
|
||||
{
|
||||
Ok(Embed::Image(Image {
|
||||
url: url.to_owned(),
|
||||
width,
|
||||
height,
|
||||
size: ImageSize::Large,
|
||||
}))
|
||||
} else {
|
||||
Ok(Embed::None)
|
||||
}
|
||||
}
|
||||
(mime::VIDEO, _) => {
|
||||
let mut file = report_internal_error!(tempfile::NamedTempFile::new())?;
|
||||
report_internal_error!(
|
||||
file.write_all(&report_internal_error!(response.bytes().await)?)
|
||||
)?;
|
||||
|
||||
if let Some((width, height)) = video_size(&file) {
|
||||
Ok(Embed::Video(Video {
|
||||
url: url.to_owned(),
|
||||
width: width as usize,
|
||||
height: height as usize,
|
||||
}))
|
||||
} else {
|
||||
Ok(Embed::None)
|
||||
}
|
||||
}
|
||||
_ => Ok(Embed::None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user