Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/clients/storage/s3_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def __init__(self) -> None:
aws_secret_access_key=settings.S3_SECRET_ACCESS_KEY,
)

def generate_presigned_put_url(
self, *, storage_key: str, content_type: str
) -> str:
def generate_presigned_put_url(self, *, storage_key: str, content_type: str) -> str:
url = self._presign_client.generate_presigned_url(
"put_object",
Params={
Expand Down Expand Up @@ -81,3 +79,8 @@ def get_object(self, *, storage_key: str) -> dict[str, Any]:
response = self._client.get_object(Bucket=self._bucket, Key=storage_key)
logger.info("get_object done key={key}", key=storage_key)
return response

def head_object(self, *, storage_key: str) -> dict[str, Any]:
response = self._client.head_object(Bucket=self._bucket, Key=storage_key)
logger.info("head_object done key={key}", key=storage_key)
return response
55 changes: 55 additions & 0 deletions app/services/media_uploads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from __future__ import annotations

from typing import Any

from app.clients.storage.s3_client import S3Client
from app.services.errors import ValidationError
from app.settings import settings


def validate_uploaded_photo(
s3_client: S3Client,
*,
storage_key: str,
expected_content_type: str | None,
) -> dict[str, Any]:
try:
metadata = s3_client.head_object(storage_key=storage_key)
except (
Exception
) as exc: # noqa: BLE001 - storage providers raise vendor-specific errors
raise ValidationError(
"Uploaded photo object not found",
data={"storage_key": storage_key},
) from exc

content_length = metadata.get("ContentLength")
if not isinstance(content_length, int):
raise ValidationError("Uploaded photo size is unavailable")
if content_length <= 0:
raise ValidationError("Uploaded photo is empty")
if content_length > settings.PHOTO_MAX_BYTES:
raise ValidationError(
"Uploaded photo is too large",
data={
"max_bytes": settings.PHOTO_MAX_BYTES,
"actual_bytes": content_length,
},
)

actual_content_type = metadata.get("ContentType")
if expected_content_type:
if not isinstance(actual_content_type, str):
raise ValidationError("Uploaded photo content type is unavailable")
normalized_actual = actual_content_type.split(";", 1)[0].strip().lower()
normalized_expected = expected_content_type.split(";", 1)[0].strip().lower()
if normalized_actual != normalized_expected:
raise ValidationError(
"Uploaded photo content type mismatch",
data={
"expected_content_type": normalized_expected,
"actual_content_type": normalized_actual,
},
)

return metadata
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from app.logging import get_logger
from app.orm.models.tutor_profile_photo import TutorProfilePhoto
from app.services.errors import ConflictError, ValidationError
from app.services.media_uploads import validate_uploaded_photo
from app.services.tutor_profile_photos.utils import ensure_photo_limit, ensure_tutor
from app.settings import settings

Expand Down Expand Up @@ -37,6 +38,15 @@ async def __call__(
)
self._validate_storage_key(tutor_id, storage_key)
await self._ensure_unique(storage_key)
metadata = validate_uploaded_photo(
self.s3_client,
storage_key=storage_key,
expected_content_type=content_type,
)
storage_content_type = metadata.get("ContentType")
if isinstance(storage_content_type, str):
content_type = storage_content_type.split(";", 1)[0].strip().lower()
size_bytes = metadata["ContentLength"]

if position is None:
position = await self._next_position(tutor_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from app.logging import get_logger
from app.orm.models.tutor_service_photo import TutorServicePhoto
from app.services.errors import ConflictError, ValidationError
from app.services.tutor_service_photos.utils import ensure_photo_limit, ensure_tutor_service
from app.services.media_uploads import validate_uploaded_photo
from app.services.tutor_service_photos.utils import (
ensure_photo_limit,
ensure_tutor_service,
)
from app.settings import settings

logger = get_logger("tutor_service_photo")
Expand Down Expand Up @@ -42,6 +46,15 @@ async def __call__(
)
self._validate_storage_key(tutor_id, service_id, storage_key)
await self._ensure_unique(storage_key)
metadata = validate_uploaded_photo(
self.s3_client,
storage_key=storage_key,
expected_content_type=content_type,
)
storage_content_type = metadata.get("ContentType")
if isinstance(storage_content_type, str):
content_type = storage_content_type.split(";", 1)[0].strip().lower()
size_bytes = metadata["ContentLength"]

if position is None:
position = await self._next_position(tutor_service.id)
Expand Down
10 changes: 10 additions & 0 deletions app/services/user_avatars/commands/create_user_avatar_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from app.logging import get_logger
from app.orm.models.user_avatar import UserAvatar
from app.services.errors import ConflictError, ValidationError
from app.services.media_uploads import validate_uploaded_photo
from app.services.user_avatars.utils import ensure_user, read_active_avatar

logger = get_logger("user_avatar")
Expand All @@ -29,6 +30,15 @@ async def __call__(
) -> UserAvatar:
await ensure_user(self.session, user_id)
self._validate_storage_key(user_id, storage_key)
metadata = validate_uploaded_photo(
self.s3_client,
storage_key=storage_key,
expected_content_type=content_type,
)
storage_content_type = metadata.get("ContentType")
if isinstance(storage_content_type, str):
content_type = storage_content_type.split(";", 1)[0].strip().lower()
size_bytes = metadata["ContentLength"]

avatar = await read_active_avatar(self.session, user_id)
if avatar is None:
Expand Down
1 change: 1 addition & 0 deletions app/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Settings(BaseSettings):
BOT_MESSAGE_DEFAULT_TTL_SECONDS: int = 21600
TUTOR_PROFILE_PHOTO_MAX_COUNT: int = 10
TUTOR_SERVICE_PHOTO_MAX_COUNT: int = 5
PHOTO_MAX_BYTES: int = 8 * 1024 * 1024
S3_ENDPOINT_URL: str | None = None
S3_REGION: str = "us-east-1"
S3_ACCESS_KEY_ID: str = "local"
Expand Down
17 changes: 17 additions & 0 deletions deploy/staging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,20 @@ Quick start on VM:
5) Run:
- `docker compose pull`
- `docker compose up -d`

S3 bucket CORS for presigned photo uploads:
- Native iOS/Android clients do not perform browser CORS preflight, but the PWA will.
- Keep `S3_PRESIGN_EXPIRES` at 900 seconds or at least 300 seconds so users can pass OS photo permissions and upload on slow mobile networks.
- Configure the private bucket with these CORS rules before enabling web/PWA upload:

```json
[
{
"AllowedOrigins": ["https://app.ass-rep-development.ru"],
"AllowedMethods": ["GET", "PUT"],
"AllowedHeaders": ["Content-Type", "Authorization", "x-amz-*"],
"ExposeHeaders": ["ETag"],
"MaxAgeSeconds": 3000
}
]
```
35 changes: 22 additions & 13 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,36 @@
sys.path.insert(0, str(BASE_DIR))

from app.api.main import create_app
from app.clients.storage.s3_client import S3Client
from app.orm.base import Base
from app.orm.models import ( # noqa: F401
bot_message,
blacklist,
bot_message,
chat_message,
chat_thread,
enums,
external_student,
finance_event,
favorite_tutor,
feedback,
finance_event,
location,
notification,
release_note,
review,
service_popularity,
service,
service_popularity,
slot,
slot_finance_fact,
slot_status_history,
student_profile,
tutor_profile,
tutor_profile_photo,
tutor_service_price_history,
tutor_service_photo,
tutor_service,
tutor_trial_price_history,
tutor_service_photo,
tutor_service_price_history,
tutor_student,
tutor_student_request,
tutor_trial_price_history,
user,
user_avatar,
user_notification_settings,
Expand All @@ -75,24 +76,24 @@
if uvloop is not None:
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
from tests.factories import (
BotMessageFactory,
BlacklistFactory,
BotMessageFactory,
ExternalStudentFactory,
FavoriteTutorFactory,
FeedbackFactory,
LocationFactory,
ReleaseNoteFactory,
ReviewFactory,
ServiceFactory,
ServicePopularityFactory,
SlotFactory,
TutorProfilePhotoFactory,
TutorServiceFactory,
TutorServicePhotoFactory,
TutorStudentFactory,
TutorStudentRequestFactory,
TutorProfilePhotoFactory,
UserAvatarFactory,
UserFactory,
ReleaseNoteFactory,
)


Expand All @@ -110,6 +111,14 @@ async def batch(self, size: int, **kwargs):
)


@pytest.fixture(autouse=True)
def fake_uploaded_photo_head(monkeypatch):
def _fake_head_object(*_args, **_kwargs):
return {"ContentLength": 1234, "ContentType": "image/jpeg"}

monkeypatch.setattr(S3Client, "head_object", _fake_head_object)


@pytest.fixture
def db_schema(worker_id: str) -> str:
if worker_id == "master":
Expand All @@ -136,7 +145,9 @@ async def test_engine(db_schema: str) -> AsyncEngine:
finally:
if db_schema != "public":
async with engine.begin() as conn:
await conn.execute(sa.text(f'DROP SCHEMA IF EXISTS "{db_schema}" CASCADE'))
await conn.execute(
sa.text(f'DROP SCHEMA IF EXISTS "{db_schema}" CASCADE')
)
await engine.dispose()


Expand Down Expand Up @@ -171,9 +182,7 @@ async def cleanup_db(async_session_factory, db_schema: str) -> None:
]
await session.execute(
sa.text(
"TRUNCATE TABLE "
+ ", ".join(table_names)
+ " RESTART IDENTITY CASCADE"
"TRUNCATE TABLE " + ", ".join(table_names) + " RESTART IDENTITY CASCADE"
)
)
await session.commit()
Expand Down
Loading