Files
mtusi2026lab4-fork/app/attachments.py
T

114 lines
5.5 KiB
Python

"""Binary upload, private download, and durable upload intents."""
import asyncio
import hashlib
import tempfile
import uuid
import unicodedata
from urllib.parse import quote
from fastapi import Depends, Request
from fastapi.responses import StreamingResponse
from starlette.concurrency import run_in_threadpool
import storage
# Two decoded images per replica at most; no unbounded waiting queue.
slots = asyncio.Semaphore(2)
def valid_filename(name):
return (isinstance(name, str) and 1 <= len(name) <= 255 and bool(name.strip())
and not any(c in '/\\' or unicodedata.category(c) == 'Cc' for c in name))
def install(app, pool, current_user, visible_chat, query, wire, ApiError):
def public(row):
row = wire(row)
row.pop('object_key', None)
return dict(row, variants=[], error_code=None)
def visible_attachment(aid, actor):
try:
aid = str(uuid.UUID(aid))
except ValueError:
raise ApiError(400, 'invalid_request', 'Invalid attachment id')
rows = query("""SELECT a.* FROM attachments a JOIN members m ON m.chat_id=a.chat_id
WHERE a.id=%s AND m.user_id=%s AND a.status='ready'""", (aid, actor['id']))
if not rows:
raise ApiError(404, 'not_found', 'Attachment not found')
return rows[0]
def save(file, chat_id, actor, name, mime, size, digest):
try:
storage.validate_image(file, mime)
except storage.TooManyPixels:
raise ApiError(413, 'payload_too_large', 'Too many image pixels')
except storage.InvalidImage:
raise ApiError(415, 'unsupported_media_type', 'Invalid PNG/JPEG or MIME mismatch')
aid = str(uuid.uuid4())
key = 'originals/' + aid
# Commit intent before PUT. Crash/uncertain PUT or COMMIT is recoverable.
query("""INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,status)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,'pending') RETURNING id""",
(aid,chat_id,actor['id'],name,key,mime,size,digest))
with pool.connection() as conn:
# Cleaner skips this row while PUT and commit are in flight.
conn.execute('SELECT id FROM attachments WHERE id=%s FOR UPDATE', (aid,)).fetchone()
storage.client.put_object(Bucket=storage.BUCKET, Key=key, Body=file,
ContentLength=size, ContentType=mime)
row = conn.execute("UPDATE attachments SET status='ready', updated_at=clock_timestamp() WHERE id=%s RETURNING *", (aid,)).fetchone()
return public(row)
@app.post('/api/v1/chats/{chat_id}/attachments', status_code=201)
async def upload(chat_id: str, request: Request, actor=Depends(current_user)):
await run_in_threadpool(visible_chat, chat_id, actor['id'])
name = request.query_params.get('filename')
if set(request.query_params) != {'filename'} or not valid_filename(name):
raise ApiError(400, 'invalid_request', 'Invalid filename')
mime = request.headers.get('content-type', '').split(';')[0].strip().lower()
if mime not in ('image/png', 'image/jpeg'):
raise ApiError(415, 'unsupported_media_type', 'PNG or JPEG required')
length = request.headers.get('content-length')
if length is not None:
if not length.isdigit():
raise ApiError(400, 'invalid_request', 'Invalid Content-Length')
if int(length) > storage.MAX_BYTES:
raise ApiError(413, 'payload_too_large', 'File too large')
try:
await asyncio.wait_for(slots.acquire(), timeout=0.5)
except asyncio.TimeoutError:
raise ApiError(503, 'unavailable', 'Upload capacity reached')
try:
with tempfile.TemporaryFile() as file:
size = 0
digest = hashlib.sha256()
try:
async with asyncio.timeout(30):
async for chunk in request.stream():
size += len(chunk)
if size > storage.MAX_BYTES:
raise ApiError(413, 'payload_too_large', 'File too large')
digest.update(chunk)
await run_in_threadpool(file.write, chunk)
except TimeoutError:
raise ApiError(400, 'invalid_request', 'Upload timed out')
return await run_in_threadpool(save, file, chat_id, actor, name, mime, size, digest.hexdigest())
finally:
slots.release()
@app.get('/api/v1/attachments/{aid}')
def metadata(aid: str, actor=Depends(current_user)):
return public(visible_attachment(aid, actor))
@app.get('/api/v1/attachments/{aid}/content')
def content(aid: str, variant: str = 'original', actor=Depends(current_user)):
row = visible_attachment(aid, actor)
if variant != 'original':
raise ApiError(404, 'not_found', 'Variant not found')
obj = storage.client.get_object(Bucket=storage.BUCKET, Key=row['object_key'])
def chunks():
try:
while data := obj['Body'].read(64 * 1024):
yield data
finally:
obj['Body'].close()
return StreamingResponse(chunks(), media_type=row['content_type'], headers={
'Content-Length': str(row['size_bytes']), 'X-Content-Type-Options': 'nosniff',
'Content-Disposition': "attachment; filename*=UTF-8''" + quote(row['filename'], safe='')})