from __future__ import annotations from datetime import datetime from typing import TYPE_CHECKING from sqlalchemy import BigInteger, ForeignKey, Index, String from sqlalchemy.orm import Mapped, mapped_column, relationship from app.models.base import Base, UTCDateTime, utcnow if TYPE_CHECKING: from app.models.clip import Clip class StoredFile(Base): """An uploaded file; belongs to exactly one clip of kind "file".""" __tablename__ = "files" __table_args__ = ( Index("ix_files_user", "user_id"), Index("ix_files_expires_at", "expires_at"), ) id: Mapped[int] = mapped_column(primary_key=True) clip_id: Mapped[int] = mapped_column(ForeignKey("clips.id", ondelete="CASCADE"), unique=True) user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE")) # Random name on disk; the original name never touches the file system storage_name: Mapped[str] = mapped_column(String(64), unique=True) name: Mapped[str] = mapped_column(String(255)) content_type: Mapped[str] = mapped_column(String(127)) size: Mapped[int] = mapped_column(BigInteger) sha256: Mapped[str] = mapped_column(String(64)) # Only set when the content really is a JPEG/PNG/GIF/WebP image (checked on upload) previewable: Mapped[bool] = mapped_column(default=False) expires_at: Mapped[datetime | None] = mapped_column(UTCDateTime) created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=utcnow) clip: Mapped[Clip] = relationship(back_populates="file")