uploadlogic.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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/zeromicro/go-zero/core/logx"
  11. )
  12. type UploadLogic struct {
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. logx.Logger
  16. }
  17. func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogic {
  18. return &UploadLogic{
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. Logger: logx.WithContext(ctx),
  22. }
  23. }
  24. // 文件上传
  25. func (l *UploadLogic) Upload(in *pb.UploadReq) (*pb.UploadResp, error) {
  26. t1 := time.Now()
  27. key := ""
  28. if in.NeedEncryption {
  29. key = entity.GetHashKey(in.File) + entity.TypeByExt(in.Name)
  30. } else {
  31. key = in.Name
  32. }
  33. b, err := entity.OssPutObject(key, bytes.NewReader(in.File), common.InterfaceToStr(in.OssBucketName))
  34. logx.Info("----------", time.Since(t1))
  35. if err == nil && b {
  36. return &pb.UploadResp{
  37. Url: in.OssUrl + "/" + key,
  38. Key: key,
  39. }, nil
  40. }
  41. return &pb.UploadResp{
  42. ErrorCode: 1,
  43. ErrorMsg: "错误",
  44. }, nil
  45. }