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
|
package imagestore
import (
"io/ioutil"
"log"
"os"
"mandible/config"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/s3"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
gcloud "google.golang.org/cloud"
gcs "google.golang.org/cloud/storage"
)
type Factory struct {
conf *config.Configuration
}
func NewFactory(conf *config.Configuration) *Factory {
return &Factory{conf}
}
func (this *Factory) NewImageStores() []ImageStore {
stores := []ImageStore{}
for _, configWrapper := range this.conf.Stores {
switch configWrapper["Type"] {
case "s3":
store := this.NewS3ImageStore(configWrapper)
stores = append(stores, store)
case "gcs":
store := this.NewGCSImageStore(configWrapper)
stores = append(stores, store)
case "local":
store := this.NewLocalImageStore(configWrapper)
stores = append(stores, store)
default:
log.Fatal("Unsupported store %s", configWrapper["Type"])
}
}
return stores
}
func (this *Factory) NewS3ImageStore(conf map[string]string) ImageStore {
bucket := os.Getenv("S3_BUCKET")
if len(bucket) == 0 {
bucket = conf["BucketName"]
}
auth, err := aws.EnvAuth()
if err != nil {
log.Fatal(err)
}
client := s3.New(auth, aws.Regions[conf["Region"]])
mapper := NewNamePathMapper(conf["NamePathRegex"], conf["NamePathMap"])
return NewS3ImageStore(
bucket,
conf["StoreRoot"],
client,
mapper,
)
}
func (this *Factory) NewGCSImageStore(conf map[string]string) ImageStore {
jsonKey, err := ioutil.ReadFile(conf["KeyFile"])
if err != nil {
log.Fatal(err)
}
cloudConf, err := google.JWTConfigFromJSON(
jsonKey,
gcs.ScopeFullControl,
)
if err != nil {
log.Fatal(err)
}
bucket := os.Getenv("GCS_BUCKET")
if len(bucket) == 0 {
bucket = conf["BucketName"]
}
ctx := gcloud.NewContext(conf["AppID"], cloudConf.Client(oauth2.NoContext))
mapper := NewNamePathMapper(conf["NamePathRegex"], conf["NamePathMap"])
return NewGCSImageStore(
ctx,
bucket,
conf["StoreRoot"],
mapper,
)
}
func (this *Factory) NewLocalImageStore(conf map[string]string) ImageStore {
mapper := NewNamePathMapper(conf["NamePathRegex"], conf["NamePathMap"])
return NewLocalImageStore(conf["StoreRoot"], mapper)
}
func (this *Factory) NewStoreObject(name string, mime string, imgType string) *StoreObject {
return &StoreObject{
Name: name,
MimeType: mime,
Type: imgType,
}
}
func (this *Factory) NewHashGenerator(store ImageStore) *HashGenerator {
hashGen := &HashGenerator{
make(chan string),
this.conf.HashLength,
store,
}
hashGen.init()
return hashGen
}
|