package model
import (
"context"
"time"
)
type Artwork struct {
Hash string `structs:"hash"`
Mime string `structs:"mime"`
Width int `structs:"width"`
Height int `structs:"height"`
SizeBytes int64 `structs:"size_bytes"`
BlurHash string `structs:"blur_hash"`
ThumbHash string `structs:"thumb_hash"`
DominantColor string `structs:"dominant_color"`
CreatedAt time.Time `structs:"created_at"`
}
const ImageTypePrimary = "primary"
const ArtworkSourceFailed = "!failed"
type ItemImage struct {
ImageHash string `structs:"-" json:"imageHash,omitempty"`
ImageAbsent bool `structs:"-" json:"imageAbsent,omitempty"`
BlurHash string `structs:"-" json:"-"`
ThumbHash string `structs:"-" json:"thumbHash,omitempty"`
DominantColor string `structs:"-" json:"dominantColor,omitempty"`
ImageWidth int `structs:"-" json:"imageWidth,omitempty"`
ImageHeight int `structs:"-" json:"imageHeight,omitempty"`
}
func (i ItemImage) AspectRatio() *float64 {
if i.ImageAbsent || i.ImageWidth <= 0 || i.ImageHeight <= 0 {
return nil
}
return new(float64(i.ImageWidth) / float64(i.ImageHeight))
}
type ItemArtwork struct {
ItemKind string `structs:"item_kind"`
ItemID string `structs:"item_id"`
ImageType string `structs:"image_type"`
Hash string `structs:"hash"`
Source string `structs:"source"`
SourcePath string `structs:"source_path"`
RefMtime int64 `structs:"ref_mtime"`
Trace string `structs:"trace"`
LastFailure string `structs:"last_failure"`
AttemptedAt time.Time `structs:"attempted_at"`
UpdatedAt time.Time `structs:"updated_at"`
}
type ItemArtworkInfo struct {
ItemID string
Hash string
BlurHash string
ThumbHash string
DominantColor string
Width int
Height int
}
func (i ItemArtworkInfo) Absent() bool { return i.Hash == "" }
func (i ItemArtworkInfo) Image() ItemImage {
return ItemImage{
ImageHash: i.Hash,
ImageAbsent: i.Absent(),
BlurHash: i.BlurHash,
ThumbHash: i.ThumbHash,
DominantColor: i.DominantColor,
ImageWidth: i.Width,
ImageHeight: i.Height,
}
}
type ArtworkQueueItem struct {
ItemKind string `structs:"item_kind"`
ItemID string `structs:"item_id"`
ImageType string `structs:"image_type"`
Priority int `structs:"priority"`
Attempts int `structs:"attempts"`
RetryAt time.Time `structs:"retry_at"`
EnqueuedAt time.Time `structs:"enqueued_at"`
Trace string `structs:"trace"`
}
const (
ArtworkPriorityRecheck = 0
ArtworkPriorityBackfill = 10
ArtworkPriorityScan = 50
ArtworkPriorityBump = 100
)
type ArtworkRepository interface {
GetImage(ctx context.Context, hash string) (*Artwork, error)
PutImage(ctx context.Context, a *Artwork) error
PurgeOrphans(ctx context.Context, createdBefore time.Time) (int64, error)
GetItemArtwork(ctx context.Context, kind Kind, id, imageType string) (*ItemArtwork, error)
PutItemArtwork(ctx context.Context, ia *ItemArtwork) error
PutLastFailure(ctx context.Context, kind Kind, id, imageType, trace string) error
DeleteForItems(ctx context.Context, kind Kind, ids []string) error
GetInfoForItems(ctx context.Context, kind Kind, ids []string) (map[string]ItemArtworkInfo, error)
GetMimeByHash(ctx context.Context) (map[string]string, error)
PurgeDanglingItems(ctx context.Context) (int64, error)
}
type ArtworkQueueRepository interface {
Get(ctx context.Context, kind Kind, id, imageType string) (*ArtworkQueueItem, error)
Enqueue(ctx context.Context, items ...ArtworkQueueItem) error
EnqueuePreservingBackoff(ctx context.Context, items ...ArtworkQueueItem) error
EnqueueAllMissing(ctx context.Context, kind Kind, priority int) (int64, error)
EnqueueIfMissing(ctx context.Context, items ...ArtworkQueueItem) error
CountBySource(ctx context.Context, kind Kind, sources []string) (int64, error)
SourcesInUse(ctx context.Context, kind Kind) ([]string, error)
EnqueueBySource(ctx context.Context, kind Kind, sources []string, priority int) (int64, error)
DequeueBatch(ctx context.Context, n int, kinds ...string) ([]ArtworkQueueItem, error)
MarkFailedIfUnchanged(ctx context.Context, kind, id, imageType string, seenRetryAt, retryAt time.Time, trace string) error
DeleteIfUnchanged(ctx context.Context, kind, id, imageType string, retryAt time.Time) error
Count(ctx context.Context) (int64, error)
CountQueued(ctx context.Context, kinds []Kind, priorities []int) ([]ArtworkQueueStat, error)
PurgeDangling(ctx context.Context) (int64, error)
PurgeQueued(ctx context.Context, kinds []Kind, priorities []int) (int64, error)
}
type ArtworkQueueStat struct {
ItemKind string
Priority int
Count int64
}