uploadFile.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package controllers
  2. import (
  3. "cc-officialweb/models"
  4. "cc-officialweb/service"
  5. "cc-officialweb/unity"
  6. "cc-officialweb/utils"
  7. "encoding/json"
  8. beego "github.com/beego/beego/v2/server/web"
  9. "github.com/go-playground/validator/v10"
  10. "github.com/google/uuid"
  11. "strconv"
  12. "strings"
  13. )
  14. type UploadFileController struct {
  15. beego.Controller
  16. }
  17. // Post 上传图片
  18. func (up *UploadFileController) Post() {
  19. file, header, err := up.GetFile("file")
  20. types := up.GetString("types")
  21. if err != nil {
  22. up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"}
  23. up.ServeJSON()
  24. return
  25. }
  26. defer file.Close()
  27. index := strings.Split(header.Filename, ".")[1]
  28. fileName := uuid.NewString() + "." + index
  29. if err := up.SaveToFile("file", "./static/file/"+fileName); err != nil {
  30. up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"}
  31. up.ServeJSON()
  32. return
  33. }
  34. var resourse models.Resource
  35. resourse.Name = fileName
  36. resourse.Url = "/file/" + fileName
  37. resourse.Type = types
  38. tx := utils.DB.Create(&resourse)
  39. if tx.RowsAffected > 0 {
  40. up.Data["json"] = &JSON{Code: 200, Msg: "上传成功", Data: resourse.Url}
  41. up.ServeJSON()
  42. return
  43. }
  44. }
  45. // UpdateResource 修改文件
  46. func (up *UploadFileController) UpdateResource() {
  47. getString := up.GetString("id")
  48. atoi, _ := strconv.Atoi(getString)
  49. file, header, err := up.GetFile("file")
  50. types := up.GetString("types")
  51. if err != nil {
  52. up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"}
  53. up.ServeJSON()
  54. return
  55. }
  56. defer file.Close()
  57. index := strings.Split(header.Filename, ".")[1]
  58. fileName := uuid.NewString() + "." + index
  59. if err := up.SaveToFile("file", "./static/upload/"+fileName); err != nil {
  60. up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"}
  61. up.ServeJSON()
  62. return
  63. }
  64. var resource models.Resource
  65. resource.ID = uint(atoi)
  66. resource.Name = fileName
  67. resource.Url = "/file/" + fileName
  68. resource.Type = types
  69. resources := service.UpdateResource(resource)
  70. if len(resources.Url) == 0 {
  71. up.Data["json"] = &JSON{Code: 101, Msg: "修改失败"}
  72. up.ServeJSON()
  73. return
  74. } else {
  75. up.Data["json"] = &JSON{Code: 200, Msg: "修改成功", Data: resource.Url}
  76. up.ServeJSON()
  77. }
  78. }
  79. // DeleteResourceById 删除图片
  80. func (up *UploadFileController) DeleteResourceById() {
  81. getString := up.GetString("id")
  82. atoi, _ := strconv.Atoi(getString)
  83. validate := validator.New()
  84. err2 := validate.Var(atoi, "required")
  85. if err2 != nil {
  86. up.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  87. up.ServeJSON()
  88. return
  89. }
  90. id, err := unity.DeleteById(atoi, models.Resource{})
  91. if err != nil {
  92. up.Data["json"] = &JSON{Code: 101, Msg: "删除失败"}
  93. up.ServeJSON()
  94. return
  95. } else {
  96. up.Data["json"] = &JSON{Code: 200, Msg: "删除成功", Data: id}
  97. up.ServeJSON()
  98. }
  99. }
  100. // GetResource 获取所有资源
  101. func (up *UploadFileController) GetResource() {
  102. var page unity.PageParams
  103. err := json.Unmarshal(up.Ctx.Input.RequestBody, &page)
  104. if err != nil {
  105. up.Data["json"] = &JSON{Code: 101, Msg: "参数错误"}
  106. up.ServeJSON()
  107. return
  108. }
  109. result, total, current, err := unity.Paginate(page, models.FileResource{})
  110. if err != nil {
  111. up.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
  112. up.ServeJSON()
  113. return
  114. } else {
  115. up.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{
  116. Current: current,
  117. Size: total,
  118. Data: result,
  119. }}
  120. up.ServeJSON()
  121. return
  122. }
  123. }
  124. func (up *UploadFileController) GetResourceById() {
  125. getString := up.GetString("id")
  126. id, _ := strconv.Atoi(getString)
  127. validate := validator.New()
  128. err2 := validate.Var(id, "required")
  129. if err2 != nil {
  130. up.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  131. up.ServeJSON()
  132. return
  133. }
  134. result, err := unity.GetById(id, &models.Resource{})
  135. if err != nil {
  136. up.Data["json"] = &JSON{Code: 101, Msg: "获取失败"}
  137. up.ServeJSON()
  138. return
  139. } else {
  140. up.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: result}
  141. up.ServeJSON()
  142. return
  143. }
  144. }