Merge branch '16-add-aspectratio-to-image-handling' into 'main'

: copy aspect ratio for images & video

Closes #16

See merge request veretcle/oolatoocs!40
This commit is contained in:
VC
2025-12-01 15:48:36 +00:00
4 changed files with 121 additions and 7 deletions

2
Cargo.lock generated
View File

@@ -1926,7 +1926,7 @@ checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]] [[package]]
name = "oolatoocs" name = "oolatoocs"
version = "4.4.2" version = "4.5.0"
dependencies = [ dependencies = [
"atrium-api", "atrium-api",
"bsky-sdk", "bsky-sdk",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "oolatoocs" name = "oolatoocs"
version = "4.4.2" version = "4.5.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -1,4 +1,4 @@
use crate::{config::BlueskyConfig, OolatoocsError}; use crate::{config::BlueskyConfig, utils::convert_aspect_ratio, OolatoocsError};
use atrium_api::{ use atrium_api::{
app::bsky::feed::post::RecordData, com::atproto::repo::upload_blob::Output, app::bsky::feed::post::RecordData, com::atproto::repo::upload_blob::Output,
types::string::Datetime, types::string::Language, types::string::RecordKey, types::string::Datetime, types::string::Language, types::string::RecordKey,
@@ -227,7 +227,9 @@ pub async fn generate_media_records(
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedVideoMain(Box::new( atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedVideoMain(Box::new(
atrium_api::app::bsky::embed::video::MainData { atrium_api::app::bsky::embed::video::MainData {
alt: media.description.clone(), alt: media.description.clone(),
aspect_ratio: None, aspect_ratio: convert_aspect_ratio(
&media.meta.as_ref().and_then(|m| m.original.clone()),
),
captions: None, captions: None,
video: blob.data.blob, video: blob.data.blob,
} }
@@ -248,7 +250,9 @@ pub async fn generate_media_records(
.description .description
.clone() .clone()
.map_or("".to_string(), |v| v.to_owned()), .map_or("".to_string(), |v| v.to_owned()),
aspect_ratio: None, aspect_ratio: convert_aspect_ratio(
&media.meta.as_ref().and_then(|m| m.original.clone()),
),
image: i.data.blob, image: i.data.blob,
} }
}) })

View File

@@ -1,7 +1,8 @@
use atrium_api::{app::bsky::embed::defs::AspectRatioData, types::Object};
use html_escape::decode_html_entities; use html_escape::decode_html_entities;
use megalodon::entities::status::Tag; use megalodon::entities::{attachment::MetaSub, status::Tag};
use regex::Regex; use regex::Regex;
use std::error::Error; use std::{error::Error, num::NonZeroU64};
/// Generate 2 contents out of 1 if that content is > 300 chars, None else /// Generate 2 contents out of 1 if that content is > 300 chars, None else
pub fn generate_multi_tweets(content: &str) -> Option<(String, String)> { pub fn generate_multi_tweets(content: &str) -> Option<(String, String)> {
@@ -110,10 +111,119 @@ fn strip_html_tags(input: &str) -> String {
data data
} }
pub fn convert_aspect_ratio(m: &Option<MetaSub>) -> Option<Object<AspectRatioData>> {
match m {
Some(ms) => {
if ms.height.is_some_and(|x| x > 0) && ms.width.is_some_and(|x| x > 0) {
Some(
AspectRatioData {
// unwrap is safe here
height: NonZeroU64::new(ms.height.unwrap().into()).unwrap(),
width: NonZeroU64::new(ms.width.unwrap().into()).unwrap(),
}
.into(),
)
} else {
None
}
}
None => None,
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn test_convert_aspect_ratio() {
// test None orig aspect ratio
let metasub: Option<MetaSub> = None;
let result = convert_aspect_ratio(&metasub);
assert_eq!(result, None);
// test complet with image
let metasub = Some(MetaSub {
width: Some(1920),
height: Some(1080),
size: Some(String::from("1920x1080")),
aspect: Some(1.7777777777777777),
frame_rate: None,
duration: None,
bitrate: None,
});
let expected_result = Some(
AspectRatioData {
height: NonZeroU64::new(1080).unwrap(),
width: NonZeroU64::new(1920).unwrap(),
}
.into(),
);
let result = convert_aspect_ratio(&metasub);
assert_eq!(result, expected_result);
// test complete with video
let metasub = Some(MetaSub {
width: Some(500),
height: Some(278),
size: None,
aspect: None,
frame_rate: Some(String::from("10/1")),
duration: Some(0.9),
bitrate: Some(973191),
});
let expected_result = Some(
AspectRatioData {
height: NonZeroU64::new(278).unwrap(),
width: NonZeroU64::new(500).unwrap(),
}
.into(),
);
let result = convert_aspect_ratio(&metasub);
assert_eq!(result, expected_result);
/* test broken shit
* that should never happened but you never know
*/
// zero width
let metasub = Some(MetaSub {
width: Some(0),
height: Some(278),
size: None,
aspect: None,
frame_rate: Some(String::from("10/1")),
duration: Some(0.9),
bitrate: Some(973191),
});
let result = convert_aspect_ratio(&metasub);
assert_eq!(result, None);
// None height
let metasub = Some(MetaSub {
width: Some(500),
height: None,
size: None,
aspect: None,
frame_rate: Some(String::from("10/1")),
duration: Some(0.9),
bitrate: Some(973191),
});
let result = convert_aspect_ratio(&metasub);
assert_eq!(result, None);
}
#[test] #[test]
fn test_twitter_count() { fn test_twitter_count() {
let content = "tamerelol?! 🐵"; let content = "tamerelol?! 🐵";