-
Notifications
You must be signed in to change notification settings - Fork 0
/
oss_http.go
79 lines (72 loc) · 2.11 KB
/
oss_http.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
package v1beta
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/choral-io/gommerce-server-core/config"
"github.com/choral-io/gommerce-server-core/logging"
"github.com/choral-io/gommerce-server-core/server"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
type ObjectStoreService struct {
client *minio.Client
logger logging.Logger
}
func NewObjectStoreService(config config.ServerMinIOConfig, logger logging.Logger) (*ObjectStoreService, error) {
client, err := minio.New(config.GetEndpoint(), &minio.Options{
Creds: credentials.NewStaticV4(config.GetAccessKey(), config.GetSecretKey(), ""),
Secure: config.GetUseSSL(),
})
if err != nil {
return nil, err
}
return &ObjectStoreService{
client: client,
logger: logger,
}, nil
}
func (s *ObjectStoreService) ServerMuxRoutes() []server.ServerMuxRoute {
return []server.ServerMuxRoute{
{
Methods: []string{"PUT"},
Pattern: "/v1beta/objects/{bucket}/{object=**}",
Handler: s.PutObject,
},
}
}
func (s *ObjectStoreService) PutObject(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
file, header, err := r.FormFile("attachment")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
data := make([]byte, 512)
_, _ = file.Read(data)
_, _ = file.Seek(0, 0)
name := queryEscapePath(header.Filename)
path := queryEscapePath(pathParams["object"])
size := header.Size
if info, err := s.client.PutObject(r.Context(), pathParams["bucket"], path, file, size, minio.PutObjectOptions{
ContentDisposition: "attachment; filename=" + name,
ContentType: http.DetectContentType(data),
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.Header().Add("Location", fmt.Sprintf("%s/%s/%s", s.client.EndpointURL(), info.Bucket, queryEscapePath(info.Key)))
w.WriteHeader(http.StatusCreated)
}
}
func queryEscapePath(path string) string {
segs := strings.Split(path, "/")
var j int
for _, s := range segs {
u := strings.TrimSpace(s)
if len(u) > 0 {
segs[j] = url.QueryEscape(u)
j++
}
}
return strings.Join(segs[:j], "/")
}