From d95f181fea79455387f8dc96ea3a19858018b9b8 Mon Sep 17 00:00:00 2001 From: Jace Browning Date: Sun, 3 Apr 2022 17:19:48 -0400 Subject: [PATCH] Infer extension from the background image --- app/models/template.py | 6 ++++++ app/tests/test_views_images.py | 12 ++++++++++++ app/views/helpers.py | 5 ----- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/models/template.py b/app/models/template.py index bcbc0117..0c315abc 100644 --- a/app/models/template.py +++ b/app/models/template.py @@ -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, diff --git a/app/tests/test_views_images.py b/app/tests/test_views_images.py index ced78edf..e7eeb252 100644 --- a/app/tests/test_views_images.py +++ b/app/tests/test_views_images.py @@ -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", diff --git a/app/views/helpers.py b/app/views/helpers.py index be59a262..52af9bd0 100644 --- a/app/views/helpers.py +++ b/app/views/helpers.py @@ -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(