-
Notifications
You must be signed in to change notification settings - Fork 8
/
base.go
92 lines (74 loc) · 1.89 KB
/
base.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
package storage
import (
"errors"
"io"
"os"
"github.com/sirupsen/logrus"
"github.com/tokubai/kinu/config"
"github.com/tokubai/kinu/logger"
)
type Storage interface {
Open() error
Fetch(key string) (*Object, error)
PutFromBlob(key string, image []byte, contentType string, metadata map[string]string) error
Put(key string, imageFile io.ReadSeeker, contentType string, metadata map[string]string) error
List(key string) ([]StorageItem, error)
Move(from string, to string) error
}
type StorageItem interface {
Key() string
Filename() string
Extension() string
ImageSize() string
}
type Object struct {
Body []byte
Metadata map[string]string
}
var (
ErrImageNotFound = errors.New("not found requested image")
)
type ErrInvalidStorageOption struct {
error
Message string
}
func (e *ErrInvalidStorageOption) Error() string { return e.Message }
var (
AvailableStorageTypes = []string{"S3", "File"}
ErrUnknownStorage = errors.New("specify unknown storage.")
selectedStorageType string
)
func init() {
if config.BackwardCompatibleMode {
selectedStorageType = "BackwardCompatibleS3"
} else {
selectedStorageType = os.Getenv("KINU_STORAGE_TYPE")
if len(selectedStorageType) == 0 {
panic("must specify KINU_STORAGE_TYPE system environment.")
}
var isAvailableStorageType bool
for _, storageType := range AvailableStorageTypes {
if selectedStorageType == storageType {
isAvailableStorageType = true
}
}
if !isAvailableStorageType {
panic("unknown KINU_STORAGE_TYPE " + selectedStorageType + ".")
}
}
logger.WithFields(logrus.Fields{
"storage_type": selectedStorageType,
}).Info("setup storage")
}
func Open() (Storage, error) {
switch selectedStorageType {
case "S3":
return openS3Storage()
case "File":
return openFileStorage()
case "BackwardCompatibleS3":
return openBackwardCompatibleS3Storage()
default:
return nil, ErrUnknownStorage
}
}