From 8da4cce68a631d888fb4e4774f98bf351f01e644 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Wed, 28 Feb 2024 12:55:30 +1300 Subject: [PATCH] rss feeds with images in them --- app/community/routes.py | 6 +++++- app/topic/routes.py | 6 +++++- app/utils.py | 8 ++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/community/routes.py b/app/community/routes.py index 28dfb2f1..b77cf25b 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -22,7 +22,7 @@ from app.community import bp from app.utils import get_setting, render_template, allowlist_html, markdown_to_html, validation_required, \ shorten_string, gibberish, community_membership, ap_datetime, \ request_etag_matches, return_304, instance_banned, can_create_post, can_upvote, can_downvote, user_filters_posts, \ - joined_communities, moderating_communities, blocked_domains + joined_communities, moderating_communities, blocked_domains, mimetype_from_url from feedgen.feed import FeedGenerator from datetime import timezone, timedelta @@ -233,6 +233,10 @@ def show_community_rss(actor): fe = fg.add_entry() fe.title(post.title) fe.link(href=f"https://{current_app.config['SERVER_NAME']}/post/{post.id}") + if post.url: + type = mimetype_from_url(post.url) + if type and not type.startswith('text/'): + fe.enclosure(post.url, type=type) fe.description(post.body_html) fe.guid(post.profile_id(), permalink=True) fe.author(name=post.author.user_name) diff --git a/app/topic/routes.py b/app/topic/routes.py index cb494b89..0cb366ac 100644 --- a/app/topic/routes.py +++ b/app/topic/routes.py @@ -15,7 +15,7 @@ from app.topic import bp from app import db, celery, cache from app.topic.forms import ChooseTopicsForm from app.utils import render_template, user_filters_posts, moderating_communities, joined_communities, \ - community_membership, blocked_domains, validation_required + community_membership, blocked_domains, validation_required, mimetype_from_url @bp.route('/topic/', methods=['GET']) @@ -113,6 +113,10 @@ def show_topic_rss(topic_name): fe = fg.add_entry() fe.title(post.title) fe.link(href=f"https://{current_app.config['SERVER_NAME']}/post/{post.id}") + if post.url: + type = mimetype_from_url(post.url) + if type and not type.startswith('text/'): + fe.enclosure(post.url, type=type) fe.description(post.body_html) fe.guid(post.profile_id(), permalink=True) fe.author(name=post.author.user_name) diff --git a/app/utils.py b/app/utils.py index eb763ded..d721503f 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1,6 +1,7 @@ from __future__ import annotations import hashlib +import mimetypes import random import urllib from collections import defaultdict @@ -327,6 +328,13 @@ def ensure_directory_exists(directory): rebuild_directory += '/' +def mimetype_from_url(url): + parsed_url = urlparse(url) + path = parsed_url.path.split('?')[0] # Strip off anything after '?' + mime_type, _ = mimetypes.guess_type(path) + return mime_type + + def validation_required(func): @wraps(func) def decorated_view(*args, **kwargs):