upfilelogic.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package logic
  2. import (
  3. "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  4. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/internal/svc"
  5. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/pb"
  6. fpb "bp.jydev.jianyu360.cn/BaseService/fileCenter/rpc/pb"
  7. "context"
  8. "encoding/json"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. "strings"
  11. )
  12. type UpFileLogic struct {
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. logx.Logger
  16. }
  17. func NewUpFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpFileLogic {
  18. return &UpFileLogic{
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. Logger: logx.WithContext(ctx),
  22. }
  23. }
  24. func (l *UpFileLogic) UpFile(in *pb.UpFileReq) (*pb.BiReply, error) {
  25. up, err := entity.FileCenterRpc.Upload(l.ctx, &fpb.UploadReq{
  26. File: in.File,
  27. OssBucketName: entity.OssBucketName,
  28. OssUrl: entity.OssUrl,
  29. Name: in.FileName,
  30. NeedEncryption: false,
  31. })
  32. if up == nil || up.Url == "" {
  33. return &pb.BiReply{
  34. ErrorCode: 1,
  35. ErrorMsg: "文件上传失败",
  36. Data: nil,
  37. }, err
  38. }
  39. key := up.Key
  40. fileTypeTmp := strings.Split(in.FileName, ".")
  41. var ftype string
  42. if len(fileTypeTmp) > 0 && len(fileTypeTmp) == 2 {
  43. ftype = strings.Split(in.FileName, ".")[1]
  44. }
  45. data := map[string]interface{}{
  46. "filename": in.FileName,
  47. "ftype": ftype,
  48. "fid": key,
  49. "size": in.FileSize,
  50. "ossurl": entity.OssUrl,
  51. "url": up.Url,
  52. }
  53. dataByte, _ := json.Marshal(data)
  54. return &pb.BiReply{
  55. ErrorCode: 0,
  56. ErrorMsg: "",
  57. Data: dataByte,
  58. }, nil
  59. }