Skip to content
Snippets Groups Projects
Select Git revision
  • f24c20c1597d8cd0ce1c08a1fec635c5adc9f209
  • main default protected
  • devcontainer-support
  • print-signal
  • v0.0.6
  • v0.0.5
  • v0.0.4
  • v0.0.3
  • v0.0.2
  • v0.0.1
10 results

app.go

Blame
  • tag.go 4.22 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"
    )
    
    // 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 {
    		for _, entry := range entries {
    			if entry.Name() == infoJson {
    				continue
    			}
    			images = append(images, entry.Name())
    		}
    	}
    	return images
    }
    
    // TagCreate creates a tag and returns ok value.
    func (s *Store) TagCreate(tag string) bool {
    	if len(tag) > 128 || !nameRegex.MatchString(tag) {
    		return false
    	}
    	if !s.dir(s.TagPath(tag)) {
    		s.getLock("tag_" + tag).Lock()