publish.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package controller
  2. import (
  3. "fmt"
  4. "github.com/RaymondCode/simple-demo/api"
  5. "github.com/RaymondCode/simple-demo/config"
  6. "github.com/RaymondCode/simple-demo/dao"
  7. "github.com/RaymondCode/simple-demo/kafka"
  8. "github.com/RaymondCode/simple-demo/service"
  9. "github.com/RaymondCode/simple-demo/util/snowflake"
  10. "github.com/gin-gonic/gin"
  11. "net/http"
  12. "path/filepath"
  13. "strconv"
  14. "strings"
  15. )
  16. type VideoListResponse struct {
  17. api.Response
  18. VideoList []api.Video `json:"video_list"`
  19. }
  20. // Publish check token then save upload file to public directory
  21. func Publish(c *gin.Context) {
  22. token := c.PostForm("token")
  23. fmt.Println("tokenis:" + token)
  24. if exist, _ := service.GetToken(token); exist != 0 {
  25. c.JSON(http.StatusOK, api.Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
  26. return
  27. }
  28. data, err := c.FormFile("data")
  29. if err != nil {
  30. c.JSON(http.StatusOK, api.Response{
  31. StatusCode: 1,
  32. StatusMsg: err.Error(),
  33. })
  34. return
  35. }
  36. filename := filepath.Base(data.Filename)
  37. fmt.Printf("文件大小为: %d 类型为 %s\n", data.Size, filename[len(filename)-3:])
  38. if filename[len(filename)-3:] != "mp4" {
  39. c.JSON(http.StatusOK, api.Response{
  40. StatusCode: 1,
  41. StatusMsg: "不支持的文件类型",
  42. })
  43. return
  44. }
  45. user_id := dao.GetIdByUserId(strings.Split(token, ":")[0])
  46. user_idInt64, _ := strconv.ParseInt(user_id, 10, 64)
  47. var key = snowflake.MakeInt64SnowFlakeId()
  48. dao.PublishTempToDB(user_idInt64, key)
  49. finalName := fmt.Sprintf("%s_%d_%s", user_id, key, filename)
  50. saveFile := config.PROJECTPATH + config.VIDEO_ADDR + finalName
  51. fmt.Println("saveFileDst" + saveFile)
  52. if err := c.SaveUploadedFile(data, saveFile); err != nil {
  53. c.JSON(http.StatusOK, api.Response{
  54. StatusCode: 1,
  55. StatusMsg: err.Error(),
  56. })
  57. return
  58. }
  59. //defer publishToDB(finalName,user.Id)
  60. defer kafka.ProducerSend(finalName, key)
  61. c.JSON(http.StatusOK, api.Response{
  62. StatusCode: 0,
  63. StatusMsg: finalName + " uploaded successfully",
  64. })
  65. }
  66. // PublishList all users have same publish video list
  67. func PublishList(c *gin.Context) {
  68. token := c.Query("token") //Query是获得get请求的参数
  69. fmt.Printf("token为%s\n", token)
  70. if exist, _ := service.GetToken(token); exist != 0 {
  71. c.JSON(http.StatusOK, api.Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
  72. return
  73. }
  74. userId := strings.Split(string(token), ":")[0]
  75. Id, _ := strconv.ParseInt(dao.GetIdByUserId(userId), 10, 64)
  76. fmt.Println(userId)
  77. c.JSON(http.StatusOK, VideoListResponse{
  78. Response: api.Response{
  79. StatusCode: 0,
  80. },
  81. VideoList: dao.GetListById(Id),
  82. })
  83. }