Support imgproxy

This commit is contained in:
magmaus3 2022-11-05 12:57:46 +01:00
parent 0d6b8e55bc
commit 0b68a1c4b4
Signed by: magmaus3
GPG key ID: 966755D3F4A9B251
5 changed files with 45 additions and 2 deletions

View file

@ -20,10 +20,13 @@ template_env = jinja2.Environment(
) )
template_env.globals['convert_times'] = utils.convert_times template_env.globals['convert_times'] = utils.convert_times
template_env.globals['generate_thumbnail_url'] = utils.generate_thumbnail_url
template_env.globals['__version__'] = __version__ template_env.globals['__version__'] = __version__
BASE_URL = "http://make.fangam.es" BASE_URL = "http://make.fangam.es"
THUMB_URL = "https://images.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. # Matches level code.
# \S[A-Z0-9]{4}\-[A-Z0-9]{4} = With dash, no whitespace # \S[A-Z0-9]{4}\-[A-Z0-9]{4} = With dash, no whitespace

View file

@ -61,7 +61,7 @@
<div class="searchResults"> <div class="searchResults">
{% for map in searchResults %} {% for map in searchResults %}
<div class="searchResultCard"> <div class="searchResultCard">
<div class="thumbnail"><img src="{{ THUMB_URL }}/{{ map['ID'] }}.png"></div> <div class="thumbnail"><img src="{{ generate_thumbnail_url(map['ID'], THUMB_URL) }}"></div>
<div class="details"> <div class="details">
<div class="basic"> <div class="basic">
<div class="levelTitle"><a href="/level/{{ map['ID'] }}">{{ map['Name'] }}</a></div> <div class="levelTitle"><a href="/level/{{ map['ID'] }}">{{ map['Name'] }}</a></div>

View file

@ -1,3 +1,5 @@
from imgproxy import ImgProxy
def convert_times(time): def convert_times(time):
# Not sure if it's the right way to do that, but it seems to work. # Not sure if it's the right way to do that, but it seems to work.
time_seconds = ((time % 60000) / 100) * 2 time_seconds = ((time % 60000) / 100) * 2
@ -11,3 +13,24 @@ def convert_times(time):
seconds = "{0:,.2f}".format(seconds).zfill(5) seconds = "{0:,.2f}".format(seconds).zfill(5)
return (hours,minutes,seconds) 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

18
poetry.lock generated
View file

@ -243,6 +243,18 @@ category = "main"
optional = false optional = false
python-versions = ">=3.5" 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]] [[package]]
name = "jinja2" name = "jinja2"
version = "3.1.2" version = "3.1.2"
@ -404,7 +416,7 @@ multidict = ">=4.0"
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.8" python-versions = "^3.8"
content-hash = "d1ec124a926e92c08231493ede23464812559355dba393164fc3b8a8c070fa0d" content-hash = "ac9c7bff4d98478ad09db04382b38def6391b752362ca0948fc4a5b1a282e72d"
[metadata.files] [metadata.files]
aiodns = [] aiodns = []
@ -611,6 +623,10 @@ idna = [
{file = "idna-3.4-py3-none-any.whl", hash = "sha256:90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2"}, {file = "idna-3.4-py3-none-any.whl", hash = "sha256:90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2"},
{file = "idna-3.4.tar.gz", hash = "sha256:814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4"}, {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 = [ jinja2 = [
{file = "Jinja2-3.1.2-py3-none-any.whl", hash = "sha256:6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61"}, {file = "Jinja2-3.1.2-py3-none-any.whl", hash = "sha256:6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61"},
{file = "Jinja2-3.1.2.tar.gz", hash = "sha256:31351a702a408a9e7595a8fc6150fc3f43bb6bf7e319770cbc0db9df9437e852"}, {file = "Jinja2-3.1.2.tar.gz", hash = "sha256:31351a702a408a9e7595a8fc6150fc3f43bb6bf7e319770cbc0db9df9437e852"},

View file

@ -15,6 +15,7 @@ uvicorn = "^0.19.0"
aiohttp = {extras = ["speedups"], version = "^3.8.3"} aiohttp = {extras = ["speedups"], version = "^3.8.3"}
httpx = "^0.23.0" httpx = "^0.23.0"
fastapi = "^0.85.1" fastapi = "^0.85.1"
imgproxy = "^1.0.0"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]