Spaces:
Running
Running
v18.11: add /api/proxy/img endpoint for CDN images
Browse files- app_v2_entry.py +15 -0
app_v2_entry.py
CHANGED
|
@@ -644,6 +644,21 @@ async def _sh_slug(slug: str, request: Request, url: str = '', title: str = '',
|
|
| 644 |
<meta http-equiv="refresh" content="0;url={safe_url}">
|
| 645 |
</head><body></body></html>''')
|
| 646 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 647 |
@app.get('/s')
|
| 648 |
async def _sh(url:str='',title:str='',img:str='',post_id:str=''):
|
| 649 |
safe_title = _clean(title) if title else 'VNEWS - Tin tức'
|
|
|
|
| 644 |
<meta http-equiv="refresh" content="0;url={safe_url}">
|
| 645 |
</head><body></body></html>''')
|
| 646 |
|
| 647 |
+
|
| 648 |
+
|
| 649 |
+
@app.get('/api/proxy/img')
|
| 650 |
+
def proxy_img(url: str = Query(default=""), max_size: int = Query(default=1200)):
|
| 651 |
+
"""Proxy image from blocked CDN to public URL."""
|
| 652 |
+
from urllib.parse import unquote
|
| 653 |
+
safe_url = unquote(url)
|
| 654 |
+
import requests as _req
|
| 655 |
+
try:
|
| 656 |
+
r = _req.get(safe_url, timeout=10, headers={"User-Agent": "Mozilla/5.0"})
|
| 657 |
+
if r.status_code == 200 and r.content:
|
| 658 |
+
return Response(r.content, media_type=r.headers.get("content-type", "image/jpeg"))
|
| 659 |
+
except Exception as e:
|
| 660 |
+
pass
|
| 661 |
+
return Response(status_code=502)
|
| 662 |
@app.get('/s')
|
| 663 |
async def _sh(url:str='',title:str='',img:str='',post_id:str=''):
|
| 664 |
safe_title = _clean(title) if title else 'VNEWS - Tin tức'
|