9c6c477f55
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 2m22s
CI/CD Pipeline / Unit Tests (pull_request) Failing after 2m24s
CI/CD Pipeline / Integration Tests (pull_request) Failing after 37s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 4m3s
CI/CD Pipeline / Build & Push Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Build Production Runtime Images (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
P0 关键修复: - P0-1: 注册接口添加 RateLimitMiddleware 限流保护 - P0-3: /metrics 端点添加 JWT 认证(移除匿名访问) - P0-4: 修复 Celery 任务名冲突(generation_task vs generate_video) - P1-5: JWT logout token 黑名单机制 P1 修复: - P1-1: forgot_password 硬编码 localhost → 使用 settings.APP_BASE_URL - P1-2: generation.py 直接创建 DB 连接 → 使用依赖注入 - P1-6: Image.open() 未关闭 → 统一使用 with 语句 - P1-7: 订阅续费事务修复 P2 代码质量: - P2-1: 修复 EditingMode 枚举重复定义 → 统一引用 shared 包 - P2-2: 修复 SMTP_FRON_NAME → SMTP_FROM_NAME 拼写 - P2-3: UserModel subscription_quota 类型统一为 float - P2-4: .env.production DATABASE_MAX_OVERFLOW 30 → 10 - 清理 15 处 except:pass(保留 2 处有注释说明的) - 禁用 SVG 上传(XSS 风险) - 删除 decode_token_unsafe() 不安全函数 - 简化 /ready 端点 - 删除 8 处死代码、10 个空文件/模块 - 合并 3 对 100% 重复函数 - 对齐 6 个废弃环境变量 v2 修复(代码审查后): - 修复密码重置路由路径: /password/forgot → /forgot-password, /password/reset → /reset-password(与前端 API 对齐) - 合并 _check_project_access: asset_libraries.py 和 edit_plans.py 中的重复函数统一到 _helpers.py(含空字符串守卫 + 中文错误信息) - 顺手修复: HTTPException 统一从 fastapi 导入(替换 starlette 导入) - OSS_ENDPOINT 拼写修复拆分为单独 PR,本 PR 不包含
226 lines
7.8 KiB
Python
226 lines
7.8 KiB
Python
"""Dependency injection providers for FastAPI endpoints.
|
|
|
|
All repository and service factories are defined here as FastAPI dependencies,
|
|
ensuring proper lifecycle management and testability.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Generator
|
|
|
|
import redis
|
|
from app.config import settings
|
|
from fastapi import Depends
|
|
from sqlalchemy.orm import Session
|
|
|
|
from packages.adapters.redis import NoopSessionStore, SessionStore
|
|
from packages.adapters.smtp import EmailConfig, EmailService, NoopEmailService, get_email_service
|
|
from packages.adapters.sqlalchemy_impl.asset_library_repository import (
|
|
SQLAlchemyAssetLibraryRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.asset_repository import SQLAlchemyAssetRepository
|
|
from packages.adapters.sqlalchemy_impl.classification_job_repository import (
|
|
SQLAlchemyClassificationJobRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.duplication_repository import (
|
|
SQLAlchemyDuplicationRecordRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.generated_video_repository import (
|
|
SQLAlchemyGeneratedVideoRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.generation_task_repository import (
|
|
SQLAlchemyGenerationTaskRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.ingest_job_repository import (
|
|
SQLAlchemyIngestJobRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.job_repository import SQLAlchemyJobRepository
|
|
from packages.adapters.sqlalchemy_impl.project_repository import (
|
|
SQLAlchemyProjectRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.session import build_session_factory
|
|
from packages.adapters.sqlalchemy_impl.tag_repository import SQLAlchemyTagRepository
|
|
from packages.adapters.sqlalchemy_impl.title_library_repository import (
|
|
SQLAlchemyTitleLibraryRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.user_repository import SQLAlchemyUserRepository
|
|
from packages.adapters.sqlalchemy_impl.voice_clone_profile_repository import (
|
|
SQLAlchemyVoiceCloneProfileRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.voice_library_repository import (
|
|
SQLAlchemyVoiceLibraryRepository,
|
|
)
|
|
from packages.ports.tag_repository import TagRepository
|
|
from packages.ports.user_repository import UserRepository
|
|
|
|
_engine, _SessionLocal = build_session_factory(settings.DATABASE_URL)
|
|
|
|
|
|
def get_db_session() -> Generator[Session, None, None]:
|
|
"""Provide a database session with automatic cleanup."""
|
|
session: Session = _SessionLocal()
|
|
try:
|
|
yield session
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
def get_asset_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyAssetRepository:
|
|
"""Provide the SQLAlchemy asset repository implementation."""
|
|
return SQLAlchemyAssetRepository(session)
|
|
|
|
|
|
def get_asset_library_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyAssetLibraryRepository:
|
|
"""Provide the SQLAlchemy asset library repository implementation."""
|
|
return SQLAlchemyAssetLibraryRepository(session)
|
|
|
|
|
|
def get_ingest_job_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyIngestJobRepository:
|
|
"""Provide the SQLAlchemy ingest job repository implementation."""
|
|
return SQLAlchemyIngestJobRepository(session)
|
|
|
|
|
|
def get_classification_job_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyClassificationJobRepository:
|
|
"""Provide the SQLAlchemy classification job repository implementation."""
|
|
return SQLAlchemyClassificationJobRepository(session)
|
|
|
|
|
|
def get_generation_task_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyGenerationTaskRepository:
|
|
"""Provide the SQLAlchemy generation task repository implementation."""
|
|
return SQLAlchemyGenerationTaskRepository(session)
|
|
|
|
|
|
def get_job_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyJobRepository:
|
|
"""Provide the SQLAlchemy job repository implementation."""
|
|
return SQLAlchemyJobRepository(session)
|
|
|
|
|
|
def get_generated_video_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyGeneratedVideoRepository:
|
|
"""Provide the SQLAlchemy generated video repository implementation."""
|
|
return SQLAlchemyGeneratedVideoRepository(session)
|
|
|
|
|
|
def get_duplication_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyDuplicationRecordRepository:
|
|
"""Provide the SQLAlchemy duplication record repository implementation."""
|
|
return SQLAlchemyDuplicationRecordRepository(session)
|
|
|
|
|
|
def get_project_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyProjectRepository:
|
|
"""Provide the SQLAlchemy project repository implementation."""
|
|
return SQLAlchemyProjectRepository(session)
|
|
|
|
|
|
def get_tag_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> TagRepository:
|
|
"""Provide the SQLAlchemy tag repository implementation."""
|
|
return SQLAlchemyTagRepository(session)
|
|
|
|
|
|
def get_user_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> UserRepository:
|
|
"""Provide the SQLAlchemy user repository implementation."""
|
|
return SQLAlchemyUserRepository(session)
|
|
|
|
|
|
def get_auth_session_store() -> SessionStore | NoopSessionStore:
|
|
"""Provide the session store based on configuration."""
|
|
if not settings.ENABLE_REDIS_SESSIONS:
|
|
return NoopSessionStore()
|
|
return SessionStore(redis_client=redis.from_url(settings.REDIS_URL, decode_responses=True))
|
|
|
|
|
|
def get_auth_email_service() -> NoopEmailService | EmailService:
|
|
"""Provide the email service based on configuration."""
|
|
if not settings.ENABLE_EMAIL_DELIVERY:
|
|
return NoopEmailService()
|
|
return get_email_service(
|
|
EmailConfig(
|
|
smtp_host=settings.SMTP_HOST,
|
|
smtp_port=settings.SMTP_PORT,
|
|
smtp_user=settings.SMTP_USER,
|
|
smtp_password=settings.SMTP_PASSWORD,
|
|
from_email=settings.SMTP_FROM_EMAIL,
|
|
from_name=settings.SMTP_FROM_NAME,
|
|
use_tls=settings.SMTP_USE_TLS,
|
|
),
|
|
enabled=True,
|
|
)
|
|
|
|
|
|
def get_title_library_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyTitleLibraryRepository:
|
|
"""Provide the SQLAlchemy title library repository implementation."""
|
|
return SQLAlchemyTitleLibraryRepository(session)
|
|
|
|
|
|
def get_voice_library_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyVoiceLibraryRepository:
|
|
"""Provide the SQLAlchemy voice library repository implementation."""
|
|
return SQLAlchemyVoiceLibraryRepository(session)
|
|
|
|
|
|
def get_voice_clone_profile_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyVoiceCloneProfileRepository:
|
|
"""Provide the SQLAlchemy voice clone profile repository implementation."""
|
|
return SQLAlchemyVoiceCloneProfileRepository(session)
|
|
|
|
|
|
def get_cosyvoice_service():
|
|
"""Provide the CosyVoice service instance.
|
|
|
|
注入 OSS 音频URL预签名函数,确保私有bucket下的参考音频
|
|
能被 CosyVoice 服务器下载。
|
|
"""
|
|
from app.core.storage import get_storage_service
|
|
|
|
from packages.application.cosyvoice_service import CosyVoiceService
|
|
|
|
storage = get_storage_service()
|
|
|
|
def _sign_audio_url(url: str) -> str:
|
|
"""对音频URL做预签名,私有bucket下 CosyVoice 服务器才能下载."""
|
|
return storage.get_download_url(url, expires_seconds=86400)
|
|
|
|
return CosyVoiceService(audio_url_signer=_sign_audio_url)
|
|
|
|
|
|
def get_audio_url_signer():
|
|
"""提供音频URL预签名函数(24小时有效期)。
|
|
|
|
用于所有 API 返回给前端的音频 URL,确保私有 OSS bucket 下可正常访问。
|
|
空 URL、非 OSS URL 直接原样返回;签名失败时回退到原始 URL。
|
|
"""
|
|
from app.core.storage import get_storage_service
|
|
|
|
storage = get_storage_service()
|
|
|
|
def sign_audio_url(url: str) -> str:
|
|
if not url:
|
|
return url
|
|
return storage.get_download_url(url, expires_seconds=86400)
|
|
|
|
return sign_audio_url
|