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-01-12 10:21:40 +08:00
|
|
|
UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHeader, scene string, creator int64) (data *model.FileManager, 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-01-11 18:05:29 +08:00
|
|
|
for k, _ := range req.Files {
|
|
|
|
fileItem, err := rp.UploadFileToMinIo(ctx, req.Files[k], req.Scene, req.Creator)
|
|
|
|
if err != nil {
|
|
|
|
goto ReturnPoint
|
|
|
|
}
|
|
|
|
list[k] = fileItem
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *repo) UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHeader, scene string, creator int64) (data *model.FileManager, err error) {
|
|
|
|
file, err := srcFile.Open()
|
|
|
|
defer file.Close()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
//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 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
|
|
|
|
data.Creator = creator
|
|
|
|
data.FileMd5 = hex.EncodeToString(md5hash.Sum(nil))
|
|
|
|
data.CreateAt = time.Now().Unix()
|
|
|
|
data.UpdateAt = time.Now().Unix()
|
|
|
|
return data, nil
|
|
|
|
}
|