diff --git a/app.py b/app.py index 7a9ff3a..7ba1314 100644 --- a/app.py +++ b/app.py @@ -4,24 +4,27 @@ import secrets import uuid import json import re +import struct +import hashlib +import hmac from datetime import datetime, timezone, timedelta from typing import List, Optional from uuid import UUID import psycopg2 import psycopg2.extras +psycopg2.extras.register_uuid() import bcrypt from fastapi import FastAPI, Header, HTTPException, Query, Response, Request from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse -from pydantic import BaseModel, Field, field_validator, ConfigDict +from pydantic import BaseModel, Field, field_validator, model_validator, ConfigDict import redis +import boto3 +from botocore.client import Config -import hashlib -import hmac - -app = FastAPI(title="MTUSI Messenger — lab3", version="1.3.0") +app = FastAPI(title="MTUSI Messenger — lab4", version="1.4.0") DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres:postgres@postgres:5432/messenger") SESSION_TTL_SECONDS = int(os.getenv("SESSION_TTL_SECONDS", "3600")) @@ -32,6 +35,20 @@ redis_client = redis.Redis.from_url(REDIS_URL, decode_responses=True) SECRET_HMAC_KEY = os.environ["SECRET_HMAC_KEY"] +S3_ENDPOINT = os.getenv("S3_ENDPOINT", "http://s3:8333") +S3_BUCKET = os.getenv("S3_BUCKET", "messenger-attachments") +S3_ACCESS_KEY = os.getenv("S3_ACCESS_KEY_ID", "minioadmin") +S3_SECRET_KEY = os.getenv("S3_SECRET_ACCESS_KEY", "minioadminpassword") + +s3_client = boto3.client( + "s3", + endpoint_url=S3_ENDPOINT, + aws_access_key_id=S3_ACCESS_KEY, + aws_secret_access_key=S3_SECRET_KEY, + config=Config(signature_version="s3v4"), + region_name="us-east-1" +) + def get_db_connection(): conn = psycopg2.connect(DATABASE_URL, cursor_factory=psycopg2.extras.RealDictCursor) return conn @@ -168,13 +185,14 @@ class CreateChat(BaseModel): class CreateMessage(BaseModel): model_config = ConfigDict(extra='forbid') - text: str = Field(..., min_length=1, max_length=4000) + text: str = Field(default="", max_length=4000) + attachment_ids: List[uuid.UUID] = Field(default=[], max_items=10) - @field_validator("text") - def not_blank(cls, v): - if not v.strip(): - raise ValueError("Must not be blank") - return v + @model_validator(mode="after") + def check_content(self) -> 'CreateMessage': + if not self.text.strip() and not self.attachment_ids: + raise ValueError("Message must contain either text or attachments") + return self @app.get("/health/live") def health_live(response: Response): @@ -192,8 +210,9 @@ def health_ready(response: Response): conn = get_db_connection() conn.close() redis_client.ping() + s3_client.head_bucket(Bucket=S3_BUCKET) except Exception: - raise HTTPException(status_code=503, detail="Database or Redis unavailable") + raise HTTPException(status_code=503, detail="Database, Redis or S3 unavailable") return {"status": "ok"} @app.post("/api/v1/users", status_code=201) @@ -265,7 +284,7 @@ def create_session(authorization: Optional[str] = Header(None), response: Respon ) token = secrets.token_urlsafe(48) - redis_client.setex(f"session:{token}", SESSION_TTL_SECONDS, user['id']) + redis_client.setex(f"session:{token}", SESSION_TTL_SECONDS, str(user['id'])) expires_at = datetime.now(timezone.utc) + timedelta(seconds=SESSION_TTL_SECONDS) expires_at_str = expires_at.isoformat().replace("+00:00", "Z") @@ -316,7 +335,7 @@ def get_current_user_from_token(authorization: Optional[str] = Header(None)) -> ) return { - "id": row['id'], + "id": str(row['id']), "username": row['username'], "display_name": row['display_name'], "created_at": row['created_at'].isoformat().replace("+00:00", "Z") if isinstance(row['created_at'], datetime) else row['created_at'] @@ -498,6 +517,205 @@ def get_chat(chat_id: UUID, response: Response, authorization: Optional[str] = H "created_at": chat['created_at'].isoformat().replace("+00:00", "Z") if isinstance(chat['created_at'], datetime) else chat['created_at'] } +def validate_image_and_get_dimensions(data: bytes, content_type: str): + if len(data) > 10 * 1024 * 1024: + raise HTTPException(status_code=413, detail="Payload too large (>10 MiB)") + + width, height = 0, 0 + if content_type == "image/png": + if not data.startswith(b'\x89PNG\r\n\x1a\n'): + raise HTTPException(status_code=415, detail="Invalid PNG signature") + if len(data) >= 24: + width, height = struct.unpack('>II', data[16:24]) + elif content_type == "image/jpeg": + if not data.startswith(b'\xff\xd8'): + raise HTTPException(status_code=415, detail="Invalid JPEG signature") + i = 2 + while i < len(data) - 9: + marker = data[i] + if marker == 0xff: + marker_type = data[i+1] + if 0xc0 <= marker_type <= 0xc3: + height, width = struct.unpack('>HH', data[i+5:i+9]) + break + i += 2 + struct.unpack('>H', data[i+2:i+4])[0] + else: + i += 1 + else: + raise HTTPException(status_code=415, detail="Unsupported media type") + + if width * height > 40_000_000: + raise HTTPException(status_code=413, detail="Image exceeds 40 million pixels limit") + +@app.post("/api/v1/chats/{chat_id}/attachments", status_code=201) +async def upload_attachment( + chat_id: UUID, + filename: str = Query(..., min_length=1, max_length=255), + request: Request = None, + response: Response = None, + authorization: Optional[str] = Header(None) +): + req_id = generate_request_id() + response.headers["X-Request-Id"] = req_id + response.headers["X-Instance-Id"] = INSTANCE_ID + user = get_current_user_from_token(authorization) + chat_id_str = str(chat_id) + + conn = get_db_connection() + with conn.cursor() as cur: + cur.execute("SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s", (chat_id_str, user["id"])) + if not cur.fetchone(): + conn.close() + raise HTTPException(status_code=404, detail="Chat not found or inaccessible") + conn.close() + + if any(ord(c) < 0x20 or ord(c) == 0x7f for c in filename): + raise HTTPException(400, "Invalid filename") + + content_type = request.headers.get("content-type") + if content_type not in ["image/png", "image/jpeg"]: + raise HTTPException(status_code=415, detail="Unsupported media type") + + MAX_UPLOAD = 10 * 1024 * 1024 + + cl = request.headers.get("content-length") + if cl is not None: + try: + if int(cl) > MAX_UPLOAD: + raise HTTPException(status_code=413, detail="Payload too large (>10 MiB)") + except ValueError: + raise HTTPException(status_code=400, detail="Invalid Content-Length") + + chunks = [] + total = 0 + async for chunk in request.stream(): + total += len(chunk) + if total > MAX_UPLOAD: + raise HTTPException(status_code=413, detail="Payload too large (>10 MiB)") + chunks.append(chunk) + body_bytes = b"".join(chunks) + + size_bytes = len(body_bytes) + sha256_hash = hashlib.sha256(body_bytes).hexdigest() + attachment_id = str(uuid.uuid4()) + now = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z") + + s3_key = f"attachments/{chat_id_str}/{attachment_id}/{filename}" + try: + s3_client.put_object( + Bucket=S3_BUCKET, + Key=s3_key, + Body=body_bytes, + ContentType=content_type + ) + except Exception: + raise HTTPException(status_code=503, detail="S3 storage unavailable") + + conn = get_db_connection() + try: + with conn.cursor() as cur: + cur.execute(""" + INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, status, variants, error_code, created_at, updated_at) + VALUES (%s, %s, %s, %s, %s, %s, %s, 'ready', '[]', NULL, %s, %s) + """, (attachment_id, chat_id_str, user["id"], filename, content_type, size_bytes, sha256_hash, now, now)) + conn.commit() + except Exception: + conn.rollback() + raise HTTPException(status_code=500, detail="Internal error saving attachment metadata") + finally: + conn.close() + + return { + "id": attachment_id, + "chat_id": chat_id_str, + "owner_id": user["id"], + "filename": filename, + "content_type": content_type, + "size_bytes": size_bytes, + "sha256": sha256_hash, + "status": "ready", + "variants": [], + "error_code": None, + "created_at": now, + "updated_at": now + } + +@app.get("/api/v1/attachments/{attachment_id}") +def get_attachment(attachment_id: UUID, response: Response, authorization: Optional[str] = Header(None)): + req_id = generate_request_id() + response.headers["X-Request-Id"] = req_id + response.headers["X-Instance-Id"] = INSTANCE_ID + user = get_current_user_from_token(authorization) + att_id_str = str(attachment_id) + + conn = get_db_connection() + with conn.cursor() as cur: + cur.execute(""" + SELECT a.* FROM attachments a + JOIN chat_members cm ON a.chat_id = cm.chat_id + WHERE a.id = %s AND cm.user_id = %s + """, (att_id_str, user["id"])) + row = cur.fetchone() + conn.close() + + if not row: + raise HTTPException(status_code=404, detail="Attachment not found or inaccessible") + + return { + "id": str(row['id']), + "chat_id": str(row['chat_id']), + "owner_id": str(row['owner_id']), + "filename": row['filename'], + "content_type": row['content_type'], + "size_bytes": row['size_bytes'], + "sha256": row['sha256'], + "status": row['status'], + "variants": row['variants'], + "error_code": row['error_code'], + "created_at": row['created_at'].isoformat().replace("+00:00", "Z") if isinstance(row['created_at'], datetime) else row['created_at'], + "updated_at": row['updated_at'].isoformat().replace("+00:00", "Z") if isinstance(row['updated_at'], datetime) else row['updated_at'] + } + +@app.get("/api/v1/attachments/{attachment_id}/content") +def download_attachment(attachment_id: UUID, response: Response, authorization: Optional[str] = Header(None)): + req_id = generate_request_id() + response.headers["X-Request-Id"] = req_id + response.headers["X-Instance-Id"] = INSTANCE_ID + user = get_current_user_from_token(authorization) + att_id_str = str(attachment_id) + + conn = get_db_connection() + with conn.cursor() as cur: + cur.execute(""" + SELECT a.* FROM attachments a + JOIN chat_members cm ON a.chat_id = cm.chat_id + WHERE a.id = %s AND cm.user_id = %s + """, (att_id_str, user["id"])) + row = cur.fetchone() + conn.close() + + if not row: + raise HTTPException(status_code=404, detail="Attachment not found or inaccessible") + + if row['status'] != 'ready': + raise HTTPException(status_code=409, detail="Attachment not ready") + + s3_key = f"attachments/{row['chat_id']}/{row['id']}/{row['filename']}" + try: + obj = s3_client.get_object(Bucket=S3_BUCKET, Key=s3_key) + data = obj['Body'].read() + except Exception: + raise HTTPException(status_code=503, detail="S3 storage unavailable or object missing") + + return Response( + content=data, + media_type=row['content_type'], + headers={ + "X-Request-Id": req_id, + "X-Instance-Id": INSTANCE_ID, + }, + ) + @app.post("/api/v1/chats/{chat_id}/messages", status_code=201) def create_message( chat_id: UUID, @@ -518,7 +736,7 @@ def create_message( method = "POST" path = f"/api/v1/chats/{chat_id_str}/messages" - body_json_str = json.dumps(body.model_dump(), sort_keys=True) + body_json_str = json.dumps(body.model_dump(mode='json'), sort_keys=True) request_hash = hashlib.sha256(body_json_str.encode("utf-8")).hexdigest() conn = get_db_connection() @@ -532,6 +750,21 @@ def create_message( conn.close() raise HTTPException(status_code=404, detail="Chat not found") + attachment_ids_str = [str(aid) for aid in body.attachment_ids] + if attachment_ids_str: + cur.execute(""" + SELECT id, status FROM attachments + WHERE id = ANY(%s::uuid[]) AND chat_id = %s + """, (attachment_ids_str, chat_id_str)) + found_atts = cur.fetchall() + if len(found_atts) != len(attachment_ids_str): + conn.close() + raise HTTPException(status_code=404, detail="One or more attachments not found or in another chat") + for att in found_atts: + if att['status'] == 'failed': + conn.close() + raise HTTPException(status_code=409, detail="Cannot attach failed attachment") + msg_id = str(uuid.uuid4()) created_at = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z") @@ -540,7 +773,8 @@ def create_message( "chat_id": chat_id_str, "sender_id": user["id"], "text": body.text, - "created_at": created_at + "created_at": created_at, + "attachment_ids": attachment_ids_str } cur.execute(""" @@ -565,8 +799,8 @@ def create_message( return row["response_body"] cur.execute( - "INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES (%s, %s, %s, %s, %s)", - (msg_id, chat_id_str, user["id"], body.text, created_at) + "INSERT INTO messages (id, chat_id, sender_id, text, created_at, attachment_ids) VALUES (%s, %s, %s, %s, %s, %s::uuid[])", + (msg_id, chat_id_str, user["id"], body.text, created_at, attachment_ids_str) ) conn.commit() except HTTPException: @@ -605,7 +839,7 @@ def list_messages(chat_id: UUID, limit: int = 50, cursor: Optional[str] = None, raise HTTPException(status_code=400, detail="Invalid limit") query = """ - SELECT id, chat_id, sender_id, text, created_at FROM messages + SELECT id, chat_id, sender_id, text, created_at, attachment_ids FROM messages WHERE chat_id = %s """ params = [chat_id_str] @@ -628,12 +862,20 @@ def list_messages(chat_id: UUID, limit: int = 50, cursor: Optional[str] = None, formatted_msgs = [] for m in page_items: + raw_atts = m.get('attachment_ids') + atts = [] + if raw_atts: + for aid in raw_atts: + if aid: + atts.append(str(aid)) + formatted_msgs.append({ "id": m['id'], "chat_id": m['chat_id'], "sender_id": m['sender_id'], "text": m['text'], - "created_at": m['created_at'].isoformat().replace("+00:00", "Z") if isinstance(m['created_at'], datetime) else m['created_at'] + "created_at": m['created_at'].isoformat().replace("+00:00", "Z") if isinstance(m['created_at'], datetime) else m['created_at'], + "attachment_ids": atts }) next_cursor = None diff --git a/compose.yaml b/compose.yaml index d559949..d695254 100644 --- a/compose.yaml +++ b/compose.yaml @@ -125,6 +125,29 @@ services: timeout: 3s retries: 3 + s3: + image: chrislusf/seaweedfs:latest + command: "mini -dir=/data" + environment: + - AWS_ACCESS_KEY_ID=minioadmin + - AWS_SECRET_ACCESS_KEY=minioadminpassword + - S3_BUCKET=messenger-attachments + ports: + - "8333:8333" + volumes: + - s3data:/data + deploy: + resources: + limits: + cpus: '0.50' + memory: 512M + healthcheck: + test: ["CMD", "nc", "-z", "localhost", "8333"] + interval: 5s + timeout: 3s + retries: 3 + volumes: pgdata: - redisdata: \ No newline at end of file + redisdata: + s3data: \ No newline at end of file diff --git a/migrations/init.sql b/migrations/init.sql index 2725c96..978ebac 100644 --- a/migrations/init.sql +++ b/migrations/init.sql @@ -23,7 +23,8 @@ CREATE TABLE IF NOT EXISTS messages ( chat_id UUID REFERENCES chats(id) ON DELETE CASCADE, sender_id UUID REFERENCES users(id) ON DELETE CASCADE, text VARCHAR(4000) NOT NULL, - created_at TIMESTAMPTZ NOT NULL + created_at TIMESTAMPTZ NOT NULL, + attachment_ids UUID[] DEFAULT '{}' ); CREATE INDEX IF NOT EXISTS idx_messages_chat_pagination ON messages(chat_id, created_at, id); @@ -38,4 +39,23 @@ CREATE TABLE IF NOT EXISTS idempotency_keys ( response_body JSONB NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), PRIMARY KEY (user_id, method, path, key) -); \ No newline at end of file +); + +CREATE TABLE IF NOT EXISTS attachments ( + id UUID PRIMARY KEY, + chat_id UUID REFERENCES chats(id) ON DELETE CASCADE, + owner_id UUID REFERENCES users(id) ON DELETE CASCADE, + filename VARCHAR(255) NOT NULL, + content_type VARCHAR(50) NOT NULL, + size_bytes BIGINT NOT NULL, + sha256 VARCHAR(64) NOT NULL, + status VARCHAR(32) NOT NULL DEFAULT 'ready', + variants JSONB NOT NULL DEFAULT '[]', + error_code VARCHAR(64), + created_at TIMESTAMPTZ NOT NULL, + updated_at TIMESTAMPTZ NOT NULL +); + +CREATE INDEX IF NOT EXISTS idx_attachments_chat ON attachments(chat_id); + +ALTER TABLE messages ADD COLUMN IF NOT EXISTS attachment_ids UUID[] DEFAULT '{}'; \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index ed45144..ab928d4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,5 @@ h11==0.14.0 annotated-types==0.6.0 psycopg2-binary==2.9.9 bcrypt==4.1.2 -redis==5.0.1 \ No newline at end of file +redis==5.0.1 +boto3==1.34.0 \ No newline at end of file