system_file_download_logic.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package system
  2. import (
  3. "context"
  4. "git.i2edu.net/i2/i2-bill-api/internal/global"
  5. "git.i2edu.net/i2/i2-bill-api/model"
  6. "io"
  7. "net/http"
  8. "strconv"
  9. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  10. "git.i2edu.net/i2/i2-bill-api/internal/types"
  11. "git.i2edu.net/i2/go-zero/core/logx"
  12. )
  13. type SystemFileDownloadLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewSystemFileDownloadLogic(ctx context.Context, svcCtx *svc.ServiceContext) SystemFileDownloadLogic {
  19. return SystemFileDownloadLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. }
  24. }
  25. func (l *SystemFileDownloadLogic) SystemFileDownload(w http.ResponseWriter, r *http.Request) (*types.Response, error) {
  26. // todo: add your logic here and delete this line
  27. r.ParseForm()
  28. id := r.FormValue("id")
  29. attachment := new(model.I2billSysAttachment)
  30. _, err := l.svcCtx.DB.ID(id).Get(attachment)
  31. if err != nil {
  32. logx.Error(err.Error())
  33. return &types.Response{500, err.Error(), nil}, nil
  34. }
  35. if attachment.Id == "" {
  36. return &types.Response{200, "", nil}, nil
  37. }
  38. f, err := global.NewAliYunOssUpDownloader().Download(attachment)
  39. if err != nil {
  40. logx.Error(err.Error())
  41. return &types.Response{500, err.Error(), nil}, nil
  42. }
  43. w.Header().Set("Content-Disposition", "attachment;filename=\""+attachment.Name+"\"")
  44. w.Header().Set("Content-Type", "application/octet-stream")
  45. w.Header().Set("Content-Length", strconv.FormatInt(int64(attachment.Size), 10))
  46. w.WriteHeader(200)
  47. io.Copy(w, f)
  48. return nil, nil
  49. }