uploadlogic.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package logic
  2. import (
  3. "bytes"
  4. "context"
  5. "time"
  6. "app.yhyue.com/moapp/jybase/common"
  7. "bp.jydev.jianyu360.cn/BaseService/fileCenter/entity"
  8. "bp.jydev.jianyu360.cn/BaseService/fileCenter/rpc/internal/svc"
  9. "bp.jydev.jianyu360.cn/BaseService/fileCenter/rpc/pb"
  10. "github.com/gogf/gf/v2/util/gconv"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type UploadLogic struct {
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. logx.Logger
  17. }
  18. func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogic {
  19. return &UploadLogic{
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. Logger: logx.WithContext(ctx),
  23. }
  24. }
  25. // 文件上传
  26. func (l *UploadLogic) Upload(in *pb.UploadReq) (*pb.UploadResp, error) {
  27. t1 := time.Now()
  28. key := ""
  29. if in.NeedEncryption {
  30. key = entity.GetHashKey(in.File) + entity.TypeByExt(in.Name)
  31. } else {
  32. fileNameOnly := entity.GetFileNameOnly(in.Name)
  33. fileExtension := entity.TypeByExt(in.Name)
  34. key = fileNameOnly + "_" + gconv.String(time.Now().Unix()) + entity.GetRando() + fileExtension
  35. }
  36. b, err := entity.OssPutObject(key, bytes.NewReader(in.File), common.InterfaceToStr(in.OssBucketName))
  37. logx.Info("----------", time.Since(t1))
  38. if err == nil && b {
  39. return &pb.UploadResp{
  40. Url: in.OssUrl + "/" + key,
  41. Key: key,
  42. }, nil
  43. }
  44. return &pb.UploadResp{
  45. ErrorCode: 1,
  46. ErrorMsg: "错误",
  47. }, nil
  48. }