summaryrefslogtreecommitdiff
path: root/store/tag.go
blob: 5f717bdeeb3a53aef956c64e0d5eb66a7d1ddf93 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package store

import (
	"encoding/json"
	log "github.com/sirupsen/logrus"
	"os"
	"time"
)

// 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 {
		log.Fatalf("Error while reading tags, %s", err)
		os.Exit(1)
	} 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 {
		log.Fatalf("Error while reading tag %s, %s", tag, err)
		os.Exit(1)
	} 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 !s.dir(s.TagPath(tag)) {
		s.getLock("tag_" + tag).Lock()
		defer s.getLock("tag_" + tag).Unlock()
		if len(tag) > 128 || !nameRegex.MatchString(tag) {
			return false
		}
		if err := os.Mkdir(s.TagPath(tag), s.PermissionDir); err != nil {
			log.Fatalf("Error while creating tag %s, %s", tag, err)
			log.Exit(1)
		}
		if payload, err := json.Marshal(Tag{Type: Generic, CreationTime: time.Now().UTC()}); err != nil {
			log.Fatalf("Error while generating tag %s metadata, %s", tag, err)
			log.Exit(1)
		} else {
			if err = os.WriteFile(s.TagMetadataPath(tag), payload, s.PermissionFile); err != nil {
				log.Fatalf("Error while writing tag %s metadata, %s", tag, err)
				log.Exit(1)
			}
		}
		log.Infof("Tag %s created.", tag)
		return true
	}
	return true
}

// TagDestroy removes all references from a tag and removes it.
func (s *Store) TagDestroy(tag string) {
	if !nameRegex.MatchString(tag) || !s.dir(s.TagPath(tag)) {
		return
	}

	flakes := s.Tag(tag)
	for _, flake := range flakes {
		s.ImageTagRemove(flake, tag)
	}
	if err := os.Remove(s.TagMetadataPath(tag)); err != nil {
		log.Fatalf("Error removing tag %s metadata, %s", tag, err)
		os.Exit(1)
	}
	if err := os.Remove(s.TagPath(tag)); err != nil {
		log.Fatalf("Error removing tag %s, %s", tag, err)
		os.Exit(1)
	}
	log.Infof("Tag %s destroyed.", tag)
}

// TagInfo returns information of a tag.
func (s *Store) TagInfo(tag string) Tag {
	if !nameRegex.MatchString(tag) || !s.file(s.TagMetadataPath(tag)) {
		return Tag{}
	}

	s.getLock("tag_" + tag).RLock()
	defer s.getLock("tag_" + tag).RUnlock()
	if payload, err := os.ReadFile(s.TagMetadataPath(tag)); err != nil {
		log.Fatalf("Error while reading tag %s metadata, %s", tag, err)
		log.Exit(1)
	} else {
		var info Tag
		if err = json.Unmarshal(payload, &info); err != nil {
			log.Fatalf("Error while parsing tag %s metadata, %s", tag, err)
			log.Exit(1)
		} else {
			return info
		}
	}
	return Tag{}
}

// TagType sets type of a tag.
func (s *Store) TagType(tag, t string) {
	if !nameRegex.MatchString(tag) || !s.file(s.TagMetadataPath(tag)) {
		return
	}

	s.getLock("tag_" + tag).Lock()
	defer s.getLock("tag_" + tag).Unlock()

	if t != Artist &&
		t != Character &&
		t != Copyright &&
		t != Generic &&
		t != Group &&
		t != Meta {
		log.Warnf("Invalid tag change on tag %s, got %s, expecting {%s,%s,%s,%s,%s,%s}", tag, t, Artist, Character, Copyright, Generic, Group, Meta)
		return
	}
	info := s.TagInfo(tag)
	info.Type = t
	if payload, err := json.Marshal(info); err != nil {
		log.Fatalf("Error while updating tag %s metadata, %s", tag, err)
		log.Exit(1)
	} else {
		if err = os.WriteFile(s.TagMetadataPath(tag), payload, s.PermissionFile); err != nil {
			log.Fatalf("Error while writing tag %s metadata, %s", tag, err)
			log.Exit(1)
		}
	}
	log.Infof("Tag %s type set to %s.", tag, t)
}