package logic import ( "bytes" "context" "time" "app.yhyue.com/moapp/jybase/common" "bp.jydev.jianyu360.cn/BaseService/fileCenter/entity" "bp.jydev.jianyu360.cn/BaseService/fileCenter/rpc/internal/svc" "bp.jydev.jianyu360.cn/BaseService/fileCenter/rpc/pb" "github.com/gogf/gf/v2/util/gconv" "github.com/zeromicro/go-zero/core/logx" ) type UploadLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogic { return &UploadLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } // 文件上传 func (l *UploadLogic) Upload(in *pb.UploadReq) (*pb.UploadResp, error) { t1 := time.Now() key := "" if in.NeedEncryption { key = entity.GetHashKey(in.File) + entity.TypeByExt(in.Name) } else { fileNameOnly := entity.GetFileNameOnly(in.Name) fileExtension := entity.TypeByExt(in.Name) key = fileNameOnly + gconv.String(time.Now().Unix()) + entity.GetRando() + fileExtension } b, err := entity.OssPutObject(key, bytes.NewReader(in.File), common.InterfaceToStr(in.OssBucketName)) logx.Info("----------", time.Since(t1)) if err == nil && b { return &pb.UploadResp{ Url: in.OssUrl + "/" + key, Key: key, }, nil } return &pb.UploadResp{ ErrorCode: 1, ErrorMsg: "错误", }, nil }