aboutsummaryrefslogtreecommitdiff
path: root/modules/grils/gril.go
blob: 7b782b3daddf152a2122ae913849b5965a306c1c (plain)
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
package grils

import (
	"database/sql"
	"fmt"
	"os"
	"strconv"
	"strings"
	"time"

	"github.com/lib/pq"
)

type DataSource int

const (
	DataSourceACD DataSource = iota
	DataSourceAniDB
	DataSourceAnilist
)

type Gril struct {
	ID         int
	KanjiName  string
	RomajiName string
	OtherNames []string
	Age        sql.NullInt64
	Birthday   pq.NullTime
	Tags       []string
	ForeignIDs map[DataSource]int
	UpdatedAt  time.Time
	Lists      []int
}

func (g *Gril) Slug() string {
	if g.RomajiName == "" {
		return strconv.Itoa(g.ID)
	}
	return fmt.Sprintf(
		"%d/%s",
		g.ID,
		strings.Replace(g.RomajiName, " ", "", -1))
}

func ImagePath(id int, prioritizeThumbnail bool) string {
	// Image Priority
	// without prioritizeThumbnail: Anilist > ACD (big) > ACD (thumbnail)
	// with prioritizeThumbnail: Anilist > ACD (thumbnail) > ACD (big)
	
	anilistPath := fmt.Sprintf("assets/img/gril/%d/%d.jpg", DataSourceAnilist, id)
	if _, err := os.Stat(anilistPath); err == nil {
		return fmt.Sprintf(anilistPath)
	}
	
	var big string
	exts := []string{"png", "jpg", "gif", "jpeg"}
	for _, ext := range exts {
		path := fmt.Sprintf("assets/img/gril/%d/%d/0.%s", DataSourceACD, id, ext)
		if _, err := os.Stat(path); err == nil {
			if prioritizeThumbnail {
				big = path
			} else {
				return path
			}
		}
	}

	for _, ext := range exts {
		path := fmt.Sprintf("assets/img/gril/%d/%d/1.%s", DataSourceACD, id, ext)
		if _, err := os.Stat(path); err == nil {
			return path
		}
	}

	return big
}

func (g *Gril) ImagePath(prioritizeThumbnail bool) string {
	return ImagePath(g.ID, prioritizeThumbnail)
}