system_file_upload_logic.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package system
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  7. "git.i2edu.net/i2/i2-bill-api/internal/types"
  8. "io"
  9. "net/http"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "git.i2edu.net/i2/go-zero/core/logx"
  14. )
  15. type SystemFileUploadLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewSystemFileUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) SystemFileUploadLogic {
  21. return SystemFileUploadLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. func (l *SystemFileUploadLogic) SystemFileUpload(r *http.Request) (*types.Response, error) {
  28. // todo: add your logic here and delete this line
  29. file, fileHeader, err := r.FormFile("file")
  30. if err != nil {
  31. logx.Error(err.Error())
  32. return &types.Response{500, err.Error(), nil}, nil
  33. }
  34. oss, err := svc.GetAliYunOssInstance()
  35. if err != nil {
  36. logx.Error(err.Error())
  37. return &types.Response{500, err.Error(), nil}, nil
  38. }
  39. res, err := oss.Upload(file, fileHeader)
  40. if err != nil {
  41. logx.Error(err.Error())
  42. return &types.Response{500, err.Error(), nil}, nil
  43. }
  44. resp := make(map[string]interface{})
  45. resByte, err := json.Marshal(res)
  46. if err != nil {
  47. logx.Error(err.Error())
  48. return &types.Response{500, err.Error(), nil}, nil
  49. }
  50. if err := json.Unmarshal(resByte, &resp); err != nil {
  51. logx.Error(err.Error())
  52. return &types.Response{500, err.Error(), nil}, nil
  53. }
  54. resp["full_url"] = strings.TrimLeft(l.svcCtx.Config.AliYunOss.FileUrl, ",") + "/" + strings.TrimRight(res.Url, "/")
  55. return &types.Response{200, "", resp}, nil
  56. }
  57. func localFileUpload(l SystemFileUploadLogic, r *http.Request) (*types.Response, error) {
  58. userId := l.svcCtx.GetUserIdByJwt(l.ctx)
  59. file, fileHeader, err := r.FormFile("file")
  60. if err != nil {
  61. logx.Error(err.Error())
  62. return &types.Response{500, err.Error(), nil}, nil
  63. }
  64. dir := "asserts/avatar"
  65. _, err = os.Stat(dir)
  66. if err != nil && !os.IsExist(err) {
  67. err := os.MkdirAll(dir, os.ModePerm)
  68. if err != nil {
  69. logx.Error(err.Error())
  70. return &types.Response{500, err.Error(), nil}, nil
  71. }
  72. }
  73. fileName := fmt.Sprintf("%s/%d%s", dir, userId, filepath.Ext(fileHeader.Filename))
  74. newFile, err := os.OpenFile(fileName, os.O_TRUNC|os.O_CREATE|os.O_RDWR, 0777)
  75. if err != nil {
  76. logx.Error(err.Error())
  77. return &types.Response{500, err.Error(), nil}, nil
  78. }
  79. _, err = io.Copy(newFile, file)
  80. if err != nil {
  81. logx.Error(err.Error())
  82. return &types.Response{500, err.Error(), nil}, nil
  83. }
  84. return &types.Response{200, "", fileName}, nil
  85. }