31 lines
1.2 KiB
Python
31 lines
1.2 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, HTTPException, Response, status
|
|
|
|
from app.core.dependencies import DbSession
|
|
from app.services import boxes as boxes_service
|
|
from app.services import objects as objects_service
|
|
from app.services.qrcodes import box_public_url, generate_qr_png, object_public_url
|
|
|
|
router = APIRouter(prefix="/api/v1", tags=["qrcodes"])
|
|
|
|
_PNG_HEADERS = {"Cache-Control": "public, max-age=86400"}
|
|
|
|
|
|
@router.get("/objects/{object_id}/qrcode.png")
|
|
async def object_qrcode(db: DbSession, object_id: uuid.UUID) -> Response:
|
|
obj = await objects_service.get_object(db, object_id)
|
|
if obj is None:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Object not found")
|
|
png = generate_qr_png(object_public_url(obj.id))
|
|
return Response(content=png, media_type="image/png", headers=_PNG_HEADERS)
|
|
|
|
|
|
@router.get("/boxes/{box_id}/qrcode.png")
|
|
async def box_qrcode(db: DbSession, box_id: uuid.UUID) -> Response:
|
|
box = await boxes_service.get_box(db, box_id)
|
|
if box is None:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Box not found")
|
|
png = generate_qr_png(box_public_url(box.id))
|
|
return Response(content=png, media_type="image/png", headers=_PNG_HEADERS)
|