microblog.pub/app/media.py

50 lines
1.3 KiB
Python
Raw Normal View History

2022-06-25 06:23:28 +00:00
import base64
import time
2022-06-25 06:23:28 +00:00
from app.config import BASE_URL
from app.config import hmac_sha256
2022-06-25 06:23:28 +00:00
SUPPORTED_RESIZE = [50, 740]
EXPIRY_PERIOD = 86400
EXPIRY_LENGTH = 7
class InvalidProxySignatureError(Exception):
pass
def proxied_media_sig(expires: int, url: str) -> str:
hm = hmac_sha256()
2022-11-06 15:57:04 +00:00
hm.update(f"{expires}".encode())
hm.update(b"|")
hm.update(url.encode())
return base64.urlsafe_b64encode(hm.digest()).decode()
def verify_proxied_media_sig(expires: int, url: str, sig: str) -> None:
now = int(time.time() / EXPIRY_PERIOD)
expected = proxied_media_sig(expires, url)
if now > expires or sig != expected:
raise InvalidProxySignatureError("invalid or expired media")
2022-06-25 06:23:28 +00:00
def proxied_media_url(url: str) -> str:
if url.startswith(BASE_URL):
return url
expires = int(time.time() / EXPIRY_PERIOD) + EXPIRY_LENGTH
sig = proxied_media_sig(expires, url)
2022-06-25 06:23:28 +00:00
2022-11-06 15:57:04 +00:00
return (
BASE_URL
+ f"/proxy/media/{expires}/{sig}/"
+ base64.urlsafe_b64encode(url.encode()).decode()
)
2022-06-25 06:23:28 +00:00
def resized_media_url(url: str, size: int) -> str:
if size not in SUPPORTED_RESIZE:
raise ValueError(f"Unsupported resize {size}")
if url.startswith(BASE_URL):
return url
return proxied_media_url(url) + f"/{size}"