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::{ use rss::{
extension::{dublincore::DublinCoreExtension, ExtensionBuilder, ExtensionMap}, extension::{dublincore::DublinCoreExtension, ExtensionBuilder, ExtensionMap},
Channel, Channel,
EnclosureBuilder,
Guid, Guid,
Item, Item,
}; };
@ -495,7 +496,6 @@ fn create_post_items(
let mut items: Vec<Item> = Vec::new(); let mut items: Vec<Item> = Vec::new();
for p in posts { for p in posts {
// TODO add images
let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id); let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id);
let community_url = format!( let community_url = format!(
"{}/c/{}", "{}/c/{}",
@ -520,12 +520,21 @@ fn create_post_items(
p.counts.comments); p.counts.comments);
// If its a url post, add it to the description // 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>"); let link_html = format!("<br><a href=\"{url}\">{url}</a>");
description.push_str(&link_html); description.push_str(&link_html);
url.to_string()
} else { let mime_type = p
post_url.clone() .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 { if let Some(body) = p.post.body {
@ -558,8 +567,9 @@ fn create_post_items(
guid, guid,
description: Some(sanitize_xml(description)), description: Some(sanitize_xml(description)),
dublin_core_ext, dublin_core_ext,
link, link: Some(post_url.clone()),
extensions, extensions,
enclosure: enclosure_opt,
..Default::default() ..Default::default()
}; };