diff --git a/app/utils/meta.py b/app/utils/meta.py index f46d0990..a7283d27 100644 --- a/app/utils/meta.py +++ b/app/utils/meta.py @@ -1,5 +1,4 @@ from pathlib import Path -from urllib.parse import unquote import aiohttp from aiocache import cached @@ -7,7 +6,7 @@ from sanic.request import Request from .. import settings -from . import http +from . import http, urls def version() -> str: @@ -45,6 +44,7 @@ async def authenticate(request: Request) -> dict: async def tokenize(request: Request, url: str) -> tuple[str, bool]: api_key = _get_api_key(request) or "" token = request.args.get("token") + url = url.replace("::", ":") # TODO: Fix Sanic bug? default_url = url.replace(f"api_key={api_key}", "").replace("?&", "?").strip("?&") if api_key == "myapikey42" and "example.png" not in url: @@ -58,6 +58,7 @@ async def tokenize(request: Request, url: str) -> tuple[str, bool]: if api_key or token: async with aiohttp.ClientSession() as session: + logger.critical((api, default_url)) response = await session.post( api, data={"url": default_url}, headers={"X-API-KEY": api_key} ) @@ -66,6 +67,7 @@ async def tokenize(request: Request, url: str) -> tuple[str, bool]: return default_url, False data = await response.json() + logger.critical(data) return data["url"], data["url"] != url return url, False @@ -79,7 +81,7 @@ async def custom_watermarks_allowed(request: Request) -> bool: token = request.args.get("token") if token: logger.info(f"Authenticating with token: {token}") - _url, updated = await tokenize(request, request.url) + _url, updated = await tokenize(request, urls.clean(request.url)) return not updated return False @@ -121,7 +123,7 @@ async def track(request: Request, lines: list[str]): return async with aiohttp.ClientSession() as session: - params = dict(text=text, referer=referer, result=unquote(request.url)) + params = dict(text=text, referer=referer, result=urls.clean(request.url)) logger.info(f"Tracking request: {params}") headers = {"X-API-KEY": _get_api_key(request) or ""} status, message = await http.fetch(api, params=params, headers=headers) diff --git a/app/utils/urls.py b/app/utils/urls.py index f6f8a715..70ffee0c 100644 --- a/app/utils/urls.py +++ b/app/utils/urls.py @@ -69,4 +69,7 @@ def clean(url: str) -> str: while "/_." in url: url = url.replace("/_.", ".") + # TODO: Fix Sanic bug? + url = url.replace("::", ":") + return url diff --git a/app/views/images.py b/app/views/images.py index 39470c95..4a36ea12 100644 --- a/app/views/images.py +++ b/app/views/images.py @@ -223,10 +223,8 @@ async def detail_text(request, template_id, text_filepath): ) return response.redirect(utils.urls.clean(url), status=301) - url, updated = await utils.meta.tokenize(request, request.url) + url, updated = await utils.meta.tokenize(request, utils.urls.clean(request.url)) if updated: - logger.critical(f"{url!r}") - assert isinstance(url, str), repr(url) return response.redirect(url, status=302) watermark, updated = await utils.meta.get_watermark(request)