import enum from sqlalchemy import ( Boolean, CheckConstraint, Column, DateTime, ForeignKey, Index, Integer, Numeric, String, Text, func, ) from sqlalchemy.orm import backref, relationship from app.db.base import Base class AssetCategory(str, enum.Enum): PRESETS = "presets" LUTS = "luts" TRANSITIONS = "transitions" OVERLAYS = "overlays" SFX = "sfx" PROJECT_FILES = "project_files" class AssetPreviewType(str, enum.Enum): BEFORE_AFTER = "before_after" VIDEO = "video" AUDIO = "audio" IMAGE = "image" class Asset(Base): """ A downloadable file in the free asset library (presets, LUTs, transitions, overlays, SFX, project files). `file_key` is the S3 key of the downloadable file and is NEVER exposed on a public response — clients must go through POST /assets/{id}/download to get a short-lived signed URL. `preview_key` is safe to sign publicly. """ __tablename__ = "assets" id = Column(Integer, primary_key=True, autoincrement=True) title = Column(String(200), nullable=False) description = Column(Text, nullable=True) category = Column(String(50), nullable=False, index=True) owner_id = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True, index=True) # Downloadable object — private, never returned by public endpoints. file_key = Column(String(500), nullable=False) # Public-facing sample (image/video/audio/before-after strip). preview_key = Column(String(500), nullable=True) preview_type = Column(String(50), nullable=True) format = Column(String(50), nullable=True) # e.g. "cube", "zip", "wav", "prproj" size_bytes = Column(Integer, nullable=True) license_note = Column(Text, nullable=True) # Kept so paid/seller uploads can ship later without reshaping the table. is_free = Column(Boolean, default=True, nullable=False, server_default="true") price = Column(Numeric(12, 2), nullable=True) download_count = Column(Integer, default=0, nullable=False, server_default="0") is_published = Column(Boolean, default=True, nullable=False, server_default="true", index=True) created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False, index=True) owner = relationship("User", backref=backref("assets", lazy="dynamic")) downloads = relationship( "AssetDownload", back_populates="asset", cascade="all, delete-orphan", ) __table_args__ = ( CheckConstraint( "category IN ('presets', 'luts', 'transitions', 'overlays', 'sfx', 'project_files')", name="ck_assets_category", ), CheckConstraint( "preview_type IS NULL OR preview_type IN ('before_after', 'video', 'audio', 'image')", name="ck_assets_preview_type", ), CheckConstraint("download_count >= 0", name="ck_assets_download_count"), Index("ix_assets_published_category", "is_published", "category"), ) class AssetDownload(Base): """ One row per download event. Feeds repeat-downloader and download-then-upload metrics. """ __tablename__ = "asset_downloads" id = Column(Integer, primary_key=True, autoincrement=True) user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False) asset_id = Column(Integer, ForeignKey("assets.id", ondelete="CASCADE"), nullable=False) created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False) user = relationship("User", backref=backref("asset_downloads", lazy="dynamic")) asset = relationship("Asset", back_populates="downloads") __table_args__ = ( Index("ix_asset_downloads_user_asset", "user_id", "asset_id"), Index("ix_asset_downloads_created_at", "created_at"), Index("ix_asset_downloads_asset_id", "asset_id"), )