Files
audio2text/app/models/upload_session.py
zikai 00e2a95fb7 Initial commit: audio2text 双语字幕生成服务
- 音频/视频转双语(英/中)SRT 字幕,Docker 容器化,CPU 开发/GPU 生产同一份代码
- faster-whisper ASR(词级时间戳) + 断句时间戳重算 + NLLB 翻译(模型不共驻)
- 分片上传(断点续传) + SQLite 持久化 + 主页/历史/日志页面
- 历史页文件名搜索;缓存定时清理(默认保留7天,可配置)
- 双 Dockerfile(cpu/gpu) + setup/start/stop 脚本
2026-07-06 06:54:19 +00:00

50 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""分片上传会话 ORM支撑断点续传。对齐 server 的 UploadSession 形态SQLite 版)。"""
from __future__ import annotations
import json
from datetime import datetime, timezone
from typing import Any
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, TypeDecorator
from sqlalchemy.orm import Mapped, mapped_column
from ..database import Base
def _now() -> datetime:
return datetime.now(timezone.utc)
class _IntList(TypeDecorator):
"""把 list[int] 存成 JSON 字符串。SQLite 没有 ARRAY 类型。"""
impl = Text
cache_ok = True
def process_bind_param(self, value: Any, dialect) -> str | None:
return json.dumps(value) if value is not None else None
def process_result_value(self, value: Any, dialect) -> list[int]:
return json.loads(value) if value else []
class UploadSession(Base):
__tablename__ = "upload_session"
upload_id: Mapped[str] = mapped_column(String(64), primary_key=True)
filename: Mapped[str] = mapped_column(String(512), nullable=False)
size_bytes: Mapped[int] = mapped_column(Integer, nullable=False)
chunk_size: Mapped[int] = mapped_column(Integer, nullable=False)
total_chunks: Mapped[int] = mapped_column(Integer, nullable=False)
uploaded_chunks: Mapped[list[int]] = mapped_column(_IntList, default=list)
# pending → completedcomplete 成功)| abandonedreaper 清理)
status: Mapped[str] = mapped_column(String(32), nullable=False, default="pending")
# 拼接完成后的视频相对 upload_dir 路径
final_path: Mapped[str | None] = mapped_column(String(1024), nullable=True)
# complete 后关联的 Task.id直接引用避免反向查找 source_path
task_id: Mapped[int | None] = mapped_column(ForeignKey("task.id"), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=_now)
updated_at: Mapped[datetime] = mapped_column(DateTime, default=_now, onupdate=_now)