Files
backend/backend/app/admin/model/file.py
2025-11-22 10:26:30 +08:00

29 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from datetime import datetime
from typing import Optional
from sqlalchemy import String, Text, DateTime, func, BigInteger, Index
from sqlalchemy.dialects.mysql import MEDIUMBLOB
from sqlalchemy.dialects.mysql import JSON as MySQLJSON # Changed from postgresql.JSONB to mysql.JSON
from sqlalchemy.orm import Mapped, mapped_column
from backend.common.model import Base, id_key, snowflake_id_key
class File(Base):
__tablename__ = 'file'
id: Mapped[snowflake_id_key] = mapped_column(BigInteger, init=False, primary_key=True)
file_hash: Mapped[str] = mapped_column(String(64), index=True, nullable=False) # SHA256哈希
file_name: Mapped[str] = mapped_column(String(255), nullable=False) # 原始文件名
content_type: Mapped[Optional[str]] = mapped_column(String(100), nullable=True) # MIME类型
file_size: Mapped[int] = mapped_column(BigInteger, nullable=False) # 文件大小(字节)
storage_path: Mapped[Optional[str]] = mapped_column(Text, nullable=True) # 存储路径(非数据库存储时使用)
file_data: Mapped[Optional[bytes]] = mapped_column(MEDIUMBLOB, default=None, nullable=True) # 文件二进制数据(数据库存储时使用)
storage_type: Mapped[str] = mapped_column(String(20), nullable=False, default='database') # 存储类型: database, local, s3
metadata_info: Mapped[Optional[dict]] = mapped_column(MySQLJSON, default=None, comment="元数据信息")
__table_args__ = (
Index('idx_file_name', file_name),
)