go_dreamfactory/modules/web/api_upload.go
2023-06-06 09:56:12 +08:00

76 lines
1.7 KiB
Go

package web
import (
"context"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/gin/engine"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"net/http"
"strconv"
)
//上传配置文件
func (this *Api_Comp) Upload(c *engine.Context) {
var (
code pb.ErrorCode
msg string
)
defer func() {
c.JSON(http.StatusOK, &Respond{
Code: code,
Message: msg,
})
}()
filenum := c.PostForm("filenum")
if filenum != "" {
if n, err := strconv.Atoi(filenum); err != nil {
this.module.Errorln(err)
msg = err.Error()
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
return
} else {
for i := 0; i < n; i++ {
if fn, err := c.FormFile(fmt.Sprintf("file_%d", i)); err != nil {
this.module.Errorln(err)
msg = err.Error()
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
return
} else {
if err = c.SaveUploadedFile(fn, fmt.Sprintf("%s/%s", configure.ConfigurePath(), fn.Filename)); err != nil {
this.module.Errorln(err)
msg = err.Error()
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
return
}
}
}
}
if err := this.module.service.RpcBroadcast(
context.Background(),
comm.Service_Worker,
string(comm.Rpc_ConfigureUpDate),
&pb.EmptyReq{},
&pb.EmptyResp{}); err != nil {
this.module.Errorln(err)
msg = err.Error()
code = pb.ErrorCode_RpcFuncExecutionError
}
} else {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
}
}