diff --git a/config/config.yaml b/config/config.yaml index 02cd19b..9cd6c7a 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -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 diff --git a/internal/handler/file.go b/internal/handler/file.go index 1d4b3b6..98e3c7c 100644 --- a/internal/handler/file.go +++ b/internal/handler/file.go @@ -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, "") diff --git a/internal/proto/request.go b/internal/proto/request.go index e2268a6..76fd6da 100644 --- a/internal/proto/request.go +++ b/internal/proto/request.go @@ -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 { diff --git a/internal/proto/response.go b/internal/proto/response.go index d262930..14543e5 100644 --- a/internal/proto/response.go +++ b/internal/proto/response.go @@ -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 { diff --git a/internal/service/dataset.go b/internal/service/dataset.go index d7a5b0c..17a418b 100644 --- a/internal/service/dataset.go +++ b/internal/service/dataset.go @@ -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 diff --git a/internal/service/fileManage.go b/internal/service/fileManage.go index 358ae80..ccb94b9 100644 --- a/internal/service/fileManage.go +++ b/internal/service/fileManage.go @@ -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 } diff --git a/model/file.go b/model/file.go index 0d42d3e..4aeb149 100644 --- a/model/file.go +++ b/model/file.go @@ -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"` //上传人 diff --git a/model/index.go b/model/index.go index 609a6b7..ed18cf2 100644 --- a/model/index.go +++ b/model/index.go @@ -12,6 +12,10 @@ import ( "xorm.io/xorm/dialects" ) +type Statistics struct { + Total int64 +} + var ( DB *xorm.Engine Redis *redis.Client