fix: burn file items after download, not metadata fetch
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
from fastapi import APIRouter, Depends, File, Form, HTTPException, Query, Request, UploadFile
|
||||
from fastapi.responses import FileResponse
|
||||
from sqlalchemy.orm import Session
|
||||
from starlette.background import BackgroundTask
|
||||
|
||||
from app import db as db_module
|
||||
from app.db import get_db
|
||||
from app.models import Item
|
||||
from app.schemas import ItemCreate, ItemOut, TTLOption, WallResponse
|
||||
from app.services import items as items_service
|
||||
from app.services import storage as storage_service
|
||||
@@ -72,6 +75,16 @@ def wall(
|
||||
)
|
||||
|
||||
|
||||
def _burn_file_after_response(item_id: int) -> None:
|
||||
db = db_module.SessionLocal()
|
||||
try:
|
||||
item = db.get(Item, item_id)
|
||||
if item is not None and item.burn_after_read and not item.burned:
|
||||
items_service.burn_file_item(db, item)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.get("/{slug}/file")
|
||||
def download_file(slug: str, db: Session = Depends(get_db)):
|
||||
item = items_service.get_file_item(db, slug)
|
||||
@@ -84,11 +97,15 @@ def download_file(slug: str, db: Session = Depends(get_db)):
|
||||
media_type = item.mime or "application/octet-stream"
|
||||
filename = item.file_name or path.name
|
||||
inline = media_type.startswith(_INLINE_IMAGE_PREFIXES)
|
||||
background = None
|
||||
if item.burn_after_read:
|
||||
background = BackgroundTask(_burn_file_after_response, item.id)
|
||||
return FileResponse(
|
||||
path,
|
||||
media_type=media_type,
|
||||
filename=filename,
|
||||
content_disposition_type="inline" if inline else "attachment",
|
||||
background=background,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user