MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / UploadCommandCsv

Method UploadCommandCsv

core/app/api/v2/command.go:22–63  ·  view source on GitHub ↗

@Tags Command @Summary Upload command csv for list @Success 200 {string} path @Security ApiKeyAuth @Security Timestamp @Router /core/commands/upload [post] @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"上传快速命令文件","formatEN":"upload quick commands with csv"}

(c *gin.Context)

Source from the content-addressed store, hash-verified

20// @Router /core/commands/upload [post]
21// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"上传快速命令文件","formatEN":"upload quick commands with csv"}
22func (b *BaseApi) UploadCommandCsv(c *gin.Context) {
23 form, err := c.MultipartForm()
24 if err != nil {
25 helper.BadRequest(c, err)
26 return
27 }
28 files := form.File["file"]
29 if len(files) == 0 {
30 helper.BadRequest(c, errors.New("no such files"))
31 return
32 }
33 uploadFile, _ := files[0].Open()
34 reader := csv.NewReader(uploadFile)
35 if _, err := reader.Read(); err != nil {
36 helper.BadRequest(c, fmt.Errorf("read title failed, err: %v", err))
37 return
38 }
39 groupRepo := repo.NewIGroupRepo()
40 group, _ := groupRepo.Get(repo.WithByType("command"), groupRepo.WithByDefault(true))
41 var commands []dto.CommandInfo
42 for {
43 record, err := reader.Read()
44 if err == io.EOF {
45 break
46 }
47 if err != nil {
48 helper.BadRequest(c, fmt.Errorf("read content failed, err: %v", err))
49 return
50 }
51 if len(record) >= 2 {
52 commands = append(commands, dto.CommandInfo{
53 Name: record[0],
54 Type: "command",
55 GroupID: group.ID,
56 Command: record[1],
57 GroupBelong: group.Name,
58 })
59 }
60 }
61
62 helper.SuccessWithData(c, commands)
63}
64
65// @Tags Command
66// @Summary Export command

Callers

nothing calls this directly

Calls 4

GetMethod · 0.95
WithByDefaultMethod · 0.95
WithByTypeMethod · 0.65
ReadMethod · 0.45

Tested by

no test coverage detected