-
Notifications
You must be signed in to change notification settings - Fork 2
/
snapshot.go
175 lines (152 loc) · 3.18 KB
/
snapshot.go
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package gosnap
import (
"errors"
"fmt"
"image"
"sort"
"strconv"
"time"
"github.com/ecwid/gosnap/registry"
)
const (
dataHash = "Hash"
keyX = "X"
keyY = "Y"
)
type Snapshot struct {
Value image.Image
Hash Hash
Metadata map[string]string
}
func atoi(value string) int {
i, err := strconv.Atoi(value)
if err != nil {
return 0
}
return i
}
func (b Snapshot) GetSize() (x int, y int) {
valueX, ok1 := b.Metadata[keyX]
valueY, ok2 := b.Metadata[keyY]
if ok1 && ok2 {
x = atoi(valueX)
y = atoi(valueY)
return x, y
}
if b.Value != nil {
point := b.Value.Bounds().Max
return point.X, point.Y
}
return 0, 0
}
func (s *Snapshot) decode(data registry.Object) error {
s.Metadata = data.Data
s.Hash = hashString(data.Data[dataHash])
var err error
if data.Body != nil {
s.Value, err = decodePng(data.Body)
if err != nil {
return errors.Join(errors.New("can't decode snapshot png"), err)
}
}
return err
}
func (b Snapshot) encode() (*registry.Object, error) {
var (
body []byte
err error
)
if b.Value != nil {
body, err = encodePng(b.Value)
if err != nil {
return nil, errors.Join(errors.New("can't encode snapshot png"), err)
}
x, y := b.GetSize()
b.Metadata[keyX] = fmt.Sprint(x)
b.Metadata[keyY] = fmt.Sprint(y)
}
data := ®istry.Object{
Body: body,
Data: b.Metadata,
}
data.Data[dataHash] = b.Hash.String()
return data, nil
}
func (s *Snapshot) Head(key string) error {
data, err := defaultRegistry.Head(key)
if err != nil {
return errors.Join(errors.New("can't pull snapshot"), err)
}
s.Metadata = data
s.Hash = hashString(data[dataHash])
return nil
}
func (s *Snapshot) Pull(key string) error {
obj, err := defaultRegistry.Pull(key)
if err != nil {
return errors.Join(errors.New("can't pull snapshot"), err)
}
return s.decode(*obj)
}
func (s Snapshot) Push(key string) error {
obj, err := s.encode()
if err != nil {
return err
}
if err = defaultRegistry.Push(key, *obj); err != nil {
return errors.Join(errors.New("can't push snapshot"), err)
}
return nil
}
type Approval struct {
Ts int64 `json:"ts"`
Hash Hash `json:"hash"`
Approver string `json:"approver"`
}
func (t Approval) Valid() bool {
return time.Unix(t.Ts, 0).AddDate(0, 2, 0).Compare(time.Now()) == 1
}
type Approvals struct {
Value []Approval
}
func (b *Approvals) Pull(key string) error {
return registry.Pull(defaultRegistry, key, &b.Value)
}
func (b Approvals) Push(key string) error {
return registry.Push(defaultRegistry, key, b.Value)
}
func (b *Approvals) sort() {
sort.Slice(b.Value, func(i, j int) bool {
return b.Value[i].Ts < b.Value[j].Ts
})
return
}
func (b *Approvals) decline(hash Hash) {
for n := range b.Value {
if b.Value[n].Hash.Equal(hash, 0) {
b.Value[n] = b.Value[len(b.Value)-1]
b.Value = b.Value[:len(b.Value)-1]
return
}
}
return
}
var MaxApprovals = 100
func (b *Approvals) accept(patch Approval) {
patch.Ts = getUnixTs()
// updating
for n, val := range b.Value {
if val.Hash.Equal(patch.Hash, 0) {
b.Value[n] = patch
return
}
}
// overflowed
if len(b.Value) >= MaxApprovals {
b.sort()
b.Value[0] = patch
return
}
// a new one
b.Value = append(b.Value, patch)
}