63 lines
3.5 KiB
Python
63 lines
3.5 KiB
Python
from __future__ import annotations
|
|||
|
|
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
from sqlalchemy import BigInteger, Boolean, DateTime, ForeignKey, Index, Integer, JSON, 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
|
||
|
|
|
||
|
|
|
||
|
|
class Artwork(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
||
|
|
__tablename__ = "artworks"
|
||
|
|
__table_args__ = (
|
||
|
|
Index("ix_artworks_user_content_viewed", "user_id", "content_type", "last_viewed_at"),
|
||
|
|
Index("ix_artworks_user_created", "user_id", "created_at"),
|
||
|
|
Index("ix_artworks_user_created_desc", "user_id", "created_at"),
|
||
|
|
Index("ix_artworks_user_updated", "user_id", "updated_at"),
|
||
|
|
Index("ix_artworks_user_updated_desc", "user_id", "updated_at"),
|
||
|
|
Index("ix_artworks_user_content_created_desc", "user_id", "content_type", "created_at"),
|
||
|
|
Index("ix_artworks_user_content_updated_desc", "user_id", "content_type", "updated_at"),
|
||
|
|
Index("ix_artworks_user_favorite_viewed", "user_id", "is_favorite", "last_viewed_at"),
|
||
|
|
)
|
||
|
|
|
||
|
|
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
||
|
|
file_id: Mapped[str | None] = mapped_column(String(36), ForeignKey("user_files.id", ondelete="SET NULL"), nullable=True)
|
||
|
|
source_id: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||
|
|
source_platform: Mapped[str] = mapped_column(String(20), nullable=False, index=True)
|
||
|
|
content_type: Mapped[str] = mapped_column(String(20), default="illust", nullable=False, index=True)
|
||
|
|
title: Mapped[str] = mapped_column(String(500), nullable=False)
|
||
|
|
author: Mapped[str | None] = mapped_column(String(200), nullable=True, index=True)
|
||
|
|
author_id: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||
|
|
page_count: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
|
||
|
|
file_path: Mapped[str] = mapped_column(Text, nullable=False)
|
||
|
|
file_size: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False)
|
||
|
|
thumbnail_path: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||
|
|
mime_type: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||
|
|
width: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
|
|
height: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
|
|
tags: Mapped[list[str]] = mapped_column(JSON, default=list, nullable=False)
|
||
|
|
is_favorite: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, index=True)
|
||
|
|
artwork_metadata: Mapped[dict] = mapped_column("metadata", JSON, default=dict, nullable=False)
|
||
|
|
last_viewed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True, index=True)
|
||
|
|
|
||
|
|
user = relationship("User", back_populates="artworks")
|
||
|
|
file = relationship("UserFile")
|
||
|
|
ocr_text = relationship("ArtworkOcrText", back_populates="artwork", cascade="all, delete-orphan", uselist=False)
|
||
|
|
|
||
|
|
@property
|
||
|
|
def image_url(self) -> str | None:
|
||
|
|
if self.file_id:
|
||
|
|
return f"/api/v1/files/{self.file_id}/content"
|
||
|
|
if self.file_path.startswith(("http://", "https://")):
|
||
|
|
return self.file_path
|
||
|
|
return None
|
||
|
|
|
||
|
|
@property
|
||
|
|
def thumbnail_url(self) -> str | None:
|
||
|
|
if self.file_id:
|
||
|
|
return f"/api/v1/files/{self.file_id}/thumbnail"
|
||
|
|
if self.thumbnail_path and self.thumbnail_path.startswith(("http://", "https://")):
|
||
|
|
return self.thumbnail_path
|
||
|
|
return None
|