Files
DeepTutor/deeptutor/api/routers/attachments.py
Qiu 506cea9d4f feat(desktop): finish Windows chat file actions (#3051)
Continues #2978 and closes #2887. Includes the original Windows file actions, integrates the changes with dev, and adds an explicit default-app action.

Co-authored-by: Botong Wu <2048400180@qq.com>
2026-11-28 13:09:15 -03:00

82 lines
2.9 KiB
Python

"""HTTP endpoint for chat attachment downloads / previews.
The chat turn runtime persists every uploaded attachment to the
:class:`~deeptutor.services.storage.AttachmentStore` and records the public
URL on the message. The frontend preview drawer loads files via this
router, which only serves paths the store hands back — every component is
sanitised to defend against directory traversal.
URL shape::
GET /files/attachments/{session_id}/{attachment_id}/{filename}
The session id functions as the ACL boundary, mirroring how the rest of
the app treats sessions today (single-tenant, session ownership is local
trust). Once multi-user auth lands we should swap this for signed URLs.
"""
from __future__ import annotations
import logging
from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import FileResponse
from deeptutor.api.desktop_files import browser_file_media_type
from deeptutor.api.utils.http_headers import content_disposition
from deeptutor.services.storage import (
LocalDiskAttachmentStore,
get_attachment_store,
)
logger = logging.getLogger(__name__)
router = APIRouter()
_content_disposition = content_disposition
@router.get("/{session_id}/{attachment_id}/{filename:path}")
@router.head("/{session_id}/{attachment_id}/{filename:path}", include_in_schema=False)
async def get_attachment(
session_id: str,
attachment_id: str,
filename: str,
request: Request,
):
"""Serve a previously uploaded chat attachment.
Responds with ``Content-Disposition: inline`` so browsers preview PDFs
and images directly in an ``<iframe>`` / ``<img>``. For unknown types
the browser still falls back to download, which is fine for the
drawer's "Download" button path.
"""
store = get_attachment_store()
if not isinstance(store, LocalDiskAttachmentStore):
# Future remote backends should issue a redirect to the signed URL
# here. Local-disk is the only backend today, so this branch just
# guards against an unexpected configuration.
raise HTTPException(status_code=501, detail="Attachment backend not servable")
target = store.resolve_path(
session_id=session_id,
attachment_id=attachment_id,
filename=filename,
)
if target is None:
raise HTTPException(status_code=404, detail="Attachment not found")
media_type = browser_file_media_type(request, target.name)
# ``inline`` lets the browser preview the file when possible while still
# honouring the suggested filename for the drawer's download action.
headers = {
"Content-Disposition": _content_disposition(target.name),
# User-uploaded data; do not let intermediaries cache it.
"Cache-Control": "private, max-age=0, must-revalidate",
}
return FileResponse(
path=str(target), media_type=media_type or "application/octet-stream", headers=headers
)