hpds_jkw_web/internal/handler/file.go

92 lines
2.6 KiB
Go
Raw Normal View History

2023-01-11 18:05:29 +08:00
package handler
import (
"fmt"
"github.com/gin-gonic/gin"
"hpds-iot-web/internal/proto"
"hpds-iot-web/internal/service"
"hpds-iot-web/model"
e "hpds-iot-web/pkg/err"
2023-03-24 08:42:28 +08:00
"strconv"
2023-01-11 18:05:29 +08:00
)
func (s HandlerService) UploadFile(c *gin.Context) (data interface{}, err error) {
repo := service.NewFileService(s.AppConfig, s.Engine, s.Logger)
us, _ := c.Get("operatorUser")
userInfo := us.(*model.SystemUser)
2023-06-17 09:38:26 +08:00
var (
req proto.UploadFileRequest
scene string
datasetId int64
)
2023-01-11 18:05:29 +08:00
form, err := c.MultipartForm()
if err != nil {
go s.SaveLog("UploadFile", "Manage", "", "", userInfo.ToString(), fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "")
return nil, e.NewValidErr(err)
}
files := form.File["file"]
2023-03-24 08:42:28 +08:00
val := form.Value["scene"]
2023-06-17 09:38:26 +08:00
if len(val) > 0 {
scene = val[0]
} else {
scene = "other"
}
2023-01-11 18:05:29 +08:00
if len(scene) < 1 {
scene = "other"
}
2023-06-17 09:38:26 +08:00
if len(form.Value["datasetId"]) > 0 {
datasetId, _ = strconv.ParseInt(form.Value["datasetId"][0], 10, 64)
} else {
datasetId = 0
}
2023-01-11 18:05:29 +08:00
req = proto.UploadFileRequest{
2023-03-24 08:42:28 +08:00
Creator: userInfo.UserId,
Scene: scene,
Files: files,
DatasetId: datasetId,
2023-01-11 18:05:29 +08:00
}
data, err = repo.UploadFile(c, req)
go s.SaveLog("文件上传", "Manage", "", "", "", fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "")
return
}
func (s HandlerService) FileList(c *gin.Context) (data interface{}, err error) {
repo := service.NewFileService(s.AppConfig, s.Engine, s.Logger)
us, _ := c.Get("operatorUser")
userInfo := us.(*model.SystemUser)
var req proto.DatasetItemRequest
err = c.ShouldBindJSON(&req)
if err != nil {
go s.SaveLog("FileList", "FileManage", "", "", req.ToString(), fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "")
return nil, e.NewValidErr(err)
}
if req.Size < 1 {
req.Size = 20
}
if req.Size > 1000 {
req.Size = 1000
}
if req.Page < 1 {
req.Page = 1
}
data, err = repo.FileList(c, req)
go s.SaveLog("获取数据集详情", "FileManage", "", "", ToString(data), fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "")
return
}
func (s HandlerService) FileLabel(c *gin.Context) (data interface{}, err error) {
repo := service.NewFileService(s.AppConfig, s.Engine, s.Logger)
us, _ := c.Get("operatorUser")
userInfo := us.(*model.SystemUser)
var req proto.FileLabelRequest
err = c.ShouldBindJSON(&req)
if err != nil {
go s.SaveLog("FileLabel", "FileManage", "", "", ToString(req), fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "")
return nil, e.NewValidErr(err)
}
data, err = repo.FileLabel(c, req)
go s.SaveLog("标注文件", "FileManage", "", "", ToString(data), fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "")
return
}