task.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package http
  2. import (
  3. "crypto/md5"
  4. "fmt"
  5. "io/ioutil"
  6. "strings"
  7. "time"
  8. "go-common/app/admin/main/sms/conf"
  9. pb "go-common/app/service/main/sms/api"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. )
  14. func addTask(ctx *bm.Context) {
  15. req := new(pb.AddTaskReq)
  16. if err := ctx.Bind(req); err != nil {
  17. return
  18. }
  19. ctx.JSON(svc.AddTask(ctx, req))
  20. }
  21. func updateTask(ctx *bm.Context) {
  22. req := new(pb.UpdateTaskReq)
  23. if err := ctx.Bind(req); err != nil {
  24. return
  25. }
  26. ctx.JSON(svc.UpdateTask(ctx, req))
  27. }
  28. func deleteTask(ctx *bm.Context) {
  29. req := new(pb.DeleteTaskReq)
  30. if err := ctx.Bind(req); err != nil {
  31. return
  32. }
  33. ctx.JSON(svc.DeleteTask(ctx, req))
  34. }
  35. func taskInfo(ctx *bm.Context) {
  36. req := new(pb.TaskInfoReq)
  37. if err := ctx.Bind(req); err != nil {
  38. return
  39. }
  40. res, err := svc.TaskInfo(ctx, req)
  41. ctx.JSON(res.Info, err)
  42. }
  43. func taskList(ctx *bm.Context) {
  44. req := new(pb.TaskListReq)
  45. if err := ctx.Bind(req); err != nil {
  46. return
  47. }
  48. res, err := svc.TaskList(ctx, req)
  49. if err != nil {
  50. ctx.JSON(nil, err)
  51. return
  52. }
  53. pager := struct {
  54. Pn int32 `json:"page"`
  55. Ps int32 `json:"pagesize"`
  56. Total int32 `json:"total"`
  57. }{
  58. Pn: req.Pn,
  59. Ps: req.Ps,
  60. Total: res.Total,
  61. }
  62. data := map[string]interface{}{
  63. "data": res.List,
  64. "pager": pager,
  65. }
  66. ctx.JSONMap(data, nil)
  67. }
  68. func upload(ctx *bm.Context) {
  69. var (
  70. err error
  71. req = ctx.Request
  72. )
  73. req.ParseMultipartForm(1024 * 1024 * 1024) // 1G
  74. fileName := req.FormValue("filename")
  75. if fileName == "" {
  76. log.Error("filename is empty")
  77. ctx.JSON(nil, ecode.RequestErr)
  78. return
  79. }
  80. file, header, err := req.FormFile("file")
  81. if err != nil {
  82. log.Error("req.FormFile() error(%v)", err)
  83. ctx.JSON(nil, err)
  84. return
  85. }
  86. defer file.Close()
  87. bs, err := ioutil.ReadAll(file)
  88. if err != nil {
  89. log.Error("ioutil.ReadAll() error(%v)", err)
  90. ctx.JSON(nil, err)
  91. return
  92. }
  93. if err = svc.CheckUploadFile(bs); err != nil {
  94. ctx.JSON(nil, err)
  95. return
  96. }
  97. dir := fmt.Sprintf("%s/%s", strings.TrimSuffix(conf.Conf.Sms.MountDir, "/"), time.Now().Format("20060102"))
  98. path := fmt.Sprintf("%s/%x", dir, md5.Sum([]byte(fileName)))
  99. if err = svc.Upload(dir, path, bs); err != nil {
  100. log.Error("upload file file(%s) error(%v)", path, err)
  101. ctx.JSON(nil, err)
  102. return
  103. }
  104. ctx.JSON(struct {
  105. Name string `json:"name"`
  106. Path string `json:"path"`
  107. }{
  108. Name: header.Filename,
  109. Path: path,
  110. }, nil)
  111. }