Coverage for app / router / app / favicons / router.py: 100%
15 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
1from fastapi import APIRouter, Depends, HTTPException, status
3from app.db.postgres.models import User
4from app.dependency.user.context import get_current_user
5from app.services import favicons as favicon_service
6from app.util.untrusted_image import untrusted_image_response
8favicon_router = APIRouter(prefix="/favicons", tags=["App - Site icons"])
11@favicon_router.get("/{domain}")
12def site_icon(domain: str, current_user: User = Depends(get_current_user)):
13 """A cited site's icon, fetched and cached by the platform.
15 Replaces loading icons from a third-party service, which told that
16 service every domain a user opened a citation for. 404 when the site has
17 no usable icon — the client shows a generic one.
18 """
19 normalised = favicon_service.normalise_domain(domain)
20 if normalised is None:
21 raise HTTPException(
22 status_code=status.HTTP_404_NOT_FOUND, detail="Not a public host name."
23 )
24 icon = favicon_service.get_icon(normalised)
25 if icon is None:
26 raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="No icon.")
27 # Third-party bytes, verified to be a raster image; the server-side cache
28 # holds an icon for a week, so a day in the browser is plenty.
29 return untrusted_image_response(icon.data, icon.content_type)