Repository navigation
Expand file tree
/
Copy pathmodels.go
More file actions
206 lines (177 loc) · 4.64 KB
/
Copy pathmodels.go
File metadata and controls
206 lines (177 loc) · 4.64 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package sal
import (
"bytes"
"database/sql"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/thisisfineio/sal/providers/aws/s3"
"github.com/aws/aws-sdk-go/aws"
"github.com/davecgh/go-spew/spew"
"github.com/gin-gonic/gin"
)
type Mapping struct {
AccountID string
AccountName string
BucketName string
ServiceProvider string
}
func (m *Mapping) ProxyManager() (ProxyManager, error) {
switch m.ServiceProvider {
case Amazon:
return &S3Proxy{}, nil
case Google:
return nil, errors.New("Google manager unimplemented")
default:
return nil, errors.New("Unrecognized ServiceProvider")
}
}
type Application struct {
ID int
ApiKey string
SecretKeyHash string
BucketMappings map[string]*Mapping
Name string
Description string
}
var (
applications = make(map[string]*Application)
)
var (
loaders []ApplicationMappingLoader
)
type JSONFileApplicationMapper struct {
path string
}
func (f *JSONFileApplicationMapper) LoadApplicationMappings() (map[string]*Application, error) {
data, err := ioutil.ReadFile(f.path)
if err != nil {
return nil, err
}
apps := make(map[string]*Application)
err = json.Unmarshal(data, &apps)
if err != nil {
return nil, err
}
return apps, nil
}
type InlineApplicationMapper struct{}
func (f *InlineApplicationMapper) LoadApplicationMappings() (map[string]*Application, error) {
apps := make(map[string]*Application)
apps["hsal"] = &Application{
ID: 1,
ApiKey: "THISISNOTAREALKEY",
SecretKeyHash: "THISISNOTAREALSECRETKEYHASH",
BucketMappings: map[string]*Mapping{
"sal.test": {
AccountID: "THISISNOTAREALACCOUNTID",
AccountName: "THISISNOTAREALACCOUNTNAME",
BucketName: "sal",
ServiceProvider: Amazon,
},
"sal-gifs": {
AccountID: "THISISNOTAREALACCOUNTID",
AccountName: "THISISNOTAREALACCOUNTNAME",
BucketName: "sal-gifs",
ServiceProvider: Amazon,
},
},
Name: "sal",
Description: "Storage Abstraction Layer",
}
return apps, nil
}
type DatabaseApplicationMapper struct {
db *sql.DB
}
func (d *DatabaseApplicationMapper) LoadApplicationMappings() (map[string]*Application, error) {
return nil, errors.New("Unimplemented")
}
func init() {
db, _ := sql.Open("mysql", "")
loaders = []ApplicationMappingLoader{
&DatabaseApplicationMapper{db: db},
&JSONFileApplicationMapper{path: "test"},
&InlineApplicationMapper{}}
for i, loader := range loaders {
apps, err := loader.LoadApplicationMappings()
if err != nil {
if i == len(loaders)-1 {
log.Fatal(err)
}
} else {
applications = apps
break
}
}
}
type S3Proxy struct{}
func (s *S3Proxy) HandleProxyDownload(mapping *Mapping, c *gin.Context) error {
b := s3.NewBucket(mapping.BucketName)
p, err := b.ListPath(strings.TrimLeft(c.Param(v1pathParamString), "/"))
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return errors.New("S3 Object not fonud")
} else {
if len(p.Contents) > 0 {
// choose the first result
fi := *p.Contents[0]
if *fi.Size > DownloadThreshold.Int64() {
// write to file and then stream the file
f, err := ioutil.TempFile("", "sal-tmp")
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return err
}
defer os.Remove(f.Name())
_, err = b.GetObjectAndWriteToWriter(f, *fi.Key)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return err
}
c.File(f.Name())
} else {
// store entirely in memory
buffer := aws.NewWriteAtBuffer([]byte{})
_, err := b.GetObjectAndWriteToWriter(buffer, *fi.Key)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return err
}
readSeeker := bytes.NewReader(buffer.Bytes())
c.Content(*fi.Key, *fi.LastModified, readSeeker)
}
} else {
c.AbortWithStatus(http.StatusNotFound)
return errors.New("S3 Object not found")
}
}
return nil
}
func (s *S3Proxy) HandleProxyUpload(mapping *Mapping, c *gin.Context) error {
//b := s3.NewBucket(app.BucketMappings[c.Param("bucket-name")].BucketName)
/*tmp, err := ioutil.TempFile("", "hsal-tmp")
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}*/
_, header, err := c.Request.FormFile("file")
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return err
}
spew.Dump(header)
c.Status(200)
return nil
}
type GoogleStorageProxy struct{}
func (g *GoogleStorageProxy) HandleProxyDownload(mapping *Mapping, c *gin.Context) error {
return nil
}
func (g *GoogleStorageProxy) HandleProxyUpload(mapping *Mapping, c *gin.Context) error {
return nil
}