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) }