Skip to content

Commit

Permalink
wip: fix urls
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Apr 1, 2023
1 parent 11c1d44 commit d3a18d4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
10 changes: 6 additions & 4 deletions app/utils/meta.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from pathlib import Path
from urllib.parse import unquote

import aiohttp
from aiocache import cached
from sanic.log import logger
from sanic.request import Request

from .. import settings
from . import http
from . import http, urls


def version() -> str:
Expand Down Expand Up @@ -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:
Expand All @@ -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}
)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions app/utils/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ def clean(url: str) -> str:
while "/_." in url:
url = url.replace("/_.", ".")

# TODO: Fix Sanic bug?
url = url.replace("::", ":")

return url
4 changes: 1 addition & 3 deletions app/views/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d3a18d4

Please sign in to comment.