2023-01-11 18:05:29 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"git.hpds.cc/Component/logging"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
"hpds-iot-web/config"
|
|
|
|
"hpds-iot-web/internal/proto"
|
|
|
|
"hpds-iot-web/model"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
2023-01-12 10:21:40 +08:00
|
|
|
"strings"
|
2023-01-11 18:05:29 +08:00
|
|
|
"time"
|
|
|
|
"xorm.io/xorm"
|
|
|
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FileService interface {
|
|
|
|
UploadFile(ctx context.Context, req proto.UploadFileRequest) (rsp *proto.BaseResponse, err error)
|
2023-04-24 15:21:17 +08:00
|
|
|
UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHeader, scene string, datasetId, creator int64, dataType int) (data *model.FileManager, err error)
|
|
|
|
FileList(ctx context.Context, req proto.DatasetItemRequest) (rsp *proto.BaseResponse, err error)
|
2023-01-11 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileService(cfg *config.WebConfig, engine *xorm.Engine, logger *logging.Logger) FileService {
|
|
|
|
return &repo{
|
|
|
|
AppConfig: cfg,
|
|
|
|
engine: engine,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *repo) UploadFile(ctx context.Context, req proto.UploadFileRequest) (rsp *proto.BaseResponse, err error) {
|
|
|
|
rsp = new(proto.BaseResponse)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err = fmt.Errorf("超时/取消")
|
|
|
|
rsp.Code = http.StatusInternalServerError
|
|
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
|
|
rsp.Message = "超时/取消"
|
|
|
|
rsp.Err = ctx.Err()
|
|
|
|
return rsp, ctx.Err()
|
|
|
|
default:
|
|
|
|
list := make([]*model.FileManager, len(req.Files))
|
2023-01-12 10:21:40 +08:00
|
|
|
fileUrl := make([]string, len(req.Files))
|
2023-03-24 08:42:28 +08:00
|
|
|
for k := range req.Files {
|
2023-04-24 15:21:17 +08:00
|
|
|
fileItem, err := rp.UploadFileToMinIo(ctx, req.Files[k], req.Scene, req.DatasetId, req.Creator, req.DataType)
|
2023-01-11 18:05:29 +08:00
|
|
|
if err != nil {
|
|
|
|
goto ReturnPoint
|
|
|
|
}
|
|
|
|
list[k] = fileItem
|
2023-03-24 08:42:28 +08:00
|
|
|
list[k].DatasetId = req.DatasetId
|
2023-01-12 10:21:40 +08:00
|
|
|
fileUrl[k] = fileItem.AccessUrl
|
2023-01-11 18:05:29 +08:00
|
|
|
}
|
|
|
|
_, err = rp.engine.Insert(list)
|
2023-01-12 10:21:40 +08:00
|
|
|
res := proto.UploadResponse{
|
|
|
|
Url: strings.Join(fileUrl, ","),
|
|
|
|
}
|
2023-01-11 18:05:29 +08:00
|
|
|
rsp.Code = http.StatusOK
|
|
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
|
|
rsp.Message = "成功"
|
2023-01-12 10:21:40 +08:00
|
|
|
rsp.Data = res
|
2023-01-11 18:05:29 +08:00
|
|
|
rsp.Err = err
|
|
|
|
return rsp, err
|
|
|
|
}
|
|
|
|
ReturnPoint:
|
|
|
|
if err != nil {
|
|
|
|
rsp.Code = http.StatusInternalServerError
|
|
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
|
|
rsp.Err = err
|
|
|
|
rsp.Message = "失败"
|
|
|
|
}
|
|
|
|
return rsp, err
|
|
|
|
}
|
|
|
|
|
2023-04-24 15:21:17 +08:00
|
|
|
func (rp *repo) UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHeader, scene string, datasetId, creator int64, dataType int) (data *model.FileManager, err error) {
|
2023-01-11 18:05:29 +08:00
|
|
|
file, err := srcFile.Open()
|
2023-03-24 08:42:28 +08:00
|
|
|
defer func(file multipart.File) {
|
|
|
|
_ = file.Close()
|
|
|
|
}(file)
|
2023-01-11 18:05:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := srcFile.Filename
|
|
|
|
|
|
|
|
opt := &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(rp.AppConfig.Minio.AccessKeyId, rp.AppConfig.Minio.SecretAccessKey, ""),
|
|
|
|
Secure: false,
|
|
|
|
}
|
|
|
|
minioClient, err := minio.New(rp.AppConfig.Minio.Endpoint, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-24 08:42:28 +08:00
|
|
|
|
2023-01-11 18:05:29 +08:00
|
|
|
objPath := fmt.Sprintf("%s/%s/%s", scene, time.Now().Format("20060102"), fileName)
|
2023-03-24 08:42:28 +08:00
|
|
|
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"})
|
2023-01-11 18:05:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fmt.Println("info =====> ", info)
|
2023-01-12 10:21:40 +08:00
|
|
|
accessUrl := fmt.Sprintf("%s://%s/jky-data/%s", rp.AppConfig.Minio.Protocol, rp.AppConfig.Minio.Endpoint, objPath)
|
2023-01-11 18:05:29 +08:00
|
|
|
|
|
|
|
md5hash := md5.New()
|
|
|
|
if _, err := io.Copy(md5hash, file); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data = new(model.FileManager)
|
|
|
|
data.FileName = fileName
|
|
|
|
data.Scene = scene
|
|
|
|
data.AccessUrl = accessUrl
|
|
|
|
data.FileSize = srcFile.Size
|
2023-04-24 15:21:17 +08:00
|
|
|
data.DataType = dataType
|
|
|
|
data.DatasetId = datasetId
|
2023-01-11 18:05:29 +08:00
|
|
|
data.Creator = creator
|
|
|
|
data.FileMd5 = hex.EncodeToString(md5hash.Sum(nil))
|
|
|
|
data.CreateAt = time.Now().Unix()
|
|
|
|
data.UpdateAt = time.Now().Unix()
|
|
|
|
return data, nil
|
|
|
|
}
|
2023-04-24 15:21:17 +08:00
|
|
|
|
|
|
|
func (rp *repo) FileList(ctx context.Context, req proto.DatasetItemRequest) (rsp *proto.BaseResponse, err error) {
|
|
|
|
rsp = new(proto.BaseResponse)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err = fmt.Errorf("超时/取消")
|
|
|
|
rsp.Code = http.StatusInternalServerError
|
|
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
|
|
rsp.Message = "超时/取消"
|
|
|
|
rsp.Err = ctx.Err()
|
|
|
|
return rsp, ctx.Err()
|
|
|
|
default:
|
|
|
|
data := make([]model.FileManager, 0)
|
|
|
|
count, err := rp.engine.Where("(? = 0 or dataset_id = ?)", req.DatasetId, req.DatasetId).
|
|
|
|
And("(? = 0 or data_type = ?)", req.DataType, req.DataType).
|
|
|
|
Limit(int(req.Size), int(((req.Page)-1)*req.Size)).
|
|
|
|
FindAndCount(&data)
|
|
|
|
if err != nil {
|
|
|
|
goto ReturnPoint
|
|
|
|
}
|
|
|
|
rsp.Code = http.StatusOK
|
|
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
|
|
rsp.Message = "成功"
|
|
|
|
rsp = FillPaging(count, req.Page, req.Size, data, rsp)
|
|
|
|
rsp.Err = err
|
|
|
|
return rsp, err
|
|
|
|
}
|
|
|
|
ReturnPoint:
|
|
|
|
if err != nil {
|
|
|
|
rsp.Code = http.StatusInternalServerError
|
|
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
|
|
rsp.Err = err
|
|
|
|
rsp.Message = "失败"
|
|
|
|
}
|
|
|
|
return rsp, err
|
|
|
|
}
|