Spaces:
Sleeping
Sleeping
Download proxy.py from tonyliu404/Manglify_Backend: direct link, hf CLI and curl.
- Browser
- Download file 1.64 kB
-
https://huggingface.co/spaces/tonyliu404/Manglify_Backend/resolve/main/proxy.py
- Command line
-
hf download hf://spaces/tonyliu404/Manglify_Backend/proxy.py
-
curl -L -o proxy.py https://huggingface.co/spaces/tonyliu404/Manglify_Backend/resolve/main/proxy.py
1.64 kB
| import httpx | |
| from fastapi.responses import StreamingResponse | |
| from fastapi import HTTPException | |
| client = httpx.AsyncClient(timeout=10.0) | |
| # async def get_manga_page_stream(target_url: str): | |
| # try: | |
| # stream = client.stream("GET", target_url) | |
| # response = await stream.__aenter__() | |
| # except httpx.RequestError: | |
| # raise HTTPException(status_code=502, detail="Upstream request failed") | |
| # if response.status_code != 200: | |
| # await stream.__aexit__(None, None, None) | |
| # raise HTTPException(status_code=response.status_code, detail="Image not found") | |
| # async def generator(): | |
| # try: | |
| # async for chunk in response.aiter_bytes(): | |
| # yield chunk | |
| # finally: | |
| # await stream.__aexit__(None, None, None) # ensure cleanup | |
| # return StreamingResponse( | |
| # generator(), | |
| # media_type=response.headers.get("Content-Type", "image/jpeg"), | |
| # ) | |
| async def get_manga_page_stream(target_url: str): | |
| req = client.build_request("GET", target_url) | |
| try: | |
| response = await client.send(req, stream=True) | |
| except httpx.RequestError: | |
| raise HTTPException(status_code=502, detail="Upstream unreachable") | |
| if response.status_code != 200: | |
| await response.aclose() # Must close if we aren't streaming | |
| raise HTTPException(status_code=response.status_code, detail="MangaDex error") | |
| return StreamingResponse( | |
| response.aiter_bytes(), | |
| media_type=response.headers.get("Content-Type", "image/jpeg"), | |
| background=response.aclose, # Ensures connection closes after stream finish | |
| ) |