forked from koderover/zadig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
208 lines (186 loc) · 6.87 KB
/
Copy pathplugin.go
File metadata and controls
208 lines (186 loc) · 6.87 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
207
208
/*
Copyright 2025 The KodeRover Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package handler
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/koderover/zadig/v2/pkg/types"
commonmodels "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/system/service"
internalhandler "github.com/koderover/zadig/v2/pkg/shared/handler"
e "github.com/koderover/zadig/v2/pkg/tool/errors"
)
func ListPlugins(c *gin.Context) {
ctx := internalhandler.NewContext(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()
ctx.Resp, ctx.RespErr = service.ListPlugins(ctx.Logger)
}
func CreatePlugin(c *gin.Context) {
ctx, err := internalhandler.NewContextWithAuthorization(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()
if err != nil {
ctx.RespErr = fmt.Errorf("authorization Info Generation failed: err %s", err)
ctx.UnAuthorized = true
return
}
// accept multipart form (fields + file)
if !ctx.Resources.IsSystemAdmin {
ctx.UnAuthorized = true
return
}
contentType := c.Request.Header.Get("Content-Type")
if !strings.HasPrefix(strings.ToLower(contentType), "multipart/") {
ctx.RespErr = e.ErrInvalidParam.AddDesc("expect multipart/form-data with fields and file")
return
}
if err := c.Request.ParseMultipartForm(50 * 1024 * 1024); err != nil {
ctx.RespErr = e.ErrInvalidParam.AddDesc("invalid multipart form")
return
}
args := new(commonmodels.Plugin)
args.Name = c.PostForm("name")
if args.Name == "" {
ctx.RespErr = e.ErrInvalidParam.AddDesc("name is required")
return
}
args.Identifier = c.PostForm("identifier")
if idxStr := c.PostForm("index"); idxStr != "" {
if parsed, err := strconv.Atoi(idxStr); err == nil {
args.Index = parsed
}
}
args.Type = c.PostForm("type")
args.Description = c.PostForm("description")
args.Route = c.PostForm("route")
args.Enabled = c.PostForm("enabled") == "true"
// parse filters if provided
if filtersStr := c.PostForm("filters"); filtersStr != "" {
var filters []*commonmodels.PluginFilter
if err := json.Unmarshal([]byte(filtersStr), &filters); err != nil {
ctx.RespErr = e.ErrInvalidParam.AddDesc("invalid filters format, should be JSON array")
return
}
args.Filters = filters
}
file, header, err := c.Request.FormFile("file")
if err != nil {
ctx.RespErr = e.ErrInvalidParam.AddDesc("file is required")
return
}
defer file.Close()
// Service will upload and analyze the file; and persist the plugin
ctx.RespErr = service.CreatePluginWithFile(ctx.UserName, args, header, file, ctx.Logger)
meta := map[string]string{
"name": args.Name,
"type": args.Type,
"storage_id": args.StorageID,
}
metaBytes, _ := json.Marshal(meta)
internalhandler.InsertOperationLog(c, ctx.UserName, "", "新增", "系统设置-插件管理", fmt.Sprintf("name:%s", args.Name), string(metaBytes), types.RequestBodyTypeJSON, ctx.Logger)
}
func UpdatePlugin(c *gin.Context) {
ctx, err := internalhandler.NewContextWithAuthorization(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()
if err != nil {
ctx.RespErr = fmt.Errorf("authorization Info Generation failed: err %s", err)
ctx.UnAuthorized = true
return
}
if !ctx.Resources.IsSystemAdmin {
ctx.UnAuthorized = true
return
}
contentType := c.Request.Header.Get("Content-Type")
if strings.HasPrefix(strings.ToLower(contentType), "multipart/") {
// multipart update: fields + optional file
if err := c.Request.ParseMultipartForm(50 * 1024 * 1024); err != nil {
ctx.RespErr = e.ErrInvalidParam.AddDesc("invalid multipart form")
return
}
args := new(commonmodels.Plugin)
args.Name = c.PostForm("name")
args.Identifier = c.PostForm("identifier")
if idxStr := c.PostForm("index"); idxStr != "" {
if parsed, err := strconv.Atoi(idxStr); err == nil {
args.Index = parsed
}
}
args.Type = c.PostForm("type")
args.Description = c.PostForm("description")
args.Route = c.PostForm("route")
args.Enabled = c.PostForm("enabled") == "true"
// parse filters if provided
if filtersStr := c.PostForm("filters"); filtersStr != "" {
var filters []*commonmodels.PluginFilter
if err := json.Unmarshal([]byte(filtersStr), &filters); err != nil {
ctx.RespErr = e.ErrInvalidParam.AddDesc("invalid filters format, should be JSON array")
return
}
args.Filters = filters
}
// try file
file, header, err := c.Request.FormFile("file")
if err == nil {
defer file.Close()
ctx.RespErr = service.UpdatePluginWithFile(ctx.UserName, c.Param("id"), args, header, file, ctx.Logger)
} else {
ctx.RespErr = fmt.Errorf("file is required")
}
op := map[string]string{"id": c.Param("id"), "name": args.Name, "type": args.Type}
metaBytes, _ := json.Marshal(op)
internalhandler.InsertOperationLog(c, ctx.UserName, "", "更新", "系统设置-插件管理", fmt.Sprintf("id:%s name:%s", c.Param("id"), args.Name), string(metaBytes), types.RequestBodyTypeJSON, ctx.Logger)
return
} else {
ctx.RespErr = e.ErrInvalidParam.AddDesc(fmt.Sprintf("invalid content type: %s", contentType))
}
}
func DeletePlugin(c *gin.Context) {
ctx, err := internalhandler.NewContextWithAuthorization(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()
if err != nil {
ctx.RespErr = fmt.Errorf("authorization Info Generation failed: err %s", err)
ctx.UnAuthorized = true
return
}
internalhandler.InsertOperationLog(c, ctx.UserName, "", "删除", "系统设置-插件管理", fmt.Sprintf("id:%s", c.Param("id")), "", types.RequestBodyTypeJSON, ctx.Logger)
if !ctx.Resources.IsSystemAdmin {
ctx.UnAuthorized = true
return
}
ctx.RespErr = service.DeletePlugin(c.Param("id"), ctx.Logger)
}
// GetPluginFile downloads the file of the plugin by id
// NOTE: service logic is intentionally left to you, see the TODO in service.GetPluginFilePath
func GetPluginFile(c *gin.Context) {
ctx := internalhandler.NewContext(c)
id := c.Param("id")
if id == "" {
c.JSON(e.ErrorMessage(e.ErrInvalidParam.AddDesc("empty plugin id")))
c.Abort()
return
}
absolutePath, fileName, err := service.GetPluginFile(id, ctx.Logger)
if err != nil {
c.JSON(e.ErrorMessage(err))
c.Abort()
return
}
c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
c.Header("Content-Type", "application/octet-stream")
c.File(absolutePath)
}