support image proxying using atmos/camo
This commit is contained in:
parent
02c32428d5
commit
c2e4683479
3 changed files with 32 additions and 6 deletions
|
@ -2,7 +2,7 @@
|
||||||
<article class="reply-context">
|
<article class="reply-context">
|
||||||
<header>
|
<header>
|
||||||
{% if context.author_photo %}
|
{% if context.author_photo %}
|
||||||
<img src="{{context.author_photo}}"/>
|
<img src="{{context.author_photo|proxy_image}}"/>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if context.author_name %}
|
{% if context.author_name %}
|
||||||
{{ context.author_name }} -
|
{{ context.author_name }} -
|
||||||
|
@ -14,7 +14,7 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if context.content %}
|
{% if context.content %}
|
||||||
<div class="content">
|
<div class="content">
|
||||||
{{ context.content_cleaned | add_preview }}
|
{{ context.content_cleaned | proxy_all | add_preview }}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<footer>
|
<footer>
|
||||||
|
@ -30,7 +30,7 @@
|
||||||
<article>
|
<article>
|
||||||
<header>
|
<header>
|
||||||
{% if entry.author_photo %}
|
{% if entry.author_photo %}
|
||||||
<img src="{{entry.author_photo}}"/>
|
<img src="{{entry.author_photo|proxy_image}}"/>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if entry.author_name %}
|
{% if entry.author_name %}
|
||||||
{{ entry.author_name }} -
|
{{ entry.author_name }} -
|
||||||
|
@ -47,7 +47,7 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if entry.content %}
|
{% if entry.content %}
|
||||||
<div class="content">
|
<div class="content">
|
||||||
{{ entry.content_cleaned | add_preview }}
|
{{ entry.content_cleaned | proxy_all | add_preview }}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,6 @@
|
||||||
{% endblock header %}
|
{% endblock header %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
|
|
||||||
<div class="button-link">
|
<div class="button-link">
|
||||||
<a id="unfold-link" href="#">n New Entries</a>
|
<a id="unfold-link" href="#">n New Entries</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -7,6 +7,8 @@ import bs4
|
||||||
import datetime
|
import datetime
|
||||||
import feedparser
|
import feedparser
|
||||||
import flask
|
import flask
|
||||||
|
import hashlib
|
||||||
|
import hmac
|
||||||
import mf2py
|
import mf2py
|
||||||
import mf2util
|
import mf2util
|
||||||
import requests
|
import requests
|
||||||
|
@ -15,6 +17,9 @@ import urllib
|
||||||
import cgi
|
import cgi
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
|
|
||||||
|
IMAGE_TAG_RE = re.compile(r'<img([^>]*) src="(https?://[^">]+)"')
|
||||||
|
|
||||||
|
|
||||||
views = flask.Blueprint('views', __name__)
|
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,
|
"""If a post ends with the URL of a known media source (youtube,
|
||||||
instagram, etc.), add the content inline.
|
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')):
|
'img', 'iframe', 'embed', 'audio', 'video')):
|
||||||
# don't add a preview to a post that already has one
|
# don't add a preview to a post that already has one
|
||||||
return content
|
return content
|
||||||
|
@ -600,6 +605,28 @@ def add_preview(content):
|
||||||
return 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('&', '&')
|
||||||
|
return '<img{} src="{}"'.format(attrs, proxy_image(url))
|
||||||
|
if content:
|
||||||
|
return IMAGE_TAG_RE.sub(repl, content)
|
||||||
|
|
||||||
|
|
||||||
@views.app_template_global()
|
@views.app_template_global()
|
||||||
def url_for_other_page(page):
|
def url_for_other_page(page):
|
||||||
"""http://flask.pocoo.org/snippets/44/#URL+Generation+Helper
|
"""http://flask.pocoo.org/snippets/44/#URL+Generation+Helper
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue