Select Git revision
tag.go 4.53 KiB
package store
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"os"
"time"
)
const (
// ArtistType is the tag type artist.
ArtistType = "artist"
// CharacterType is the tag type character.
CharacterType = "character"
// CopyrightType is the tag type copyright.
CopyrightType = "copyright"
// MetaType is the tag type meta.
MetaType = "meta"
// GenericType is the tag type generic.
GenericType = "generic"
)
var (
// AllowedTagTypes represent tag type strings that are allowed.
AllowedTagTypes = []string{ArtistType, CharacterType, CopyrightType, MetaType, GenericType}
allowedTagTypesMap = map[string]bool{
ArtistType: true,
CharacterType: true,
CopyrightType: true,
MetaType: true,
GenericType: true,
}
)
// TagTypeAllowed returns whether str is an allowed tag type.
func TagTypeAllowed(str string) bool {
return allowedTagTypesMap[str]
}
// Tag represents metadata of a tag.
type Tag struct {
Type string `json:"type"`
CreationTime time.Time `json:"creation_time"`
}
// Tags returns a slice of tag names.
func (s *Store) Tags() []string {
var tags []string
if entries, err := os.ReadDir(s.TagsDir()); err != nil {
s.fatalClose(fmt.Sprintf("Error reading tags, %s", err))
} else {
for _, entry := range entries {
if entry.IsDir() {
tags = append(tags, entry.Name())
}
}
}
return tags
}
// Tag returns a slice of image snowflakes in a specific tag.
func (s *Store) Tag(tag string) []string {
if !nameRegex.MatchString(tag) || !s.dir(s.TagPath(tag)) {
return nil
}
var images []string
if entries, err := os.ReadDir(s.TagPath(tag)); err != nil {
s.fatalClose(fmt.Sprintf("Error reading tag %s, %s", tag, err))
} else {