1、修改文件上传

This commit is contained in:
wangjian 2023-03-24 08:42:28 +08:00
parent 93405fe90a
commit 62b6338613
8 changed files with 48 additions and 15 deletions

View File

@ -20,6 +20,7 @@ minio:
endpoint: 127.0.0.1:9000
accessKeyId: root
secretAccessKey: OIxv7QptYBO3
bucket: jky-data
consul:
host: http://consul.hpds.cc
port: 80

View File

@ -7,6 +7,7 @@ import (
"hpds-iot-web/internal/service"
"hpds-iot-web/model"
e "hpds-iot-web/pkg/err"
"strconv"
)
func (s HandlerService) UploadFile(c *gin.Context) (data interface{}, err error) {
@ -20,14 +21,17 @@ func (s HandlerService) UploadFile(c *gin.Context) (data interface{}, err error)
return nil, e.NewValidErr(err)
}
files := form.File["file"]
scene := c.GetString("scene")
val := form.Value["scene"]
scene := val[0]
if len(scene) < 1 {
scene = "other"
}
datasetId, _ := strconv.ParseInt(form.Value["datasetId"][0], 10, 64)
req = proto.UploadFileRequest{
Creator: userInfo.UserId,
Scene: scene,
Files: files,
Creator: userInfo.UserId,
Scene: scene,
Files: files,
DatasetId: datasetId,
}
data, err = repo.UploadFile(c, req)
go s.SaveLog("文件上传", "Manage", "", "", "", fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "")

View File

@ -408,9 +408,10 @@ func (p DeviceItemRequest) ToString() string {
}
type UploadFileRequest struct {
Creator int64 `json:"creator"`
Scene string `json:"scene"`
Files []*multipart.FileHeader `json:"files"`
Creator int64 `json:"creator"`
Scene string `json:"scene"`
Files []*multipart.FileHeader `json:"files"`
DatasetId int64 `json:"datasetId"`
}
type BrandRequest struct {

View File

@ -65,6 +65,7 @@ type DatasetItem struct {
Creator int64 `json:"creator"`
CreateAt int64 `json:"createAt"`
DatasetCount int64 `json:"datasetCount"`
DatasetSize int64 `json:"datasetSize"`
}
type NodeState struct {

View File

@ -97,11 +97,16 @@ func (rp *repo) DatasetList(ctx context.Context, req proto.DatasetRequest) (rsp
}
data := make([]proto.DatasetItem, len(list))
for k, v := range list {
detailList := make([]model.DatasetDetail, 0)
detailList := make([]model.FileManager, 0)
datasetCount, err := rp.engine.Where("dataset_id = ?", v.DatasetId).FindAndCount(&detailList)
if err != nil {
goto ReturnPoint
}
fm := new(model.FileManager)
datasetSize, err := rp.engine.Where("dataset_id = ?", v.DatasetId).SumInt(fm, "file_size")
if err != nil {
goto ReturnPoint
}
data[k] = proto.DatasetItem{
DatasetId: v.DatasetId,
DatasetName: v.DatasetName,
@ -113,6 +118,7 @@ func (rp *repo) DatasetList(ctx context.Context, req proto.DatasetRequest) (rsp
Creator: v.Creator,
CreateAt: v.CreateAt,
DatasetCount: datasetCount,
DatasetSize: datasetSize,
}
}
rsp.Code = http.StatusOK

View File

@ -22,7 +22,7 @@ import (
type FileService interface {
UploadFile(ctx context.Context, req proto.UploadFileRequest) (rsp *proto.BaseResponse, err error)
UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHeader, scene string, creator int64) (data *model.FileManager, err error)
UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHeader, scene string, datasetId, creator int64) (data *model.FileManager, err error)
}
func NewFileService(cfg *config.WebConfig, engine *xorm.Engine, logger *logging.Logger) FileService {
@ -46,12 +46,13 @@ func (rp *repo) UploadFile(ctx context.Context, req proto.UploadFileRequest) (rs
default:
list := make([]*model.FileManager, len(req.Files))
fileUrl := make([]string, len(req.Files))
for k, _ := range req.Files {
fileItem, err := rp.UploadFileToMinIo(ctx, req.Files[k], req.Scene, req.Creator)
for k := range req.Files {
fileItem, err := rp.UploadFileToMinIo(ctx, req.Files[k], req.Scene, req.DatasetId, req.Creator)
if err != nil {
goto ReturnPoint
}
list[k] = fileItem
list[k].DatasetId = req.DatasetId
fileUrl[k] = fileItem.AccessUrl
}
_, err = rp.engine.Insert(list)
@ -75,9 +76,11 @@ ReturnPoint:
return rsp, err
}
func (rp *repo) UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHeader, scene string, creator int64) (data *model.FileManager, err error) {
func (rp *repo) UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHeader, scene string, datasetId, creator int64) (data *model.FileManager, err error) {
file, err := srcFile.Open()
defer file.Close()
defer func(file multipart.File) {
_ = file.Close()
}(file)
if err != nil {
return nil, err
}
@ -92,9 +95,21 @@ func (rp *repo) UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHe
if err != nil {
return nil, err
}
//bucketName := fmt.Sprintf("jky-data/%s/%s", scene, time.Now().Format("20060102"))
objPath := fmt.Sprintf("%s/%s/%s", scene, time.Now().Format("20060102"), fileName)
info, err := minioClient.PutObject(ctx, "jky-data", objPath, file, srcFile.Size, minio.PutObjectOptions{ContentType: "application/octet-stream"})
if datasetId > 0 {
var h bool
item := new(model.Dataset)
h, err = rp.engine.ID(datasetId).Get(item)
if err != nil {
return nil, err
}
if h && len(item.StoreName) > 0 {
objPath = fmt.Sprintf("%s/%s/%s/%s", scene, item.StoreName, time.Now().Format("20060102"), fileName)
}
}
//bucketName := fmt.Sprintf("jky-data/%s/%s", scene, time.Now().Format("20060102"))
info, err := minioClient.PutObject(ctx, rp.AppConfig.Minio.Bucket, objPath, file, srcFile.Size, minio.PutObjectOptions{ContentType: "application/octet-stream"})
if err != nil {
return nil, err
}

View File

@ -5,6 +5,7 @@ type FileManager struct {
FileName string `xorm:"VARCHAR(200)" json:"fileName"` //文件名
AccessUrl string `xorm:"VARCHAR(400)" json:"url"` //访问路径
Scene string `xorm:"VARCHAR(40)" json:"scene"` //应用场景0 : 其他, 1: 道路; 2: 桥梁; 3:隧道; 4: 边坡
DatasetId int64 `xorm:"INT(11) index default 0" json:"datasetId"` //数据集
FileSize int64 `xorm:"BIGINT" json:"fileSize"` //文件大小
FileMd5 string `xorm:"VARCHAR(64)" json:"fileMd5"` //文件MD5
Creator int64 `xorm:"INT(11) index" json:"creator"` //上传人

View File

@ -12,6 +12,10 @@ import (
"xorm.io/xorm/dialects"
)
type Statistics struct {
Total int64
}
var (
DB *xorm.Engine
Redis *redis.Client