file.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package controller
  2. import (
  3. "fmt"
  4. "gin-template/common"
  5. "gin-template/model"
  6. "github.com/gin-gonic/gin"
  7. "net/http"
  8. "path/filepath"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func GetAllFiles(c *gin.Context) {
  14. p, _ := strconv.Atoi(c.Query("p"))
  15. if p < 0 {
  16. p = 0
  17. }
  18. files, err := model.GetAllFiles(p*common.ItemsPerPage, common.ItemsPerPage)
  19. if err != nil {
  20. c.JSON(http.StatusOK, gin.H{
  21. "success": false,
  22. "message": err.Error(),
  23. })
  24. return
  25. }
  26. c.JSON(http.StatusOK, gin.H{
  27. "success": true,
  28. "message": "",
  29. "data": files,
  30. })
  31. return
  32. }
  33. func SearchFiles(c *gin.Context) {
  34. keyword := c.Query("keyword")
  35. files, err := model.SearchFiles(keyword)
  36. if err != nil {
  37. c.JSON(http.StatusOK, gin.H{
  38. "success": false,
  39. "message": err.Error(),
  40. })
  41. return
  42. }
  43. c.JSON(http.StatusOK, gin.H{
  44. "success": true,
  45. "message": "",
  46. "data": files,
  47. })
  48. return
  49. }
  50. func UploadFile(c *gin.Context) {
  51. form, err := c.MultipartForm()
  52. if err != nil {
  53. c.JSON(http.StatusOK, gin.H{
  54. "success": false,
  55. "message": err.Error(),
  56. })
  57. return
  58. }
  59. uploadPath := common.UploadPath
  60. description := c.PostForm("description")
  61. if description == "" {
  62. description = "无描述信息"
  63. }
  64. uploader := c.GetString("username")
  65. if uploader == "" {
  66. uploader = "访客用户"
  67. }
  68. uploaderId := c.GetInt("id")
  69. currentTime := time.Now().Format("2006-01-02 15:04:05")
  70. files := form.File["file"]
  71. for _, file := range files {
  72. filename := filepath.Base(file.Filename)
  73. ext := filepath.Ext(filename)
  74. link := common.GetUUID() + ext
  75. savePath := filepath.Join(uploadPath, link) // both parts are checked, so this path should be safe to use
  76. if err := c.SaveUploadedFile(file, savePath); err != nil {
  77. c.JSON(http.StatusOK, gin.H{
  78. "success": false,
  79. "message": err.Error(),
  80. })
  81. return
  82. }
  83. // save to database
  84. fileObj := &model.File{
  85. Description: description,
  86. Uploader: uploader,
  87. UploadTime: currentTime,
  88. UploaderId: uploaderId,
  89. Link: link,
  90. Filename: filename,
  91. }
  92. err = fileObj.Insert()
  93. if err != nil {
  94. _ = fmt.Errorf(err.Error())
  95. }
  96. }
  97. c.JSON(http.StatusOK, gin.H{
  98. "success": true,
  99. "message": "",
  100. })
  101. return
  102. }
  103. func DeleteFile(c *gin.Context) {
  104. fileIdStr := c.Param("id")
  105. fileId, err := strconv.Atoi(fileIdStr)
  106. if err != nil || fileId == 0 {
  107. c.JSON(http.StatusBadRequest, gin.H{
  108. "success": false,
  109. "message": "无效的参数",
  110. })
  111. return
  112. }
  113. fileObj := &model.File{
  114. Id: fileId,
  115. }
  116. model.DB.Where("id = ?", fileId).First(&fileObj)
  117. if fileObj.Link == "" {
  118. c.JSON(http.StatusOK, gin.H{
  119. "success": false,
  120. "message": "文件不存在!",
  121. })
  122. return
  123. }
  124. err = fileObj.Delete()
  125. if err != nil {
  126. c.JSON(http.StatusOK, gin.H{
  127. "success": true,
  128. "message": err.Error(),
  129. })
  130. return
  131. } else {
  132. message := "文件删除成功"
  133. c.JSON(http.StatusOK, gin.H{
  134. "success": true,
  135. "message": message,
  136. })
  137. }
  138. }
  139. func DownloadFile(c *gin.Context) {
  140. path := c.Param("file")
  141. fullPath := filepath.Join(common.UploadPath, path)
  142. if !strings.HasPrefix(fullPath, common.UploadPath) {
  143. // We may being attacked!
  144. c.Status(403)
  145. return
  146. }
  147. c.File(fullPath)
  148. // Update download counter
  149. go func() {
  150. model.UpdateDownloadCounter(path)
  151. }()
  152. }