Add support for RSS media enclosures in feeds (#4442)

* Add support for RSS media enclosures in feeds

* Use post.url_content_type
This commit is contained in:
Lcchy 2024-02-13 09:46:46 +00:00 committed by GitHub
parent 677d54ae57
commit 8a6a86c1bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -29,6 +29,7 @@ use once_cell::sync::Lazy;
use rss::{
extension::{dublincore::DublinCoreExtension, ExtensionBuilder, ExtensionMap},
Channel,
EnclosureBuilder,
Guid,
Item,
};
@ -495,7 +496,6 @@ fn create_post_items(
let mut items: Vec<Item> = Vec::new();
for p in posts {
// TODO add images
let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id);
let community_url = format!(
"{}/c/{}",
@ -520,12 +520,21 @@ fn create_post_items(
p.counts.comments);
// If its a url post, add it to the description
let link = Some(if let Some(url) = p.post.url {
// and see if we can parse it as a media enclosure.
let enclosure_opt = p.post.url.map(|url| {
let link_html = format!("<br><a href=\"{url}\">{url}</a>");
description.push_str(&link_html);
url.to_string()
} else {
post_url.clone()
let mime_type = p
.post
.url_content_type
.unwrap_or_else(|| "application/octet-stream".to_string());
let mut enclosure_bld = EnclosureBuilder::default();
enclosure_bld.url(url.as_str().to_string());
enclosure_bld.mime_type(mime_type);
enclosure_bld.length("0".to_string());
enclosure_bld.build()
});
if let Some(body) = p.post.body {
@ -558,8 +567,9 @@ fn create_post_items(
guid,
description: Some(sanitize_xml(description)),
dublin_core_ext,
link,
link: Some(post_url.clone()),
extensions,
enclosure: enclosure_opt,
..Default::default()
};