from pathlib import Path from fastapi import APIRouter, Depends, File, Form, Query, Response, UploadFile, status from fastapi.responses import FileResponse from sqlalchemy.orm import Session from ..core.config import settings from ..core.db import get_db from ..core.deps import get_current_user from ..core.exceptions import CodedHTTPException from ..models import Attachment, User from ..schemas.attachments import AttachmentListOut, AttachmentOut from ..services.attachment_service import ( assert_entity_visible, delete_attachment, save_attachment, ) router = APIRouter(prefix="/attachments", tags=["attachments"]) def _to_out(att: Attachment, user_names: dict[int, str]) -> AttachmentOut: return AttachmentOut( id=att.id, entity_type=att.entity_type, entity_id=att.entity_id, original_name=att.original_name, mime_type=att.mime_type, size_bytes=att.size_bytes, uploaded_by_id=att.uploaded_by_id, uploaded_by_name=user_names.get(att.uploaded_by_id), uploaded_at=att.uploaded_at, ) @router.post("", response_model=AttachmentOut, status_code=status.HTTP_201_CREATED) def upload_attachment( entity_type: str = Form(...), entity_id: int = Form(...), file: UploadFile = File(...), db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): att = save_attachment(db, current_user, entity_type, entity_id, file) return _to_out(att, {current_user.id: current_user.name}) @router.get("", response_model=AttachmentListOut) def list_attachments( entity_type: str = Query(...), entity_id: int = Query(...), db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): assert_entity_visible(db, current_user, entity_type, entity_id) rows = ( db.query(Attachment) .filter(Attachment.entity_type == entity_type, Attachment.entity_id == entity_id) .order_by(Attachment.uploaded_at.desc(), Attachment.id.desc()) .all() ) user_ids = {r.uploaded_by_id for r in rows} user_names = {} if user_ids: user_names = { u.id: u.name for u in db.query(User.id, User.name).filter(User.id.in_(user_ids)).all() } return { "items": [_to_out(r, user_names) for r in rows], "total": len(rows), } @router.get("/{attachment_id}/download") def download_attachment( attachment_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): att = db.query(Attachment).filter(Attachment.id == attachment_id).first() if att is None: raise CodedHTTPException(status_code=404, detail="Attachment not found.", code="NOT_FOUND") assert_entity_visible(db, current_user, att.entity_type, att.entity_id) file_path = Path(settings.UPLOAD_DIR) / att.stored_name if not file_path.is_file(): raise CodedHTTPException(status_code=404, detail="Attachment file missing on disk.", code="FILE_NOT_FOUND") return FileResponse( path=file_path, media_type=att.mime_type, filename=att.original_name, ) @router.delete("/{attachment_id}", status_code=status.HTTP_204_NO_CONTENT) def remove_attachment( attachment_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): delete_attachment(db, current_user, attachment_id) return Response(status_code=status.HTTP_204_NO_CONTENT)