file.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. nowSysUser, _ := cache.GetNowSysUser(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.ReqSysUserFile{}
  47. json.Unmarshal([]byte(str), &userFile)
  48. savePath := "uploadfiles/" + newfileName
  49. distribute := service.FileService.Distribute(formFile.Filename, savePath, &userFile, nowSysUser.ID)
  50. c.JSON(http.StatusOK, distribute)
  51. }
  52. func (f *file) GetSysUserFiles(c *gin.Context) {
  53. header := c.GetHeader("Authorization")
  54. claims, _ := token.JwtClaims.ParseJwtToken(header)
  55. var search model.SearchSysUserFiles
  56. err := c.ShouldBindJSON(&search)
  57. if err != nil {
  58. c.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  59. return
  60. }
  61. files := service.FileService.GetSysUserFiles(claims.ID, search)
  62. c.JSON(http.StatusOK, files)
  63. }
  64. // 转发文件
  65. func (f *file) ForwardingFile(c *gin.Context) {
  66. fileid := c.Query("fileId")
  67. header := c.GetHeader("Authorization")
  68. claims, _ := token.JwtClaims.ParseJwtToken(header)
  69. requsers := model.ReqSysUsers{}
  70. if err := c.ShouldBindJSON(&requsers); err != nil {
  71. c.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  72. return
  73. }
  74. uid, _ := strconv.Atoi(claims.ID)
  75. fid, _ := strconv.Atoi(fileid)
  76. forwardingFile := service.FileService.ForwardingFile(uid, fid, requsers.SysUsersId)
  77. c.JSON(http.StatusOK, forwardingFile)
  78. }
  79. // 查看文件
  80. func (f *file) ViewFile(c *gin.Context) {
  81. fileId := c.Query("fileId")
  82. service.FileService.GetFile(fileId, c)
  83. }
  84. // 下载文件
  85. func (f *file) DownloadFile(c *gin.Context) {
  86. fileid := c.Query("fileId")
  87. service.FileService.GetFile(fileid, c)
  88. }