import hashlib import tempfile import uuid import warnings from threading import BoundedSemaphore from flask import Response, request, stream_with_context from PIL import Image, UnidentifiedImageError from src.storage import BUCKET, s3 MAX_BYTES = 10 * 1024 * 1024 Image.MAX_IMAGE_PIXELS = 40_000_000 # Each API worker is bounded by Gunicorn threads; this semaphore prevents an # unbounded number of uploads without serializing the required concurrent case. slots = BoundedSemaphore(16) def public(row): return {k: v for k, v in row.items() if k != 'object_key'} def register_routes(app, pool, actor, chat_access, uid, response, APIError, invalid): @app.post('/api/v1/chats//attachments') def upload(chat_id): with pool.connection() as conn: user = actor(conn) chat_id = uid(chat_id) chat_access(conn, chat_id, user) filename = request.args.get('filename', '') if not 1 <= len(filename) <= 255 or not filename.strip() or any( ord(c) < 32 or ord(c) == 127 or c in '/\\' for c in filename): invalid() if request.mimetype not in ('image/png', 'image/jpeg'): raise APIError(415, 'unsupported_media_type', 'Expected PNG or JPEG') if request.content_length and request.content_length > MAX_BYTES: raise APIError(413, 'payload_too_large', 'Image too large') if not slots.acquire(timeout=5): raise APIError(429, 'rate_limited', 'Upload capacity reached') try: with tempfile.SpooledTemporaryFile(max_size=1024*1024) as file: length, digest = 0, hashlib.sha256() while True: chunk = request.stream.read(65536) if not chunk: break length += len(chunk) if length > MAX_BYTES: raise APIError(413, 'payload_too_large', 'Image too large') digest.update(chunk) file.write(chunk) file.seek(0) try: with warnings.catch_warnings(): warnings.simplefilter('error', Image.DecompressionBombWarning) with Image.open(file) as image: if image.width * image.height > 40_000_000: raise APIError(413, 'payload_too_large', 'Too many pixels') if Image.MIME.get(image.format) != request.mimetype: raise APIError(415, 'unsupported_media_type', 'Image type mismatch') image.verify() file.seek(0) with Image.open(file) as image: image.load() except (Image.DecompressionBombError, Image.DecompressionBombWarning): raise APIError(413, 'payload_too_large', 'Too many pixels') from None except (UnidentifiedImageError, OSError, ValueError, SyntaxError): raise APIError(415, 'unsupported_media_type', 'Invalid image') from None file.seek(0) identifier = uuid.uuid4() key = 'original/' + str(identifier) with pool.connection() as conn: conn.execute('INSERT INTO object_intents(object_key) VALUES (%s)', (key,)) s3.put_object(Bucket=BUCKET, Key=key, Body=file, ContentLength=length, ContentType=request.mimetype) # Intent survives a crash or failed DB commit; the janitor removes old unreferenced objects. with pool.connection() as conn: row = conn.execute('INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,object_key) ' 'VALUES (%s,%s,%s,%s,%s,%s,%s,%s) RETURNING *', (identifier,chat_id,user,filename,request.mimetype,length,digest.hexdigest(),key)).fetchone() conn.execute('DELETE FROM object_intents WHERE object_key=%s', (key,)) return response(public(row),201) finally: slots.release() def authorized(attachment_id): with pool.connection() as conn: user = actor(conn) identifier = uid(attachment_id) row = conn.execute('SELECT a.* FROM attachments a JOIN members m ON m.chat_id=a.chat_id ' 'WHERE a.id=%s AND m.user_id=%s', (identifier,user)).fetchone() if not row: raise APIError(404,'not_found','Attachment not found') return row @app.get('/api/v1/attachments/') def metadata(attachment_id): return response(public(authorized(attachment_id))) @app.get('/api/v1/attachments//content') def download(attachment_id): row = authorized(attachment_id) if row['status'] != 'ready': raise APIError(409,'conflict','Attachment not ready') variant = request.args.get('variant','original') if variant != 'original': raise APIError(404,'not_found','Variant not found') obj = s3.get_object(Bucket=BUCKET,Key=row['object_key']) def chunks(): try: while True: chunk = obj['Body'].read(65536) if not chunk: break yield chunk finally: obj['Body'].close() return Response(stream_with_context(chunks()),content_type=row['content_type'], headers={'Content-Length':str(row['size_bytes']),'X-Content-Type-Options':'nosniff'})