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" "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) } 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)) 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 } _, err = rp.engine.Insert(list) rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "成功" rsp.Data = list 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 //out, err := os.Create(fileName) //defer out.Close() //if err != nil { // return nil, err //} // //_, err = io.Copy(out, file) //if err != nil { // return nil, err //} // //fileStat, err := out.Stat() //if err != nil { // return nil, err //} 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) accessUrl := fmt.Sprintf("%s/jky-data/%s", rp.AppConfig.Minio.Endpoint, objPath) 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 }