1、修改文件上传
This commit is contained in:
parent
93405fe90a
commit
62b6338613
|
@ -20,6 +20,7 @@ minio:
|
||||||
endpoint: 127.0.0.1:9000
|
endpoint: 127.0.0.1:9000
|
||||||
accessKeyId: root
|
accessKeyId: root
|
||||||
secretAccessKey: OIxv7QptYBO3
|
secretAccessKey: OIxv7QptYBO3
|
||||||
|
bucket: jky-data
|
||||||
consul:
|
consul:
|
||||||
host: http://consul.hpds.cc
|
host: http://consul.hpds.cc
|
||||||
port: 80
|
port: 80
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"hpds-iot-web/internal/service"
|
"hpds-iot-web/internal/service"
|
||||||
"hpds-iot-web/model"
|
"hpds-iot-web/model"
|
||||||
e "hpds-iot-web/pkg/err"
|
e "hpds-iot-web/pkg/err"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s HandlerService) UploadFile(c *gin.Context) (data interface{}, err error) {
|
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)
|
return nil, e.NewValidErr(err)
|
||||||
}
|
}
|
||||||
files := form.File["file"]
|
files := form.File["file"]
|
||||||
scene := c.GetString("scene")
|
val := form.Value["scene"]
|
||||||
|
scene := val[0]
|
||||||
if len(scene) < 1 {
|
if len(scene) < 1 {
|
||||||
scene = "other"
|
scene = "other"
|
||||||
}
|
}
|
||||||
|
datasetId, _ := strconv.ParseInt(form.Value["datasetId"][0], 10, 64)
|
||||||
req = proto.UploadFileRequest{
|
req = proto.UploadFileRequest{
|
||||||
Creator: userInfo.UserId,
|
Creator: userInfo.UserId,
|
||||||
Scene: scene,
|
Scene: scene,
|
||||||
Files: files,
|
Files: files,
|
||||||
|
DatasetId: datasetId,
|
||||||
}
|
}
|
||||||
data, err = repo.UploadFile(c, req)
|
data, err = repo.UploadFile(c, req)
|
||||||
go s.SaveLog("文件上传", "Manage", "", "", "", fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "")
|
go s.SaveLog("文件上传", "Manage", "", "", "", fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "")
|
||||||
|
|
|
@ -408,9 +408,10 @@ func (p DeviceItemRequest) ToString() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
type UploadFileRequest struct {
|
type UploadFileRequest struct {
|
||||||
Creator int64 `json:"creator"`
|
Creator int64 `json:"creator"`
|
||||||
Scene string `json:"scene"`
|
Scene string `json:"scene"`
|
||||||
Files []*multipart.FileHeader `json:"files"`
|
Files []*multipart.FileHeader `json:"files"`
|
||||||
|
DatasetId int64 `json:"datasetId"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BrandRequest struct {
|
type BrandRequest struct {
|
||||||
|
|
|
@ -65,6 +65,7 @@ type DatasetItem struct {
|
||||||
Creator int64 `json:"creator"`
|
Creator int64 `json:"creator"`
|
||||||
CreateAt int64 `json:"createAt"`
|
CreateAt int64 `json:"createAt"`
|
||||||
DatasetCount int64 `json:"datasetCount"`
|
DatasetCount int64 `json:"datasetCount"`
|
||||||
|
DatasetSize int64 `json:"datasetSize"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeState struct {
|
type NodeState struct {
|
||||||
|
|
|
@ -97,11 +97,16 @@ func (rp *repo) DatasetList(ctx context.Context, req proto.DatasetRequest) (rsp
|
||||||
}
|
}
|
||||||
data := make([]proto.DatasetItem, len(list))
|
data := make([]proto.DatasetItem, len(list))
|
||||||
for k, v := range 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)
|
datasetCount, err := rp.engine.Where("dataset_id = ?", v.DatasetId).FindAndCount(&detailList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
goto ReturnPoint
|
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{
|
data[k] = proto.DatasetItem{
|
||||||
DatasetId: v.DatasetId,
|
DatasetId: v.DatasetId,
|
||||||
DatasetName: v.DatasetName,
|
DatasetName: v.DatasetName,
|
||||||
|
@ -113,6 +118,7 @@ func (rp *repo) DatasetList(ctx context.Context, req proto.DatasetRequest) (rsp
|
||||||
Creator: v.Creator,
|
Creator: v.Creator,
|
||||||
CreateAt: v.CreateAt,
|
CreateAt: v.CreateAt,
|
||||||
DatasetCount: datasetCount,
|
DatasetCount: datasetCount,
|
||||||
|
DatasetSize: datasetSize,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rsp.Code = http.StatusOK
|
rsp.Code = http.StatusOK
|
||||||
|
|
|
@ -22,7 +22,7 @@ import (
|
||||||
|
|
||||||
type FileService interface {
|
type FileService interface {
|
||||||
UploadFile(ctx context.Context, req proto.UploadFileRequest) (rsp *proto.BaseResponse, err error)
|
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 {
|
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:
|
default:
|
||||||
list := make([]*model.FileManager, len(req.Files))
|
list := make([]*model.FileManager, len(req.Files))
|
||||||
fileUrl := make([]string, len(req.Files))
|
fileUrl := make([]string, len(req.Files))
|
||||||
for k, _ := range req.Files {
|
for k := range req.Files {
|
||||||
fileItem, err := rp.UploadFileToMinIo(ctx, req.Files[k], req.Scene, req.Creator)
|
fileItem, err := rp.UploadFileToMinIo(ctx, req.Files[k], req.Scene, req.DatasetId, req.Creator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
goto ReturnPoint
|
goto ReturnPoint
|
||||||
}
|
}
|
||||||
list[k] = fileItem
|
list[k] = fileItem
|
||||||
|
list[k].DatasetId = req.DatasetId
|
||||||
fileUrl[k] = fileItem.AccessUrl
|
fileUrl[k] = fileItem.AccessUrl
|
||||||
}
|
}
|
||||||
_, err = rp.engine.Insert(list)
|
_, err = rp.engine.Insert(list)
|
||||||
|
@ -75,9 +76,11 @@ ReturnPoint:
|
||||||
return rsp, err
|
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()
|
file, err := srcFile.Open()
|
||||||
defer file.Close()
|
defer func(file multipart.File) {
|
||||||
|
_ = file.Close()
|
||||||
|
}(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -92,9 +95,21 @@ func (rp *repo) UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHe
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ type FileManager struct {
|
||||||
FileName string `xorm:"VARCHAR(200)" json:"fileName"` //文件名
|
FileName string `xorm:"VARCHAR(200)" json:"fileName"` //文件名
|
||||||
AccessUrl string `xorm:"VARCHAR(400)" json:"url"` //访问路径
|
AccessUrl string `xorm:"VARCHAR(400)" json:"url"` //访问路径
|
||||||
Scene string `xorm:"VARCHAR(40)" json:"scene"` //应用场景,0 : 其他, 1: 道路; 2: 桥梁; 3:隧道; 4: 边坡
|
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"` //文件大小
|
FileSize int64 `xorm:"BIGINT" json:"fileSize"` //文件大小
|
||||||
FileMd5 string `xorm:"VARCHAR(64)" json:"fileMd5"` //文件MD5
|
FileMd5 string `xorm:"VARCHAR(64)" json:"fileMd5"` //文件MD5
|
||||||
Creator int64 `xorm:"INT(11) index" json:"creator"` //上传人
|
Creator int64 `xorm:"INT(11) index" json:"creator"` //上传人
|
||||||
|
|
|
@ -12,6 +12,10 @@ import (
|
||||||
"xorm.io/xorm/dialects"
|
"xorm.io/xorm/dialects"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Statistics struct {
|
||||||
|
Total int64
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DB *xorm.Engine
|
DB *xorm.Engine
|
||||||
Redis *redis.Client
|
Redis *redis.Client
|
||||||
|
|
Loading…
Reference in New Issue