Skip to content

Commit

Permalink
Infer extension from the background image
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Apr 3, 2022
1 parent 8ce3065 commit d95f181
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 6 additions & 0 deletions app/models/template.py
Expand Up @@ -196,10 +196,16 @@ def build_custom_url(
with suppress(IndexError):
for index, text in enumerate(self.text):
text_lines[index] = text.normalize(text_lines[index])
if not extension and background:
url = furl(background)
extension = url.path.segments[-1].split(".")[-1]
if extension not in settings.ALLOWED_EXTENSIONS:
extension = settings.DEFAULT_EXTENSION
if style == "default":
style = ""
elif style == "animated":
extension = "gif"
style = ""
url = request.app.url_for(
"images.detail_text",
template_id="custom" if self.id == "_custom" else self.id,
Expand Down
12 changes: 12 additions & 0 deletions app/tests/test_views_images.py
Expand Up @@ -427,6 +427,18 @@ def it_supports_custom_backgrounds(expect, client, as_json):
"?background=https://www.gstatic.com/webp/gallery/3.png"
}

def it_returns_gif_when_background_is_gif(expect, client):
data = {
"background": "https://media.giphy.com/media/ICOgUNjpvO0PC/giphy.gif",
"text_lines[]": ["foo", "bar"],
}
request, response = client.post("/images/custom", data=data)
expect(response.status) == 201
expect(response.json) == {
"url": "http://localhost:5000/images/custom/foo/bar.gif"
"?background=https://media.giphy.com/media/ICOgUNjpvO0PC/giphy.gif"
}

def it_redirects_if_requested(expect, client):
data = {
"background": "https://www.gstatic.com/webp/gallery/4.png",
Expand Down
5 changes: 0 additions & 5 deletions app/views/helpers.py
Expand Up @@ -46,12 +46,7 @@ async def generate_url(
background = utils.urls.arg(payload, "", "background", "image_url")
extension = utils.urls.arg(payload, "", "extension")

if style == "animated":
extension = "gif"
style = ""

status = 201

if template_id:
template: models.Template = models.Template.objects.get_or_create(template_id)
url = template.build_custom_url(
Expand Down

0 comments on commit d95f181

Please sign in to comment.