-
Notifications
You must be signed in to change notification settings - Fork 33
/
showitem.go
134 lines (112 loc) · 3.78 KB
/
showitem.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
134
package handler
import (
"context"
"fmt"
"github.com/emicklei/go-restful/v3"
"github.com/opensource-f2f/open-podcasts/api/osf2f.my.domain/v1alpha1"
"github.com/opensource-f2f/open-podcasts/apiserver/server/common"
"io/ioutil"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"mime/multipart"
"net/http"
"os"
"path"
)
type ShowItem struct {
common.CommonOption
showitemPath *restful.Parameter
showQuery *restful.Parameter
}
func (r ShowItem) WebService() (ws *restful.WebService) {
ws = new(restful.WebService)
ws.Path("/showitems")
// set the parameters
r.showitemPath = restful.PathParameter("showitem", "")
r.showQuery = restful.QueryParameter("show", "The name of a show")
// set the routes
ws.Route(ws.POST("/").
To(r.create).
Reads(&v1alpha1.ShowItem{}).
Returns(http.StatusOK, "OK", []RSS{}))
ws.Route(ws.GET("/").
To(r.findAll).
Param(r.showQuery).
Returns(http.StatusOK, "OK", []v1alpha1.ShowItem{}))
ws.Route(ws.POST("/{showitem}/upload").
To(r.upload).
Consumes("multipart/form-data").
Returns(http.StatusOK, "OK", []RSS{}))
ws.Route(ws.GET("/{showitem}/download").
To(r.download).
Returns(http.StatusOK, "OK", []RSS{}))
return
}
func (r ShowItem) create(request *restful.Request, response *restful.Response) {
ctx := context.Background()
showitem := &v1alpha1.ShowItem{}
request.ReadEntity(showitem)
if len(showitem.Labels) == 0 {
showitem.Labels = map[string]string{}
}
showitem.Labels[v1alpha1.LabelKeyShowRef] = showitem.Spec.ShowRef
// calculate the next index
var err error
var showItemList *v1alpha1.ShowItemList
if showItemList, err = r.Client.Osf2fV1alpha1().ShowItems(r.DefaultNamespace).List(ctx, v1.ListOptions{
LabelSelector: fmt.Sprintf("rss=%s", showitem.Spec.ShowRef),
}); err == nil {
showitem.Spec.Index = len(showItemList.Items)
}
_, _ = r.Client.Osf2fV1alpha1().ShowItems(r.DefaultNamespace).Create(ctx, showitem, v1.CreateOptions{})
}
func (r ShowItem) findAll(request *restful.Request, response *restful.Response) {
ctx := context.Background()
showName := request.QueryParameter(r.showQuery.Data().Name)
labelSelector := ""
if showName != "" {
labelSelector = fmt.Sprintf("%s=%s", v1alpha1.LabelKeyShowRef, showName)
}
showItemList, _ := r.Client.Osf2fV1alpha1().ShowItems(r.DefaultNamespace).List(ctx, v1.ListOptions{
LabelSelector: labelSelector,
})
response.WriteAsJson(showItemList)
}
func (r ShowItem) upload(request *restful.Request, response *restful.Response) {
showItemName := request.PathParameter(r.showitemPath.Data().Name)
ctx := context.Background()
showItem, err := r.Client.Osf2fV1alpha1().ShowItems(r.DefaultNamespace).Get(ctx, showItemName, v1.GetOptions{})
httpReq := request.Request
err = httpReq.ParseMultipartForm(10 << 20)
for fName := range httpReq.MultipartForm.File {
var f multipart.File
f, _, err = httpReq.FormFile(fName)
defer func() {
_ = f.Close()
}()
var data []byte
data, err = ioutil.ReadAll(f)
filepath := path.Join(getTempDir(), showItem.Spec.Filename)
ioutil.WriteFile(filepath, data, 0644)
break
}
fmt.Println(err)
}
func (r ShowItem) download(request *restful.Request, response *restful.Response) {
showItemName := request.PathParameter(r.showitemPath.Data().Name)
ctx := context.Background()
showItem, err := r.Client.Osf2fV1alpha1().ShowItems(r.DefaultNamespace).Get(ctx, showItemName, v1.GetOptions{})
if err == nil {
filepath := path.Join(getTempDir(), showItem.Spec.Filename)
response.Header().Set("Content-Type", "application/octet-stream")
response.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", showItem.Spec.Filename))
data, err := ioutil.ReadFile(filepath)
if err == nil {
_, _ = response.Write(data)
} else {
_, _ = response.Write([]byte(err.Error()))
}
}
}
func getTempDir() string {
return os.TempDir()
}