support image proxying using atmos/camo

This commit is contained in:
Kyle Mahan 2015-09-24 15:44:22 +00:00
parent 02c32428d5
commit c2e4683479
3 changed files with 32 additions and 6 deletions

View file

@ -2,7 +2,7 @@
<article class="reply-context">
<header>
{% if context.author_photo %}
<img src="{{context.author_photo}}"/>
<img src="{{context.author_photo|proxy_image}}"/>
{% endif %}
{% if context.author_name %}
{{ context.author_name }} -
@ -14,7 +14,7 @@
{% endif %}
{% if context.content %}
<div class="content">
{{ context.content_cleaned | add_preview }}
{{ context.content_cleaned | proxy_all | add_preview }}
</div>
{% endif %}
<footer>
@ -30,7 +30,7 @@
<article>
<header>
{% if entry.author_photo %}
<img src="{{entry.author_photo}}"/>
<img src="{{entry.author_photo|proxy_image}}"/>
{% endif %}
{% if entry.author_name %}
{{ entry.author_name }} -
@ -47,7 +47,7 @@
{% endif %}
{% if entry.content %}
<div class="content">
{{ entry.content_cleaned | add_preview }}
{{ entry.content_cleaned | proxy_all | add_preview }}
</div>
{% endif %}

View file

@ -31,7 +31,6 @@
{% endblock header %}
{% block body %}
<div class="button-link">
<a id="unfold-link" href="#">n New Entries</a>
</div>

View file

@ -7,6 +7,8 @@ import bs4
import datetime
import feedparser
import flask
import hashlib
import hmac
import mf2py
import mf2util
import requests
@ -15,6 +17,9 @@ import urllib
import cgi
import sqlalchemy
IMAGE_TAG_RE = re.compile(r'<img([^>]*) src="(https?://[^">]+)"')
views = flask.Blueprint('views', __name__)
@ -558,7 +563,7 @@ def add_preview(content):
"""If a post ends with the URL of a known media source (youtube,
instagram, etc.), add the content inline.
"""
if any('<' + tag in content for tag in (
if not content or any('<' + tag in content for tag in (
'img', 'iframe', 'embed', 'audio', 'video')):
# don't add a preview to a post that already has one
return content
@ -599,6 +604,28 @@ def add_preview(content):
return content
@views.app_template_filter()
def proxy_image(url):
camo_url = flask.current_app.config.get('CAMO_URL')
camo_key = flask.current_app.config.get('CAMO_KEY')
if not camo_url or not camo_key:
return url
digest = hmac.new(camo_key.encode(), url.encode(), hashlib.sha1).hexdigest()
return (urllib.parse.urljoin(camo_url, digest)
+ '?url=' + urllib.parse.quote_plus(url))
@views.app_template_filter()
def proxy_all(content):
def repl(m):
attrs = m.group(1)
url = m.group(2)
url = url.replace('&amp;', '&')
return '<img{} src="{}"'.format(attrs, proxy_image(url))
if content:
return IMAGE_TAG_RE.sub(repl, content)
@views.app_template_global()
def url_for_other_page(page):