Fix content negotiation - default to HTML, not raw data
The /cache/{hash} endpoint now defaults to HTML for browsers. Raw
data was being returned for requests without explicit text/html in
Accept header (e.g., link clicks). JSON is only returned when
explicitly requested. Raw data is served only from /cache/{hash}/raw.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
12
server.py
12
server.py
@@ -1991,8 +1991,8 @@ async def get_cached(content_hash: str, request: Request):
|
||||
return HTMLResponse(render_page("Not Found", content, ctx.actor_id if ctx else None, active_tab="media"), status_code=404)
|
||||
raise HTTPException(404, f"Content {content_hash} not in cache")
|
||||
|
||||
# JSON response for API clients
|
||||
if "application/json" in accept:
|
||||
# JSON response only if explicitly requested
|
||||
if "application/json" in accept and "text/html" not in accept:
|
||||
t0 = time.time()
|
||||
meta = await database.load_item_metadata(content_hash, ctx.actor_id if ctx else None)
|
||||
logger.debug(f"get_cached: load_item_metadata took {time.time()-t0:.3f}s")
|
||||
@@ -2018,8 +2018,9 @@ async def get_cached(content_hash: str, request: Request):
|
||||
"meta": meta
|
||||
}
|
||||
|
||||
# HTML response for browsers - show detail page
|
||||
if wants_html(request):
|
||||
# HTML response for browsers (default for all non-JSON requests)
|
||||
# Raw data is only served from /cache/{hash}/raw endpoint
|
||||
if True: # Always show HTML page, raw data via /raw endpoint
|
||||
if not ctx:
|
||||
content = '<p class="text-gray-400 py-8 text-center"><a href="/login" class="text-blue-400 hover:text-blue-300">Login via L2</a> to view cached content.</p>'
|
||||
return HTMLResponse(render_page("Login Required", content, None, active_tab="media"), status_code=401)
|
||||
@@ -2157,9 +2158,6 @@ async def get_cached(content_hash: str, request: Request):
|
||||
|
||||
return HTMLResponse(render_page(f"Cache: {content_hash[:16]}...", content, ctx.actor_id, active_tab="media"))
|
||||
|
||||
# Default: return raw file
|
||||
return FileResponse(cache_path)
|
||||
|
||||
|
||||
@app.get("/cache/{content_hash}/raw")
|
||||
async def get_cached_raw(content_hash: str):
|
||||
|
||||
Reference in New Issue
Block a user