blob: 65c690fed42f8d05c722f8035031cfaaaaf3d8a6 (
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
|
package models
import (
"bytes"
"encoding/gob"
"fmt"
"git.capotej.com/capotej/communique/config"
"github.com/dgraph-io/badger/v3"
)
type Avatar struct {
Handler config.Handler
ContentType string
Bytes []byte
}
// used for lookup purposes (count, collect, find)
func NewAvatar(h config.Handler) *Avatar {
aso := &Avatar{Handler: h}
return aso
}
func CreateAvatar(h config.Handler, contentType string, bytes []byte) (*Avatar, error) {
aso := &Avatar{
Handler: h,
ContentType: contentType,
Bytes: bytes,
}
return aso, nil
}
func (a *Avatar) Name() string {
return "Avatar"
}
func (a *Avatar) Key() string {
keyBase := fmt.Sprintf("avatars:%s", a.Handler.Name)
return keyBase
}
func (a *Avatar) DedupKey() string {
return a.Key()
}
func (a *Avatar) Keybase() string {
return a.Key()
}
func (a *Avatar) SaveDedup(txn *badger.Txn) error {
txn.Discard() // nothing to do here
return nil
}
func (a *Avatar) Save(txn *badger.Txn) error {
if a.Bytes == nil {
return fmt.Errorf("bytes not set")
}
if a.ContentType == "" {
return fmt.Errorf("content type not set")
}
var network bytes.Buffer
enc := gob.NewEncoder(&network)
err := enc.Encode(a)
if err != nil {
return fmt.Errorf("could not encode Avatar: %w", err)
}
e := badger.NewEntry([]byte(a.Key()), network.Bytes())
return txn.SetEntry(e)
}
|