file.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "message-pusher/common"
  7. "message-pusher/model"
  8. "net/http"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. )
  14. type FileDeleteRequest struct {
  15. Id int
  16. Link string
  17. //Token string
  18. }
  19. func UploadFile(c *gin.Context) {
  20. uploadPath := common.UploadPath
  21. //saveToDatabase := true
  22. //path := c.PostForm("path")
  23. //if path != "" { // Upload to explorer's path
  24. // uploadPath = filepath.Join(common.ExplorerRootPath, path)
  25. // if !strings.HasPrefix(uploadPath, common.ExplorerRootPath) {
  26. // // In this case the given path is not valid, so we reset it to ExplorerRootPath.
  27. // uploadPath = common.ExplorerRootPath
  28. // }
  29. // saveToDatabase = false
  30. //}
  31. description := c.PostForm("description")
  32. if description == "" {
  33. description = "无描述信息"
  34. }
  35. uploader := c.GetString("username")
  36. if uploader == "" {
  37. uploader = "匿名用户"
  38. }
  39. currentTime := time.Now().Format("2006-01-02 15:04:05")
  40. form, err := c.MultipartForm()
  41. if err != nil {
  42. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  43. return
  44. }
  45. files := form.File["file"]
  46. for _, file := range files {
  47. // In case someone wants to upload to other folders.
  48. filename := filepath.Base(file.Filename)
  49. link := filename
  50. savePath := filepath.Join(uploadPath, filename)
  51. if _, err := os.Stat(savePath); err == nil {
  52. // File already existed.
  53. t := time.Now()
  54. timestamp := t.Format("_2006-01-02_15-04-05")
  55. ext := filepath.Ext(filename)
  56. if ext == "" {
  57. link += timestamp
  58. } else {
  59. link = filename[:len(filename)-len(ext)] + timestamp + ext
  60. }
  61. savePath = filepath.Join(uploadPath, link)
  62. }
  63. if err := c.SaveUploadedFile(file, savePath); err != nil {
  64. c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
  65. return
  66. }
  67. // save to database
  68. fileObj := &model.File{
  69. Description: description,
  70. Uploader: uploader,
  71. Time: currentTime,
  72. Link: link,
  73. Filename: filename,
  74. }
  75. err = fileObj.Insert()
  76. if err != nil {
  77. _ = fmt.Errorf(err.Error())
  78. }
  79. }
  80. c.Redirect(http.StatusSeeOther, "./")
  81. }
  82. func DeleteFile(c *gin.Context) {
  83. var deleteRequest FileDeleteRequest
  84. err := json.NewDecoder(c.Request.Body).Decode(&deleteRequest)
  85. if err != nil {
  86. c.JSON(http.StatusBadRequest, gin.H{
  87. "success": false,
  88. "message": "无效的参数",
  89. })
  90. return
  91. }
  92. fileObj := &model.File{
  93. Id: deleteRequest.Id,
  94. }
  95. model.DB.Where("id = ?", deleteRequest.Id).First(&fileObj)
  96. err = fileObj.Delete()
  97. if err != nil {
  98. c.JSON(http.StatusOK, gin.H{
  99. "success": true,
  100. "message": err.Error(),
  101. })
  102. } else {
  103. message := "文件删除成功"
  104. c.JSON(http.StatusOK, gin.H{
  105. "success": true,
  106. "message": message,
  107. })
  108. }
  109. }
  110. func DownloadFile(c *gin.Context) {
  111. path := c.Param("file")
  112. fullPath := filepath.Join(common.UploadPath, path)
  113. if !strings.HasPrefix(fullPath, common.UploadPath) {
  114. // We may being attacked!
  115. c.Status(403)
  116. return
  117. }
  118. c.File(fullPath)
  119. // Update download counter
  120. go func() {
  121. model.UpdateDownloadCounter(path)
  122. }()
  123. }