upfilelogic.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice"
  5. "context"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "bp.jydev.jianyu360.cn/BaseService/biService/api/internal/svc"
  12. "bp.jydev.jianyu360.cn/BaseService/biService/api/internal/types"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type UpFileLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. r *http.Request
  20. }
  21. func NewUpFileLogic(ctx context.Context, svcCtx *svc.ServiceContext, r *http.Request) *UpFileLogic {
  22. return &UpFileLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. r: r,
  27. }
  28. }
  29. func (l *UpFileLogic) UpFile(req *types.UpFileReq) (resp *types.BiResp, err error) {
  30. file, header, _ := l.r.FormFile("file")
  31. defer file.Close()
  32. bt, _ := io.ReadAll(file)
  33. formUrl := l.r.Form.Get("url")
  34. u, err := url.Parse(formUrl)
  35. if err != nil {
  36. l.Error(err)
  37. }
  38. queryValues, _ := url.ParseQuery(u.RawQuery)
  39. fileName := queryValues.Get("fileId")
  40. index := strings.Index(fileName, "-")
  41. // 截取 "-" 后边的内容
  42. fileName = fileName[index+1:]
  43. size := fmt.Sprintf("%.4f", common.Float64All(header.Size)/1024)
  44. for {
  45. if strings.HasSuffix(size, "0") {
  46. size = strings.TrimRight(size, "0")
  47. continue
  48. }
  49. if strings.HasSuffix(size, ".") {
  50. size = strings.TrimRight(size, ".")
  51. break
  52. }
  53. break
  54. }
  55. fileSize := size + "KB"
  56. res, err := l.svcCtx.BiServiceRpc.UpFile(l.ctx, &biservice.UpFileReq{
  57. AppId: req.AppId,
  58. UserId: req.BaseUserId,
  59. PositionId: req.PositionId,
  60. EntUserId: req.EntUserId,
  61. EntId: req.EntId,
  62. Stype: req.FileType,
  63. File: bt,
  64. FileName: fileName,
  65. FileSize: fileSize,
  66. })
  67. return &types.BiResp{Error_code: res.ErrorCode, Error_msg: res.ErrorMsg, Data: res.Data}, err
  68. }