from pathlib import Path import secrets from urllib.parse import urlparse from uuid import uuid4 from fastapi import Depends, FastAPI, File, Form, HTTPException, UploadFile from pydantic import BaseModel from fastapi.responses import FileResponse from fastapi.security import HTTPAuthorizationCredentials, HTTPBasic, HTTPBasicCredentials, HTTPBearer from fastapi.staticfiles import StaticFiles from .ai_config import get_ai_status, get_config_value, load_local_whisper_config, load_text_ai_config from .auth import ( create_auth_token, generate_activation_code, hash_password, normalize_username, verify_auth_token, verify_password, ) from .db import ( DATABASE_PATH, create_activation_codes, create_video, delete_video, delete_unused_activation_code, get_user_auth_record, get_user_by_id, initialize_database, list_activation_codes, list_users, list_videos, mark_user_login, parse_tags, register_user_with_activation, table_names, get_video, update_user_active, update_video, upsert_video_subtitle, ) from .deepseek_client import DeepSeekClient, DeepSeekError from .local_whisper import LocalWhisperError, transcribe_video_to_vtt from .subtitle_ai import process_subtitles_with_ai PROJECT_DIR = Path(__file__).resolve().parent.parent UPLOADS_DIR = PROJECT_DIR / "uploads" VIDEO_EXTENSIONS = {".mp4", ".webm", ".mov", ".m4v"} SUBTITLE_EXTENSIONS = {".srt", ".vtt"} IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".gif"} UPLOAD_CHUNK_SIZE = 1024 * 1024 def ensure_upload_directories() -> None: UPLOADS_DIR.mkdir(parents=True, exist_ok=True) (UPLOADS_DIR / "videos").mkdir(parents=True, exist_ok=True) (UPLOADS_DIR / "images").mkdir(parents=True, exist_ok=True) (UPLOADS_DIR / "subtitles").mkdir(parents=True, exist_ok=True) def extension_for_upload(upload: UploadFile) -> str: return Path(upload.filename or "").suffix.lower() def safe_upload_name(upload: UploadFile) -> str: extension = extension_for_upload(upload) return f"{uuid4().hex}{extension}" async def save_upload(upload: UploadFile, target_dir: Path) -> tuple[str, str]: target_dir.mkdir(parents=True, exist_ok=True) filename = safe_upload_name(upload) target_path = target_dir / filename content = await upload.read() try: target_path.write_bytes(content) except OSError as exc: target_path.unlink(missing_ok=True) raise HTTPException(status_code=500, detail="保存文件失败,请重试") from exc return filename, content.decode("utf-8-sig", errors="replace") async def save_binary_upload(upload: UploadFile, target_dir: Path) -> str: target_dir.mkdir(parents=True, exist_ok=True) filename = safe_upload_name(upload) target_path = target_dir / filename try: with target_path.open("wb") as target_file: while chunk := await upload.read(UPLOAD_CHUNK_SIZE): target_file.write(chunk) except OSError as exc: target_path.unlink(missing_ok=True) raise HTTPException(status_code=500, detail="保存文件失败,请重试") from exc return filename def cleanup_saved_files(paths: list[Path]) -> None: for path in paths: try: path.unlink(missing_ok=True) except OSError: pass def upload_url_to_path(value: str) -> Path | None: if not value.startswith("/uploads/"): return None relative_parts = value.removeprefix("/uploads/").split("/") if any(part in {"", ".", ".."} for part in relative_parts): return None candidate = (UPLOADS_DIR / Path(*relative_parts)).resolve() uploads_root = UPLOADS_DIR.resolve() try: candidate.relative_to(uploads_root) except ValueError: return None return candidate def is_supported_video_url(value: str) -> bool: parsed = urlparse(value) return parsed.scheme in {"http", "https"} and bool(parsed.netloc) def cleanup_upload_urls(upload_urls: list[str]) -> list[str]: warnings: list[str] = [] for upload_url in upload_urls: path = upload_url_to_path(str(upload_url)) if path is None: continue try: path.unlink(missing_ok=True) except OSError: warnings.append(f"无法删除文件: {path}") return warnings def save_generated_subtitle_file(content: str, language_code: str) -> str: subtitles_dir = UPLOADS_DIR / "subtitles" subtitles_dir.mkdir(parents=True, exist_ok=True) filename = f"{uuid4().hex}.ai.{language_code}.vtt" (subtitles_dir / filename).write_text(content, encoding="utf-8") return f"/uploads/subtitles/{filename}" ensure_upload_directories() app = FastAPI(title="Speaking Lab API") admin_security = HTTPBasic(auto_error=False) bearer_security = HTTPBearer(auto_error=False) class LoginRequest(BaseModel): username: str password: str class RegisterRequest(BaseModel): activationCode: str username: str password: str class GenerateActivationCodesRequest(BaseModel): quantity: int = 1 class UpdateUserStatusRequest(BaseModel): isActive: bool @app.on_event("startup") def on_startup() -> None: initialize_database() ensure_upload_directories() app.mount("/uploads", StaticFiles(directory=UPLOADS_DIR), name="uploads") @app.get("/api/health") def health_check() -> dict[str, object]: return { "ok": True, "database": str(DATABASE_PATH), "tables": table_names(), } @app.get("/api/videos") def api_list_videos() -> list[dict[str, object]]: return list_videos() @app.get("/api/videos/{video_id}") def api_get_video(video_id: int) -> dict[str, object]: video = get_video(video_id) if video is None: raise HTTPException(status_code=404, detail="视频不存在") return video def get_token_secret() -> str: configured = get_config_value("AUTH_SECRET") if configured: return configured admin_password = get_config_value("ADMIN_PASSWORD") return admin_password or "speaking-lab-development-secret" def build_user_response(username: str, role: str, user_id: int | None = None) -> dict[str, object]: return { "id": user_id, "username": username, "role": role, } def create_session_payload(username: str, role: str, user_id: int | None = None) -> dict[str, object]: token = create_auth_token( { "username": username, "role": role, "userId": user_id, }, get_token_secret(), ) return { "token": token, "user": build_user_response(username, role, user_id), } def validate_bearer_session(token: str) -> dict[str, object] | None: payload = verify_auth_token(token, get_token_secret()) if not payload: return None role = str(payload.get("role") or "") username = str(payload.get("username") or "") if role == "admin" and username: return build_user_response(username, "admin", None) user_id = int(payload.get("userId") or 0) user = get_user_by_id(user_id) if user_id else None if not user or not user["isActive"]: return None if user["username"] != username: return None return build_user_response(str(user["username"]), str(user["role"]), int(user["id"])) def require_session(credentials: HTTPAuthorizationCredentials | None = Depends(bearer_security)) -> dict[str, object]: if credentials is None: raise HTTPException(status_code=401, detail="请先登录。") session = validate_bearer_session(credentials.credentials) if session is None: raise HTTPException(status_code=401, detail="登录已失效,请重新登录。") return session @app.post("/api/auth/login") def api_auth_login(request: LoginRequest) -> dict[str, object]: username = normalize_username(request.username) password = request.password if not username or not password: raise HTTPException(status_code=400, detail="请输入账号和密码。") admin_username = normalize_username(get_config_value("ADMIN_USERNAME")) admin_password = get_config_value("ADMIN_PASSWORD") if admin_username and admin_password and username == admin_username: if not secrets.compare_digest(password, admin_password): raise HTTPException(status_code=401, detail="账号或密码错误。") return create_session_payload(admin_username, "admin") user = get_user_auth_record(username) if user is None or not verify_password(password, user["password_hash"]): raise HTTPException(status_code=401, detail="账号或密码错误。") if not bool(user["is_active"]): raise HTTPException(status_code=403, detail="账号已停用,请联系管理员。") mark_user_login(int(user["id"])) return create_session_payload(str(user["email"]), str(user["role"]), int(user["id"])) @app.post("/api/auth/register") def api_auth_register(request: RegisterRequest) -> dict[str, object]: username = normalize_username(request.username) password = request.password activation_code = request.activationCode.strip().upper() if not activation_code: raise HTTPException(status_code=400, detail="请输入激活卡密。") if not username or not password: raise HTTPException(status_code=400, detail="请输入账号和密码。") if len(password) < 6: raise HTTPException(status_code=400, detail="密码至少需要 6 位。") try: user = register_user_with_activation( username=username, password_hash=hash_password(password), activation_code=activation_code, ) except ValueError as error: if str(error) == "username_exists": raise HTTPException(status_code=409, detail="账号已存在。") from error raise HTTPException(status_code=400, detail="激活卡密无效或已被使用。") from error mark_user_login(int(user["id"])) return create_session_payload(str(user["username"]), str(user["role"]), int(user["id"])) @app.get("/api/auth/session") def api_auth_session(session: dict[str, object] = Depends(require_session)) -> dict[str, object]: return {"ok": True, "user": session} def require_admin( credentials: HTTPBasicCredentials | None = Depends(admin_security), bearer: HTTPAuthorizationCredentials | None = Depends(bearer_security), ) -> dict[str, str]: admin_username = get_config_value("ADMIN_USERNAME") admin_password = get_config_value("ADMIN_PASSWORD") if not admin_username or not admin_password: raise HTTPException( status_code=503, detail="未配置 ADMIN_USERNAME / ADMIN_PASSWORD,请先在 .env 中填写管理员账号密码。", ) if credentials is not None: username_ok = secrets.compare_digest(credentials.username, admin_username) password_ok = secrets.compare_digest(credentials.password, admin_password) if username_ok and password_ok: return {"username": admin_username, "role": "admin"} if bearer is not None and hasattr(bearer, "credentials"): session = validate_bearer_session(bearer.credentials) if session and session["role"] == "admin": return {"username": str(session["username"]), "role": "admin"} raise HTTPException( status_code=401, detail="需要管理员账号。", ) @app.get("/api/admin/session") def api_admin_session(admin: dict[str, str] = Depends(require_admin)) -> dict[str, object]: return {"ok": True, "username": admin["username"], "role": "admin"} @app.get("/api/admin/users") def api_admin_users(admin: dict[str, str] = Depends(require_admin)) -> list[dict[str, object]]: return list_users() @app.patch("/api/admin/users/{user_id}") def api_admin_update_user( user_id: int, request: UpdateUserStatusRequest, admin: dict[str, str] = Depends(require_admin), ) -> dict[str, object]: user = update_user_active(user_id, request.isActive) if user is None: raise HTTPException(status_code=404, detail="用户不存在,或不能修改管理员账号。") return {"ok": True, "user": user} @app.get("/api/admin/activation-codes") def api_admin_activation_codes(admin: dict[str, str] = Depends(require_admin)) -> list[dict[str, object]]: return list_activation_codes() @app.post("/api/admin/activation-codes") def api_admin_create_activation_codes( request: GenerateActivationCodesRequest, admin: dict[str, str] = Depends(require_admin), ) -> dict[str, object]: if request.quantity < 1 or request.quantity > 100: raise HTTPException(status_code=400, detail="一次只能生成 1 到 100 个卡密。") codes: list[str] = [] while len(codes) < request.quantity: code = generate_activation_code() if code not in codes: codes.append(code) created = create_activation_codes(codes, admin["username"]) return {"ok": True, "codes": created} @app.delete("/api/admin/activation-codes/{code_id}") def api_admin_delete_activation_code( code_id: int, admin: dict[str, str] = Depends(require_admin), ) -> dict[str, object]: if not delete_unused_activation_code(code_id): raise HTTPException(status_code=400, detail="已使用的卡密不能删除。") return {"ok": True, "deletedCodeId": code_id} @app.post("/api/videos") async def api_create_video( admin: dict[str, str] = Depends(require_admin), title: str = Form(...), speaker: str = Form(""), description: str = Form(""), tags: str = Form(""), video_url: str = Form(""), video_file: UploadFile | None = File(None), cover_file: UploadFile | None = File(None), subtitle_en: UploadFile | None = File(None), subtitle_zh: UploadFile | None = File(None), ) -> dict[str, object]: clean_title = title.strip() if not clean_title: raise HTTPException(status_code=400, detail="标题不能为空") clean_video_url = video_url.strip() if video_file is None and not clean_video_url: raise HTTPException(status_code=400, detail="请上传视频文件或填写视频地址") if video_file is None and not is_supported_video_url(clean_video_url): raise HTTPException(status_code=400, detail="视频地址必须以 http:// 或 https:// 开头") if video_file is not None: if extension_for_upload(video_file) not in VIDEO_EXTENSIONS: raise HTTPException(status_code=400, detail="视频文件格式不支持") if cover_file is not None and extension_for_upload(cover_file) not in IMAGE_EXTENSIONS: raise HTTPException(status_code=400, detail="封面图片格式不支持") subtitle_uploads: list[tuple[UploadFile, str, str, str]] = [] for upload, language_code, language_name in ( (subtitle_en, "en", "English"), (subtitle_zh, "zh", "中文"), ): if upload is None: continue extension = extension_for_upload(upload) if extension not in SUBTITLE_EXTENSIONS: raise HTTPException(status_code=400, detail="字幕只支持 .srt 或 .vtt") subtitle_uploads.append((upload, language_code, language_name, extension)) saved_paths: list[Path] = [] try: final_video_url = clean_video_url if video_file is not None: video_filename = await save_binary_upload(video_file, UPLOADS_DIR / "videos") saved_paths.append(UPLOADS_DIR / "videos" / video_filename) final_video_url = f"/uploads/videos/{video_filename}" final_cover_url = "" if cover_file is not None: cover_filename = await save_binary_upload(cover_file, UPLOADS_DIR / "images") saved_paths.append(UPLOADS_DIR / "images" / cover_filename) final_cover_url = f"/uploads/images/{cover_filename}" subtitles: list[dict[str, str]] = [] for upload, language_code, language_name, extension in subtitle_uploads: subtitle_filename, subtitle_text = await save_upload(upload, UPLOADS_DIR / "subtitles") saved_paths.append(UPLOADS_DIR / "subtitles" / subtitle_filename) subtitles.append( { "language_code": language_code, "language_name": language_name, "format": extension.removeprefix("."), "file_path": f"/uploads/subtitles/{subtitle_filename}", "content": subtitle_text, } ) return create_video( title=clean_title, speaker=speaker.strip(), description=description.strip(), tags=parse_tags(tags), video_url=final_video_url, cover_url=final_cover_url, subtitles=subtitles, ) except Exception: cleanup_saved_files(saved_paths) raise @app.patch("/api/videos/{video_id}") async def api_update_video( video_id: int, admin: dict[str, str] = Depends(require_admin), title: str = Form(...), speaker: str = Form(""), description: str = Form(""), tags: str = Form(""), video_url: str = Form(""), subtitle_en_content: str | None = Form(None), subtitle_zh_content: str | None = Form(None), video_file: UploadFile | None = File(None), cover_file: UploadFile | None = File(None), subtitle_en: UploadFile | None = File(None), subtitle_zh: UploadFile | None = File(None), ) -> dict[str, object]: existing = get_video(video_id) if existing is None: raise HTTPException(status_code=404, detail="视频不存在") clean_title = title.strip() if not clean_title: raise HTTPException(status_code=400, detail="标题不能为空") clean_video_url = video_url.strip() final_video_url: str | None = None if video_file is None and clean_video_url: if not is_supported_video_url(clean_video_url) and not clean_video_url.startswith("/uploads/"): raise HTTPException(status_code=400, detail="视频地址必须以 http:// 或 https:// 开头") final_video_url = clean_video_url if video_file is not None and extension_for_upload(video_file) not in VIDEO_EXTENSIONS: raise HTTPException(status_code=400, detail="视频文件格式不支持") if cover_file is not None and extension_for_upload(cover_file) not in IMAGE_EXTENSIONS: raise HTTPException(status_code=400, detail="封面图片格式不支持") subtitle_uploads: list[tuple[UploadFile, str, str, str]] = [] for upload, language_code, language_name in ( (subtitle_en, "en", "English"), (subtitle_zh, "zh", "中文"), ): if upload is None: continue extension = extension_for_upload(upload) if extension not in SUBTITLE_EXTENSIONS: raise HTTPException(status_code=400, detail="字幕只支持 .srt 或 .vtt") subtitle_uploads.append((upload, language_code, language_name, extension)) saved_paths: list[Path] = [] try: if video_file is not None: video_filename = await save_binary_upload(video_file, UPLOADS_DIR / "videos") saved_paths.append(UPLOADS_DIR / "videos" / video_filename) final_video_url = f"/uploads/videos/{video_filename}" final_cover_url: str | None = None if cover_file is not None: cover_filename = await save_binary_upload(cover_file, UPLOADS_DIR / "images") saved_paths.append(UPLOADS_DIR / "images" / cover_filename) final_cover_url = f"/uploads/images/{cover_filename}" subtitles: list[dict[str, str]] = [] for upload, language_code, language_name, extension in subtitle_uploads: subtitle_filename, subtitle_text = await save_upload(upload, UPLOADS_DIR / "subtitles") saved_paths.append(UPLOADS_DIR / "subtitles" / subtitle_filename) subtitles.append( { "language_code": language_code, "language_name": language_name, "format": extension.removeprefix("."), "file_path": f"/uploads/subtitles/{subtitle_filename}", "content": subtitle_text, } ) uploaded_language_codes = {language_code for _, language_code, _, _ in subtitle_uploads} existing_subtitles = existing.get("subtitles", {}) for language_code, language_name, content in ( ("en", "English", subtitle_en_content), ("zh", "中文", subtitle_zh_content), ): if content is None or language_code in uploaded_language_codes: continue existing_subtitle = existing_subtitles.get(language_code, {}) subtitles.append( { "language_code": language_code, "language_name": existing_subtitle.get("languageName") or language_name, "format": existing_subtitle.get("format") or "vtt", "file_path": existing_subtitle.get("filePath") or "", "content": content, } ) updated = update_video( video_id, title=clean_title, speaker=speaker.strip(), description=description.strip(), tags=parse_tags(tags), video_url=final_video_url, cover_url=final_cover_url, subtitles=subtitles, ) if updated is None: cleanup_saved_files(saved_paths) raise HTTPException(status_code=404, detail="视频不存在") warnings = cleanup_upload_urls([str(path) for path in updated["oldUploadPaths"]]) return { "ok": True, "video": updated["video"], "warnings": warnings, } except HTTPException: cleanup_saved_files(saved_paths) raise except Exception: cleanup_saved_files(saved_paths) raise @app.delete("/api/videos/{video_id}") def api_delete_video(video_id: int, admin: dict[str, str] = Depends(require_admin)) -> dict[str, object]: deleted = delete_video(video_id) if deleted is None: raise HTTPException(status_code=404, detail="视频不存在") warnings = cleanup_upload_urls([str(path) for path in deleted["uploadPaths"]]) return { "ok": True, "deletedVideoId": deleted["deletedVideoId"], "warnings": warnings, } @app.get("/api/ai/status") def api_ai_status() -> dict[str, object]: return get_ai_status() @app.post("/api/ai/test") def api_ai_test(admin: dict[str, str] = Depends(require_admin)) -> dict[str, object]: status = get_ai_status() if not status["configured"]: raise HTTPException(status_code=400, detail=status["message"]) try: text_ai_config = load_text_ai_config() client = DeepSeekClient(text_ai_config, timeout_seconds=text_ai_config.timeout_seconds) client.chat_json( "请只返回 JSON:{\"ok\":true}", {"task": "ping", "expected": {"ok": True}}, ) except DeepSeekError as error: raise HTTPException(status_code=400, detail=str(error)) from error return {"ok": True, "message": "AI API 连接正常。"} @app.post("/api/videos/{video_id}/ai/subtitles") def api_process_video_subtitles(video_id: int, admin: dict[str, str] = Depends(require_admin)) -> dict[str, object]: status = get_ai_status() if not status["configured"]: raise HTTPException(status_code=400, detail=status["message"]) video = get_video(video_id) if video is None: raise HTTPException(status_code=404, detail="视频不存在") subtitles = video.get("subtitles", {}) if not isinstance(subtitles, dict): raise HTTPException(status_code=400, detail="字幕数据异常。") english_subtitle = subtitles.get("en", {}) if not isinstance(english_subtitle, dict) or not english_subtitle.get("content"): raise HTTPException(status_code=400, detail="请先上传英文字幕。") try: text_ai_config = load_text_ai_config() client = DeepSeekClient(text_ai_config, timeout_seconds=text_ai_config.timeout_seconds) result = process_subtitles_with_ai( str(english_subtitle["content"]), client, batch_size=text_ai_config.translation_batch_size, ) except (DeepSeekError, ValueError) as error: raise HTTPException(status_code=400, detail=str(error)) from error english_file_path = save_generated_subtitle_file(str(result["subtitle_en_content"]), "en") upsert_video_subtitle( video_id, language_code="en", language_name="English", format="vtt", file_path=english_file_path, content=str(result["subtitle_en_content"]), ) chinese_file_path = save_generated_subtitle_file(str(result["subtitle_zh_content"]), "zh") upsert_video_subtitle( video_id, language_code="zh", language_name="中文", format="vtt", file_path=chinese_file_path, content=str(result["subtitle_zh_content"]), ) return { "video": get_video(video_id), "report": result["report"], } def save_processed_subtitles(video_id: int, subtitles: dict[str, object], result: dict[str, object]) -> None: english_subtitle = subtitles.get("en", {}) english_file_path = "" if isinstance(english_subtitle, dict): english_file_path = str(english_subtitle.get("filePath") or "") upsert_video_subtitle( video_id, language_code="en", language_name="English", format="vtt", file_path=english_file_path, content=str(result["subtitle_en_content"]), ) chinese_subtitle = subtitles.get("zh", {}) chinese_file_path = "" if isinstance(chinese_subtitle, dict): chinese_file_path = str(chinese_subtitle.get("filePath") or "") upsert_video_subtitle( video_id, language_code="zh", language_name="中文", format="vtt", file_path=chinese_file_path, content=str(result["subtitle_zh_content"]), ) @app.post("/api/videos/{video_id}/local-whisper/subtitles") def api_generate_local_whisper_subtitles(video_id: int, admin: dict[str, str] = Depends(require_admin)) -> dict[str, object]: video = get_video(video_id) if video is None: raise HTTPException(status_code=404, detail="视频不存在") video_path = upload_url_to_path(str(video.get("videoUrl") or "")) if not video_path: raise HTTPException(status_code=400, detail="本地 Whisper 只能处理本地上传的视频文件。") try: whisper_config = load_local_whisper_config() english_vtt = transcribe_video_to_vtt(video_path, whisper_config) except LocalWhisperError as error: raise HTTPException(status_code=400, detail=str(error)) from error upsert_video_subtitle( video_id, language_code="en", language_name="English", format="vtt", file_path="", content=english_vtt, ) return { "video": get_video(video_id), "report": { "local_whisper_generated": True, "english_count": english_vtt.count("-->"), }, } @app.get("/") def read_index() -> FileResponse: return FileResponse(PROJECT_DIR / "index.html") app.mount("/", StaticFiles(directory=PROJECT_DIR), name="static")