from __future__ import annotations from datetime import datetime from sqlalchemy import BigInteger, DateTime, ForeignKey, Index, String, Text from sqlalchemy.orm import Mapped, mapped_column, relationship from sakura_cloud.db.base import Base from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin, utc_now class FileBlob(Base): __tablename__ = "file_blobs" sha256: Mapped[str] = mapped_column(String(64), primary_key=True) storage_path: Mapped[str] = mapped_column(Text, nullable=False) thumbnail_path: Mapped[str | None] = mapped_column(Text, nullable=True) file_size: Mapped[int] = mapped_column(BigInteger, nullable=False) mime_type: Mapped[str | None] = mapped_column(String(50), nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, nullable=False) user_files = relationship("UserFile", back_populates="blob") class UserFile(UUIDPrimaryKeyMixin, Base): __tablename__ = "user_files" user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False) file_sha256: Mapped[str] = mapped_column(String(64), ForeignKey("file_blobs.sha256", ondelete="CASCADE"), nullable=False) original_name: Mapped[str] = mapped_column(String(255), nullable=False) uploaded_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, nullable=False) user = relationship("User", back_populates="files") blob = relationship("FileBlob", back_populates="user_files") __table_args__ = (Index("idx_user_files_user_id", "user_id"),) class UploadSession(UUIDPrimaryKeyMixin, TimestampMixin, Base): __tablename__ = "upload_sessions" user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False) sha256: Mapped[str] = mapped_column(String(64), nullable=False, index=True) file_name: Mapped[str] = mapped_column(String(255), nullable=False) file_size: Mapped[int] = mapped_column(BigInteger, nullable=False) mime_type: Mapped[str | None] = mapped_column(String(50), nullable=True) resource_type: Mapped[str] = mapped_column(String(20), default="image", nullable=False) chunk_size: Mapped[int] = mapped_column(BigInteger, nullable=False) received_size: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False) temp_path: Mapped[str] = mapped_column(Text, nullable=False) status: Mapped[str] = mapped_column(String(20), default="pending", nullable=False) user = relationship("User") __table_args__ = (Index("idx_upload_sessions_user_id", "user_id"),)