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

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

	"github.com/lib/pq"
)

type DataSource int

const (
	DataSourceACD DataSource = iota
	DataSourceAniDB
)

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 {
	var big string
	exts := []string{"png", "jpg", "gif", "jpeg"}
	for _, ext := range exts {
		path := fmt.Sprintf("assets/img/gril/%d/0.%s", 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/1.%s", 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)
}