Tweak media proxy error

This commit is contained in:
Thomas Sileo 2022-08-26 22:04:38 +02:00
parent dd4773fc27
commit 40c4a4413d

View file

@ -915,13 +915,23 @@ def _add_cache_control(headers: dict[str, str]) -> dict[str, str]:
@app.get("/proxy/media/{encoded_url}") @app.get("/proxy/media/{encoded_url}")
async def serve_proxy_media(request: Request, encoded_url: str) -> StreamingResponse: async def serve_proxy_media(
request: Request,
encoded_url: str,
) -> StreamingResponse | PlainTextResponse:
# Decode the base64-encoded URL # Decode the base64-encoded URL
url = base64.urlsafe_b64decode(encoded_url).decode() url = base64.urlsafe_b64decode(encoded_url).decode()
check_url(url) check_url(url)
proxy_resp = await _proxy_get(request, url, stream=True) proxy_resp = await _proxy_get(request, url, stream=True)
if proxy_resp.status_code >= 300:
logger.info(f"failed to proxy {url}, got {proxy_resp.status_code}")
return PlainTextResponse(
"proxy error",
status_code=proxy_resp.status_code,
)
return StreamingResponse( return StreamingResponse(
proxy_resp.aiter_raw(), proxy_resp.aiter_raw(),
status_code=proxy_resp.status_code, status_code=proxy_resp.status_code,
@ -966,9 +976,10 @@ async def serve_proxy_media_resized(
) )
proxy_resp = await _proxy_get(request, url, stream=False) proxy_resp = await _proxy_get(request, url, stream=False)
if proxy_resp.status_code != 200: if proxy_resp.status_code >= 300:
logger.info(f"failed to proxy {url}, got {proxy_resp.status_code}")
return PlainTextResponse( return PlainTextResponse(
proxy_resp.content, "proxy error",
status_code=proxy_resp.status_code, status_code=proxy_resp.status_code,
) )