Skip to content

Commit

Permalink
Switch to JPEG-only
Browse files Browse the repository at this point in the history
JPEGs should be good enough and this prevents over-zealous route
matches that killed the CSS.
  • Loading branch information
jacebrowning committed Jun 18, 2015
1 parent 48fdcb6 commit 294a1fb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 54 deletions.
7 changes: 2 additions & 5 deletions memegen/domain/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@


class Image:
"""Meme image generated from a template."""

# TODO: support more image types
KINDS = ('JPG',) # 'PNG', 'GIF')
"""Meme JPEG generated from a template."""

@classmethod
def from_template(cls, template, text, kind):
def from_template(cls, template, text):
make_meme(text.top, text.bottom, template.path)
return "../temp.jpg"

Expand Down
14 changes: 7 additions & 7 deletions memegen/routes/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
blueprint = Blueprint('image', __name__, url_prefix="/")


@blueprint.route("<key>/<path:path>.<kind>")
def get(key, path, kind):
@blueprint.route("<key>/<path:path>.jpg")
def get(key, path):
template = domain.Template(key)
text = domain.Text(path)
if path != text.path:
return redirect(url_for(".get", key=key, path=text.path, kind=kind))
_path = app.image_service.create_image(template, text, kind)
return redirect(url_for(".get", key=key, path=text.path))
_path = app.image_service.create_image(template, text)
return send_file(_path, mimetype='image/jpeg')


@blueprint.route("<code>.<kind>")
def get_encoded(code, kind):
@blueprint.route("<code>.jpg")
def get_encoded(code):
key, path = app.link_service.decode(code)
# TODO: maybe this shouldn't redirect
# url = url_for('.get_visible', key=key, top=top, bottom=bottom, kind=kind)
# return redirect(url)
template = domain.Template(key)
text = domain.Text(path)
_path = app.image_service.create_image(template, text, kind)
_path = app.image_service.create_image(template, text)
return send_file(_path, mimetype='image/jpeg')
17 changes: 6 additions & 11 deletions memegen/routes/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from flask import Blueprint, current_app as app, url_for, redirect

from ..domain import Image, Text
from ..domain import Text


blueprint = Blueprint('links', __name__, url_prefix="/")
Expand All @@ -11,20 +11,15 @@
@blueprint.route("<key>/<path:path>")
def get(**kwargs):
"""Get links for generated images."""
data = OrderedDict()
data['visible'] = OrderedDict()
data['hidden'] = OrderedDict()
text = Text(kwargs['path'])
if kwargs['path'] != text.path:
kwargs['path'] = text.path
return redirect(url_for(".get", **kwargs))
for kind in Image.KINDS:
url = url_for('image.get', kind=kind.lower(), _external=True, **kwargs)
data['visible'][kind] = url
code = app.link_service.encode(**kwargs)
url = url_for('image.get_encoded', kind=kind.lower(), _external=True,
code=code)
data['hidden'][kind] = url

data = OrderedDict()
data['visible'] = url_for('image.get', _external=True, **kwargs)
code = app.link_service.encode(**kwargs)
data['hidden'] = url_for('image.get_encoded', _external=True, code=code)
return data


Expand Down
4 changes: 2 additions & 2 deletions memegen/services/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def find_template(self, template):
raise self.exceptions.not_found
return template

def create_image(self, template, text, kind):
def create_image(self, template, text):
template = self.find_template(template)
image = domain.Image.from_template(template, text, kind)
image = domain.Image.from_template(template, text)
return image
33 changes: 4 additions & 29 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,16 @@ def test_get_links(self, client):
response = client.get("/iw/hello/world")
assert response.status_code == 200
assert dict(
visible=dict(
JPG="http://localhost/iw/hello/world.jpg",
),
hidden=dict(
JPG="http://localhost/aXcJaGVsbG8vd29ybGQJ.jpg",
),
visible="http://localhost/iw/hello/world.jpg",
hidden="http://localhost/aXcJaGVsbG8vd29ybGQJ.jpg",
) == load(response)

def test_get_links_with_1_line(self, client):
response = client.get("/iw/hello")
assert response.status_code == 200
assert dict(
visible=dict(
JPG="http://localhost/iw/hello.jpg",
),
hidden=dict(
JPG="http://localhost/aXcJaGVsbG8J.jpg",
),
visible="http://localhost/iw/hello.jpg",
hidden="http://localhost/aXcJaGVsbG8J.jpg",
) == load(response)

def test_get_links_redirects_to_dashes(self, client):
Expand Down Expand Up @@ -83,23 +75,6 @@ def test_get_visible_jpg_1_line(self, client):
assert response.status_code == 200
assert response.mimetype == 'image/jpeg'

# TODO: add more image types

# def test_get_visible_jpeg(self, client):
# response = client.get("/iw/hello/world.jpeg")
# assert response.status_code == 200
# assert response.mimetype == 'image/jpeg'

# def test_get_visible_png(self, client):
# response = client.get("/iw/hello/world.png")
# assert response.status_code == 200
# assert response.mimetype == 'image/png'

# def test_get_visible_gif(self, client):
# response = client.get("/iw/hello/world.gif")
# assert response.status_code == 200
# assert response.mimetype == 'image/gif'

def test_get_hidden_jpg(self, client):
response = client.get("/aXcJaGVsbG8vd29ybGQJ.jpg")
assert response.status_code == 200
Expand Down

0 comments on commit 294a1fb

Please sign in to comment.