From 0b68a1c4b4e9933f8be4baf14335f2925482e74e Mon Sep 17 00:00:00 2001 From: magmaus3 Date: Sat, 5 Nov 2022 12:57:46 +0100 Subject: [PATCH] Support imgproxy --- iwm_browser/main.py | 3 +++ iwm_browser/templates/search.html | 2 +- iwm_browser/utils.py | 23 +++++++++++++++++++++++ poetry.lock | 18 +++++++++++++++++- pyproject.toml | 1 + 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/iwm_browser/main.py b/iwm_browser/main.py index b845436..409ec8a 100644 --- a/iwm_browser/main.py +++ b/iwm_browser/main.py @@ -20,10 +20,13 @@ template_env = jinja2.Environment( ) template_env.globals['convert_times'] = utils.convert_times +template_env.globals['generate_thumbnail_url'] = utils.generate_thumbnail_url template_env.globals['__version__'] = __version__ BASE_URL = "http://make.fangam.es" THUMB_URL = "https://images.make.fangam.es" +utils.global_imgproxy_url = "http://127.0.0.1:8080" # Set it to None to disable the proxy +utils.global_imgproxy_params = {"extension": "webp", "advanced": ["q:50"]} # Set it to None to disable the proxy params # Matches level code. # \S[A-Z0-9]{4}\-[A-Z0-9]{4} = With dash, no whitespace diff --git a/iwm_browser/templates/search.html b/iwm_browser/templates/search.html index 6a49c34..14a0068 100644 --- a/iwm_browser/templates/search.html +++ b/iwm_browser/templates/search.html @@ -61,7 +61,7 @@
{% for map in searchResults %}
-
+
diff --git a/iwm_browser/utils.py b/iwm_browser/utils.py index a1f4cfe..f3dc1a4 100644 --- a/iwm_browser/utils.py +++ b/iwm_browser/utils.py @@ -1,3 +1,5 @@ +from imgproxy import ImgProxy + def convert_times(time): # Not sure if it's the right way to do that, but it seems to work. time_seconds = ((time % 60000) / 100) * 2 @@ -11,3 +13,24 @@ def convert_times(time): seconds = "{0:,.2f}".format(seconds).zfill(5) return (hours,minutes,seconds) + +# Specify default imgproxy instance. +global_imgproxy_url = None +global_imgproxy_params = None +def generate_thumbnail_url(level_id, thumbnail_url, imgproxy_url=None, imgproxy_params={}): + """Generates thumbnail url.""" + global global_imgproxy_url + global global_imgproxy_params + if imgproxy_url is None and global_imgproxy_url is not None: + imgproxy_url = global_imgproxy_url + if imgproxy_params == {} and global_imgproxy_params is not None: + imgproxy_params = global_imgproxy_params + + # Create url + img_url = thumbnail_url + "/" + str(level_id) + ".png" + + if imgproxy_url is not None: + img_url = ImgProxy(img_url, proxy_host=imgproxy_url, **imgproxy_params) + img_url = str(img_url) + + return img_url diff --git a/poetry.lock b/poetry.lock index fa0f50e..6580280 100644 --- a/poetry.lock +++ b/poetry.lock @@ -243,6 +243,18 @@ category = "main" optional = false python-versions = ">=3.5" +[[package]] +name = "imgproxy" +version = "1.0.0" +description = "Python support for ImgProxy" +category = "main" +optional = false +python-versions = ">=3.7" + +[package.extras] +build = ["bump2version", "twine", "wheel"] +tests = ["pytest", "pytest-mypy"] + [[package]] name = "jinja2" version = "3.1.2" @@ -404,7 +416,7 @@ multidict = ">=4.0" [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "d1ec124a926e92c08231493ede23464812559355dba393164fc3b8a8c070fa0d" +content-hash = "ac9c7bff4d98478ad09db04382b38def6391b752362ca0948fc4a5b1a282e72d" [metadata.files] aiodns = [] @@ -611,6 +623,10 @@ idna = [ {file = "idna-3.4-py3-none-any.whl", hash = "sha256:90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2"}, {file = "idna-3.4.tar.gz", hash = "sha256:814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4"}, ] +imgproxy = [ + {file = "imgproxy-1.0.0-py3-none-any.whl", hash = "sha256:18d3686c1b8f976906192e7bbdcd44dca305600e3e338fab68c30ee9a4102ded"}, + {file = "imgproxy-1.0.0.tar.gz", hash = "sha256:f2e34b0da688284ad3a1e7ccd907b1038285001832362fb64cd2bd39c6c952e3"}, +] jinja2 = [ {file = "Jinja2-3.1.2-py3-none-any.whl", hash = "sha256:6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61"}, {file = "Jinja2-3.1.2.tar.gz", hash = "sha256:31351a702a408a9e7595a8fc6150fc3f43bb6bf7e319770cbc0db9df9437e852"}, diff --git a/pyproject.toml b/pyproject.toml index 0ac0c00..10b18bd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,7 @@ uvicorn = "^0.19.0" aiohttp = {extras = ["speedups"], version = "^3.8.3"} httpx = "^0.23.0" fastapi = "^0.85.1" +imgproxy = "^1.0.0" [tool.poetry.dev-dependencies]