Skip to content
Snippets Groups Projects
Select Git revision
  • 76e7c3f0686b5f47bfb0058df62a327d87a08727
  • master default protected
  • v1.4.5
  • v1.4.4
  • v1.4.3
  • v1.4.2
  • v1.4.1
  • v1.4.0
  • v1.3.10
  • v1.3.9
  • v1.3.8
  • v1.3.7
  • v1.3.6
  • v1.3.5
  • v1.3.4
  • v1.3.3
  • v1.3.2
  • v1.3.1
  • v1.3.0
  • v1.2.9
  • v1.2.8
  • v1.2.7
22 results

tag.go

Blame
  • 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 {