file.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package controller
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "iot_manager_service/app/file/dao"
  6. "iot_manager_service/app/file/model"
  7. "iot_manager_service/app/file/service"
  8. dao2 "iot_manager_service/app/user/dao"
  9. "iot_manager_service/util/cache"
  10. "iot_manager_service/util/common"
  11. "iot_manager_service/util/token"
  12. "net/http"
  13. "os"
  14. "path"
  15. "path/filepath"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. var FileController = new(file)
  21. type file struct{}
  22. func (f *file) Upload(c *gin.Context) {
  23. header := c.GetHeader("Authorization")
  24. claims, _ := token.JwtClaims.ParseJwtToken(header)
  25. uploader, _ := strconv.Atoi(claims.ID)
  26. //校验权限 -- 4代表上传权限
  27. authUpload := CheckUserAuth(uploader, "4")
  28. if !authUpload {
  29. c.JSON(http.StatusForbidden, common.StatusForbidden("您没有上传权限,请联系管理员。", nil))
  30. return
  31. }
  32. // 获取表单字段的值
  33. categoryName := c.PostForm("categoryName")
  34. iv := c.PostForm("iv")
  35. str := c.PostForm("authId")
  36. authId := strings.Replace(str, ",", "", -1)
  37. formFile, err := c.FormFile("file")
  38. if err != nil {
  39. c.JSON(http.StatusOK, common.ParamsInvalidResponse("获取文件数据失败", nil))
  40. return
  41. }
  42. //获取后缀
  43. sufx := path.Ext(formFile.Filename)
  44. //利用时间戳生成文件名
  45. fileNameInt := time.Now().Unix()
  46. fileNameStr := strconv.FormatInt(fileNameInt, 10)
  47. //新的文件名
  48. newfileName := fileNameStr + sufx
  49. currentDir, _ := os.Getwd()
  50. parentDir := filepath.Dir(currentDir)
  51. folderPath := filepath.Join(parentDir, "uploadfiles")
  52. _, err = os.Stat(folderPath)
  53. if os.IsNotExist(err) {
  54. os.Mkdir(folderPath, os.ModePerm)
  55. }
  56. //保存文件
  57. filePath := filepath.Join(folderPath, "/", newfileName)
  58. c.SaveUploadedFile(formFile, filePath)
  59. savePath := "/uploadfiles/" + newfileName
  60. currentTimeValue := time.Now()
  61. upload := service.FileService.Upload(dao.File{
  62. OriginalName: strings.TrimSuffix(formFile.Filename, filepath.Ext(formFile.Filename)),
  63. EncryptedName: formFile.Filename,
  64. SavePath: savePath,
  65. CategoryName: categoryName,
  66. AuthId: authId,
  67. SuffixName: path.Ext(formFile.Filename),
  68. UploadTime: &currentTimeValue,
  69. Uploader: uploader,
  70. Icon: strings.TrimPrefix(path.Ext(formFile.Filename), ".") + ".png",
  71. Iv: iv,
  72. })
  73. c.JSON(http.StatusOK, upload)
  74. }
  75. func (f *file) Distribute(c *gin.Context) {
  76. //获取当前用户
  77. header := c.GetHeader("Authorization")
  78. claims, _ := token.JwtClaims.ParseJwtToken(header)
  79. nowSysUser, _ := cache.GetNowSysUser(claims.ID)
  80. var reqUserFile model.ReqSysUserFile
  81. err := c.ShouldBindJSON(&reqUserFile)
  82. if err != nil {
  83. c.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  84. return
  85. }
  86. distribute := service.FileService.Distribute(&reqUserFile, int(nowSysUser.ID))
  87. c.JSON(http.StatusOK, distribute)
  88. }
  89. func (f *file) GetSysUserFiles(c *gin.Context) {
  90. header := c.GetHeader("Authorization")
  91. claims, _ := token.JwtClaims.ParseJwtToken(header)
  92. var search model.SearchSysUserFiles
  93. err := c.ShouldBindJSON(&search)
  94. if err != nil {
  95. c.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  96. return
  97. }
  98. files := service.FileService.GetSysUserFiles(claims.ID, search)
  99. c.JSON(http.StatusOK, files)
  100. }
  101. // 转发文件
  102. func (f *file) ForwardingFile(c *gin.Context) {
  103. header := c.GetHeader("Authorization")
  104. claims, _ := token.JwtClaims.ParseJwtToken(header)
  105. uid, _ := strconv.Atoi(claims.ID)
  106. requsers := model.ReqSysUsers{}
  107. if err := c.ShouldBind(&requsers); err != nil {
  108. c.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  109. return
  110. }
  111. authView := CheckUserAuth(uid, "3")
  112. if !authView {
  113. c.JSON(http.StatusForbidden, common.StatusForbidden("您没有【转发】权限,请联系管理员。", nil))
  114. return
  115. }
  116. for _, fid := range requsers.FilesId {
  117. auth, time, name := CheckFileAuth(uid, fid, "3")
  118. if time {
  119. c.JSON(http.StatusForbidden, common.StatusForbidden(fmt.Sprintf("文件【%s】操作时间已过,无权操作。", name), nil))
  120. return
  121. }
  122. if auth {
  123. c.JSON(http.StatusForbidden, common.StatusForbidden(fmt.Sprintf("文件【%s】不支持转发操作,请联系管理员。", name), nil))
  124. return
  125. }
  126. }
  127. forwardingFile := service.FileService.ForwardingFile(uid, requsers.FilesId, requsers.SysUsersId)
  128. c.JSON(http.StatusOK, forwardingFile)
  129. }
  130. // 查看文件
  131. func (f *file) ViewFile(c *gin.Context) {
  132. //校验权限
  133. header := c.GetHeader("Authorization")
  134. claims, _ := token.JwtClaims.ParseJwtToken(header)
  135. uid, _ := strconv.Atoi(claims.ID)
  136. authView := CheckUserAuth(uid, "1")
  137. if !authView {
  138. c.JSON(http.StatusForbidden, common.StatusForbidden("您没有【查看】权限,请联系管理员。", nil))
  139. return
  140. }
  141. fileId := c.Query("fileId")
  142. fid, _ := strconv.Atoi(fileId)
  143. auth, time, name := CheckFileAuth(uid, fid, "1")
  144. if time {
  145. c.JSON(http.StatusForbidden, common.StatusForbidden(fmt.Sprintf("文件【%s】操作时间已过,无权操作。", name), nil))
  146. return
  147. }
  148. if auth {
  149. c.JSON(http.StatusForbidden, common.StatusForbidden(fmt.Sprintf("文件【%s】不支持查看操作,请联系管理员。", name), nil))
  150. return
  151. }
  152. service.FileService.GetFile(fileId, c)
  153. }
  154. // 下载文件
  155. func (f *file) DownloadFile(c *gin.Context) {
  156. //校验权限
  157. header := c.GetHeader("Authorization")
  158. claims, _ := token.JwtClaims.ParseJwtToken(header)
  159. uid, _ := strconv.Atoi(claims.ID)
  160. authView := CheckUserAuth(uid, "2")
  161. if !authView {
  162. c.JSON(http.StatusForbidden, common.StatusForbidden("您没有【下载】权限,请联系管理员。", nil))
  163. return
  164. }
  165. fileId := c.Query("fileId")
  166. fid, _ := strconv.Atoi(fileId)
  167. auth, time, name := CheckFileAuth(uid, fid, "2")
  168. if time {
  169. c.JSON(http.StatusForbidden, common.StatusForbidden(fmt.Sprintf("文件【%s】操作时间已过,无权操作。", name), nil))
  170. return
  171. }
  172. if auth {
  173. c.JSON(http.StatusForbidden, common.StatusForbidden(fmt.Sprintf("文件【%s】不支持下载操作,请联系管理员。", name), nil))
  174. return
  175. }
  176. service.FileService.GetFile(fileId, c)
  177. }
  178. func (f *file) GetMyUploadFiles(c *gin.Context) {
  179. header := c.GetHeader("Authorization")
  180. claims, _ := token.JwtClaims.ParseJwtToken(header)
  181. files := service.FileService.GetMyUploadFiles(claims.ID)
  182. c.JSON(http.StatusOK, files)
  183. }
  184. func CheckUserAuth(uid int, authstr string) bool {
  185. userdao := dao2.SysUser{}
  186. user, _ := userdao.GetNowSysUser(uid)
  187. if !strings.Contains(user.AuthId, authstr) {
  188. return false
  189. }
  190. return true
  191. }
  192. func CheckFileAuth(uid, fileid int, authstr string) (bool, bool, string) {
  193. filedao := &dao.File{}
  194. fileInfo, _ := filedao.GetFileInfo(fileid, uid)
  195. targetTime, _ := time.Parse("2006-01-02 15:04:05 -0700 MST", fileInfo.EffectiveDate.String())
  196. now := time.Now()
  197. return !strings.Contains(fileInfo.AuthId, authstr), !now.Before(targetTime), fileInfo.OriginalName
  198. }