This repository has been archived by the owner on Dec 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Creates image proxy view (closes #104). * Passes .env to environment when creating locally. * Use image proxy URLs for served images.
- Loading branch information
1 parent
ed50148
commit 3cdd926
Showing
16 changed files
with
228 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/usr/bin/env bash | ||
|
||
exec uwsgi --http :${PORT:-8000} --wsgi-file /app/recommendation/wsgi.py --master | ||
exec uwsgi --wsgi-disable-file-wrapper --http :${PORT:-8000} --wsgi-file /app/recommendation/wsgi.py --master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from urllib.parse import parse_qs, quote, urlparse | ||
|
||
from flask import current_app | ||
from nose.tools import eq_ | ||
|
||
from recommendation.tests.util import AppTestCase | ||
from recommendation.util import image_url | ||
|
||
|
||
DIMENSION = '64' | ||
IMAGE = 'https://foo.bar/image.jpg' | ||
EMBEDLY_BASE = 'https://i.embed.ly/' | ||
EMBEDLY_IMAGE = '{}?url={}'.format(EMBEDLY_BASE, quote(IMAGE)) | ||
|
||
|
||
class TestImageUrl(AppTestCase): | ||
def _image_url(self, url, **kwargs): | ||
with current_app.app_context(): | ||
url = image_url(url, **kwargs) | ||
parsed = urlparse(url) if url else None | ||
qs = parse_qs(parsed.query) if parsed else None | ||
return url, parsed, qs | ||
|
||
def test_none(self): | ||
url, parsed, qs = self._image_url(None) | ||
eq_(url, None) | ||
|
||
def test_formed(self): | ||
url, parsed, qs = self._image_url(IMAGE, width=DIMENSION, | ||
height=DIMENSION) | ||
eq_(IMAGE, qs['url'][0]) | ||
eq_(DIMENSION, qs['width'][0]) | ||
eq_(DIMENSION, qs['height'][0]) | ||
|
||
def test_embedly(self): | ||
url = self._image_url(IMAGE) | ||
embedly_url = self._image_url(EMBEDLY_IMAGE) | ||
eq_(url, embedly_url) | ||
|
||
def test_embedly_no_url(self): | ||
url, parsed, qs = self._image_url(EMBEDLY_BASE) | ||
eq_(qs['url'][0], EMBEDLY_BASE) | ||
|
||
def test_embedly_empty_url(self): | ||
URL = '{}?url='.format(EMBEDLY_BASE) | ||
url, parsed, qs = self._image_url(URL) | ||
eq_(qs['url'][0], URL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from urllib.parse import parse_qs, urlparse | ||
|
||
from flask import current_app, url_for | ||
|
||
|
||
def image_url(url, **kwargs): | ||
if not url: | ||
return | ||
kwargs['url'] = url | ||
parsed = urlparse(url) | ||
|
||
# If the image is already being proxied by Embedly, pull the `url` | ||
# querystring param out and use that instead to prevent double-billing. | ||
if parsed.netloc == 'i.embed.ly': | ||
qs = parse_qs(parsed.query) | ||
try: | ||
kwargs['url'] = qs['url'][0] | ||
except (IndexError, KeyError): | ||
pass | ||
|
||
with current_app.app_context(): | ||
return url_for('images.proxy', **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from urllib.parse import urlencode | ||
|
||
import requests | ||
from flask import abort, Blueprint, request, Response, stream_with_context | ||
|
||
from recommendation import conf | ||
|
||
EMBEDLY_RESIZE = 'https://i.embed.ly/1/display/resize' | ||
|
||
|
||
images = Blueprint('images', __name__) | ||
|
||
|
||
def make_embedly_url(url, **kwargs): | ||
""" | ||
Passed the URL to an image, returns a string to the Embedly resize URL for | ||
that image. Accepts optional `width` and `height` keyword arguments. | ||
""" | ||
qs = {} | ||
for param in ['width', 'height']: | ||
if param in kwargs: | ||
qs[param] = kwargs[param][0] | ||
qs['animate'] = 'false' | ||
qs['compresspng'] = 'true' | ||
qs['key'] = conf.EMBEDLY_API_KEY | ||
return '{}?{}'.format(EMBEDLY_RESIZE, urlencode(qs)) | ||
|
||
|
||
@images.route('/images') | ||
def proxy(): | ||
try: | ||
url = make_embedly_url(**request.args) | ||
except TypeError: | ||
abort(400) | ||
try: | ||
req = requests.get(url, stream=True, timeout=10) | ||
except requests.RequestException: | ||
abort(400) | ||
if req.status_code != 200: | ||
abort(400) | ||
response = Response(stream_with_context(req.iter_content()), | ||
content_type=req.headers['content-type']) | ||
response.headers['Cache-Control'] = 'max-age=%d' % conf.IMAGEPROXY_TTL | ||
return response |
Oops, something went wrong.