file.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package controller
  2. import (
  3. "encoding/json"
  4. "github.com/gin-gonic/gin"
  5. "iot_manager_service/app/file/model"
  6. "iot_manager_service/app/file/service"
  7. "iot_manager_service/util/cache"
  8. "iot_manager_service/util/common"
  9. "iot_manager_service/util/token"
  10. "net/http"
  11. "os"
  12. "path"
  13. "path/filepath"
  14. "strconv"
  15. "time"
  16. )
  17. var FileController = new(file)
  18. type file struct{}
  19. func (f *file) Distribute(c *gin.Context) {
  20. //获取当前用户
  21. header := c.GetHeader("Authorization")
  22. claims, _ := token.JwtClaims.ParseJwtToken(header)
  23. nowUser, _ := cache.GetNowUser(claims.ID)
  24. //TODO:校验签名 如果成功则上传否则不上传
  25. formFile, err := c.FormFile("file")
  26. if err != nil {
  27. c.JSON(http.StatusOK, common.ParamsInvalidResponse("获取文件数据失败", nil))
  28. return
  29. }
  30. //获取后缀
  31. sufx := path.Ext(formFile.Filename)
  32. //利用时间戳生成文件名
  33. fileNameInt := time.Now().Unix()
  34. fileNameStr := strconv.FormatInt(fileNameInt, 10)
  35. //新的文件名
  36. newfileName := fileNameStr + sufx
  37. _, err = os.Stat("uploadfiles")
  38. if os.IsNotExist(err) {
  39. os.Mkdir("./uploadfiles", os.ModePerm)
  40. }
  41. //保存文件
  42. filePath := filepath.Join("uploadfiles", "/", newfileName)
  43. c.SaveUploadedFile(formFile, filePath)
  44. //获取另外一个参数
  45. str := c.Request.FormValue("req")
  46. userFile := model.ReqUserFile{}
  47. json.Unmarshal([]byte(str), &userFile)
  48. savePath := "uploadfiles/" + newfileName
  49. distribute := service.FileService.Distribute(formFile.Filename, savePath, &userFile, nowUser.User.ID)
  50. c.JSON(http.StatusOK, distribute)
  51. }
  52. func (f *file) GetUserFiles(c *gin.Context) {
  53. header := c.GetHeader("Authorization")
  54. claims, _ := token.JwtClaims.ParseJwtToken(header)
  55. files := service.FileService.GetUserFiles(claims.ID)
  56. c.JSON(http.StatusOK, files)
  57. }
  58. // 转发文件
  59. func (f *file) ForwardingFile(c *gin.Context) {
  60. fileid := c.Query("fileid")
  61. header := c.GetHeader("Authorization")
  62. claims, _ := token.JwtClaims.ParseJwtToken(header)
  63. requsers := model.ReqUsers{}
  64. if err := c.ShouldBindJSON(&requsers); err != nil {
  65. c.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  66. return
  67. }
  68. uid, _ := strconv.Atoi(claims.ID)
  69. fid, _ := strconv.Atoi(fileid)
  70. forwardingFile := service.FileService.ForwardingFile(uid, fid, requsers.UsersId)
  71. c.JSON(http.StatusOK, forwardingFile)
  72. }
  73. // 查看文件
  74. func (f *file) ViewFile(c *gin.Context) {
  75. fileid := c.Query("fileid")
  76. service.FileService.GetFile(fileid, c)
  77. }
  78. // 下载文件
  79. func (f *file) DownloadFile(c *gin.Context) {
  80. fileid := c.Query("fileid")
  81. service.FileService.GetFile(fileid, c)
  82. }