File size: 3,948 Bytes
8ad8368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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"),
    )