/
store.go
133 lines (114 loc) · 3.08 KB
/
store.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
package store
import (
"bytes"
"encoding/hex"
"io"
"strconv"
"strings"
"time"
"github.com/timemore/foundation/errors"
"github.com/timemore/foundation/media"
"golang.org/x/crypto/blake2b"
)
type Store struct {
config Config
serviceClient Service
}
type Object interface {
io.Reader
io.Seeker
io.WriterAt
}
func New(config Config) (*Store, error) {
if len(config.Modules) == 0 {
return nil, errors.ArgMsg("config.Modules", "empty")
}
if config.StoreService == "" {
return nil, errors.ArgMsg("config.StoreService", "empty")
}
modsAvailable := ModuleNames()
modCfg := config.Modules[config.StoreService]
if modCfg == nil {
return nil, errors.ArgMsg("config.StoreService", config.StoreService+" not configured, availbale StoreService name : "+strings.Join(modsAvailable, ","))
}
serviceClient, err := NewServiceClient(config.StoreService, modCfg)
if err != nil {
return nil, errors.ArgWrap("config.StoreService", config.StoreService+" initialization failed", err)
}
return &Store{
config: config,
serviceClient: serviceClient,
}, nil
}
func (mediaStore *Store) Upload(
mediaName string,
contentSource io.Reader,
mediaType media.MediaType,
) (uploadInfo *UploadInfo, err error) {
uploadInfo, err = mediaStore.serviceClient.PutObject(mediaName, contentSource)
if err != nil {
return nil, errors.Wrap("putting object", err)
}
tNow := time.Now().UTC()
if uploadInfo.LastModified.IsZero() {
uploadInfo.LastModified = tNow
}
return uploadInfo, nil
}
func (mediaStore *Store) GetPublicURL(sourceKey string) (publicURL string, err error) {
return mediaStore.serviceClient.GetPublicObject(sourceKey)
}
func (mediaStore *Store) Download(sourceKey string) (buffer *bytes.Buffer, err error) {
return mediaStore.serviceClient.GetObject(sourceKey)
}
const nameGenHashLength = 16
const nameGenKeyDefault = "N0k3y"
// GenerateName is used to generate a name, for file or other identifier
// based on the content. It utilized hash so the result could be used to
// prevent duplicates when storing the media object
func (mediaStore *Store) GenerateName(stream io.Reader) string {
keyBytes := []byte(mediaStore.config.NameGenerationKey)
if len(keyBytes) == 0 {
keyBytes = []byte(nameGenKeyDefault)
}
hasher, err := blake2b.New(nameGenHashLength, keyBytes)
if err != nil {
panic(err)
}
buf := make([]byte, 1*1024*1024)
dataSize := 0
for {
n, err := stream.Read(buf)
if err != nil {
if err != io.EOF {
panic(err)
}
}
dataSize += n
hasher.Write(buf)
if err == io.EOF || n == 0 {
break
}
}
hashBytes := hasher.Sum(nil)
encodedHash := hex.EncodeToString(hashBytes) + "K" + hex.EncodeToString(keyBytes[:4]) + "N" + strconv.FormatInt(int64(dataSize), 16)
return encodedHash
}
func ContentTypeInList(contentType string, contentTypeList []string) bool {
for _, ct := range contentTypeList {
if ct == contentType {
return true
}
}
return false
}
type UploadInfo struct {
Bucket string
ETag string
Key string
LastModified time.Time
Location string
Output *bytes.Buffer
Size int
UploadID string
}