app_file.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package system
  2. import (
  3. "context"
  4. "github.com/gin-gonic/gin"
  5. "go.uber.org/zap"
  6. "google.golang.org/grpc"
  7. "google.golang.org/grpc/credentials/insecure"
  8. "io/ioutil"
  9. "net/http"
  10. "os"
  11. "path"
  12. "path/filepath"
  13. "server/global"
  14. "server/model/common/request"
  15. "server/model/common/response"
  16. "server/model/system"
  17. systemReq "server/model/system/request"
  18. proto "server/proto"
  19. "server/utils"
  20. "strconv"
  21. "strings"
  22. "time"
  23. )
  24. type FileApi struct{}
  25. func (b *FileApi) GetFileList(c *gin.Context) {
  26. var search systemReq.SearchAppFileParams
  27. err := c.ShouldBindJSON(&search)
  28. if err != nil {
  29. response.FailWithMessage(err.Error(), c)
  30. return
  31. }
  32. err = utils.Verify(search.PageInfo, utils.PageInfoVerify)
  33. if err != nil {
  34. response.FailWithMessage(err.Error(), c)
  35. return
  36. }
  37. list, total, err := fileService.GetFileInfoList(search)
  38. if err != nil {
  39. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  40. response.FailWithMessage("获取失败", c)
  41. return
  42. }
  43. response.OkWithDetailed(response.PageResult{
  44. List: list,
  45. Total: total,
  46. Page: search.PageInfo.Page,
  47. PageSize: search.PageInfo.PageSize,
  48. }, "获取成功", c)
  49. }
  50. func (b *FileApi) Upload(c *gin.Context) {
  51. id := utils.GetUserID(c)
  52. // 获取表单字段的值
  53. categoryName := c.PostForm("categoryName")
  54. str := c.PostForm("authId")
  55. iv := c.PostForm("iv")
  56. authId := strings.Replace(str, ",", "", -1)
  57. // 获取上传的文件
  58. file, err := c.FormFile("file")
  59. if err != nil {
  60. global.GVA_LOG.Error("获取文件数据失败", zap.Error(err))
  61. response.FailWithMessage("获取文件数据失败", c)
  62. return
  63. }
  64. //获取后缀
  65. sufx := path.Ext(file.Filename)
  66. //利用时间戳生成文件名
  67. fileNameInt := time.Now().Unix()
  68. fileNameStr := strconv.FormatInt(fileNameInt, 10)
  69. //新的文件名
  70. newfileName := fileNameStr + sufx
  71. currentDir, _ := os.Getwd()
  72. parentDir := filepath.Dir(currentDir)
  73. folderPath := filepath.Join(parentDir, "uploadfiles")
  74. _, err = os.Stat(folderPath)
  75. if os.IsNotExist(err) {
  76. os.Mkdir(folderPath, os.ModePerm)
  77. }
  78. //保存文件
  79. filePath := filepath.Join(folderPath, "/", newfileName)
  80. c.SaveUploadedFile(file, filePath)
  81. savePath := "/uploadfiles/" + newfileName
  82. currentTimeValue := time.Now()
  83. err = fileService.Upload(system.File{
  84. OriginalName: strings.TrimSuffix(file.Filename, filepath.Ext(file.Filename)),
  85. EncryptedName: file.Filename,
  86. SavePath: savePath,
  87. CategoryName: categoryName,
  88. AuthId: authId,
  89. SuffixName: path.Ext(file.Filename),
  90. UploadTime: &currentTimeValue,
  91. Uploader: int(id),
  92. Icon: strings.TrimPrefix(path.Ext(file.Filename), ".") + ".png",
  93. Iv: iv,
  94. })
  95. if err != nil {
  96. global.GVA_LOG.Error("上传失败!", zap.Error(err))
  97. response.FailWithMessage(err.Error(), c)
  98. return
  99. }
  100. response.OkWithMessage("上传成功", c)
  101. }
  102. func (b *FileApi) SetFileInfo(c *gin.Context) {
  103. var file systemReq.ChangeFileInfo
  104. err := c.ShouldBindJSON(&file)
  105. if err != nil {
  106. response.FailWithMessage(err.Error(), c)
  107. return
  108. }
  109. err = utils.Verify(file, utils.IdVerify)
  110. if err != nil {
  111. response.FailWithMessage(err.Error(), c)
  112. return
  113. }
  114. // 将整数数组转换为字符串切片
  115. var result string
  116. for _, v := range file.AuthId {
  117. result += strconv.Itoa(v)
  118. }
  119. err = fileService.SetFileInfo(system.File{
  120. ID: file.ID,
  121. CategoryName: file.CategoryName,
  122. AuthId: result,
  123. })
  124. if err != nil {
  125. global.GVA_LOG.Error("设置失败!", zap.Error(err))
  126. response.FailWithMessage("设置失败", c)
  127. return
  128. }
  129. response.OkWithMessage("设置成功", c)
  130. }
  131. func (b *FileApi) DeleteBaseFile(c *gin.Context) {
  132. var reqId request.GetById
  133. err := c.ShouldBindJSON(&reqId)
  134. if err != nil {
  135. response.FailWithMessage(err.Error(), c)
  136. return
  137. }
  138. err = utils.Verify(reqId, utils.IdVerify)
  139. if err != nil {
  140. response.FailWithMessage(err.Error(), c)
  141. return
  142. }
  143. err = fileService.DeleteFile(reqId.ID)
  144. if err != nil {
  145. global.GVA_LOG.Error("删除失败!", zap.Error(err))
  146. response.FailWithMessage(err.Error(), c)
  147. return
  148. }
  149. response.OkWithMessage("删除成功", c)
  150. }
  151. func (b *FileApi) Distribute(c *gin.Context) {
  152. id := utils.GetUserID(c)
  153. var file systemReq.Distribute
  154. err := c.ShouldBindJSON(&file)
  155. if err != nil {
  156. response.FailWithMessage(err.Error(), c)
  157. return
  158. }
  159. err = utils.Verify(file, utils.IdVerify)
  160. if err != nil {
  161. response.FailWithMessage(err.Error(), c)
  162. return
  163. }
  164. users, err := deptService.GetDeptsUsers(file.Depts)
  165. if err != nil {
  166. global.GVA_LOG.Error("操作失败!", zap.Error(err))
  167. response.FailWithMessage(err.Error(), c)
  168. return
  169. }
  170. var userFile []systemReq.UserFile
  171. var usersId []int64
  172. for _, user := range users {
  173. userFile = append(userFile, systemReq.UserFile{
  174. Operator: int(id),
  175. Receiver: int(user.ID),
  176. FileId: file.ID,
  177. EffectiveDate: file.EfectiveDate,
  178. OperationStatus: "下发",
  179. IsDeleted: 0,
  180. })
  181. usersId = append(usersId, int64(user.ID))
  182. }
  183. err = fileService.Distribute(userFile)
  184. if err != nil {
  185. global.GVA_LOG.Error("下发失败!", zap.Error(err))
  186. response.FailWithMessage(err.Error(), c)
  187. return
  188. }
  189. //下发消息
  190. conn, err := grpc.Dial("127.0.0.1:9090", grpc.WithTransportCredentials(insecure.NewCredentials()))
  191. if err != nil {
  192. global.GVA_LOG.Error("grpc远程连接错误!", zap.Error(err))
  193. response.FailWithMessage(err.Error(), c)
  194. }
  195. defer conn.Close()
  196. //建立连接
  197. client := proto.NewSendMessageClient(conn)
  198. resp, err := client.Distribution(context.Background(), &proto.DistributionRequest{Users: usersId})
  199. if resp.GetCode() != 200 && err != nil {
  200. global.GVA_LOG.Error("消息通知失败!", zap.Error(err))
  201. response.FailWithMessage(err.Error(), c)
  202. }
  203. response.OkWithMessage("下发成功", c)
  204. }
  205. func (b *FileApi) View(c *gin.Context) {
  206. var reqId request.GetById
  207. err := c.ShouldBindJSON(&reqId)
  208. if err != nil {
  209. response.FailWithMessage(err.Error(), c)
  210. return
  211. }
  212. file, err := fileService.View(reqId.ID)
  213. if err != nil {
  214. global.GVA_LOG.Error("查找失败!", zap.Error(err))
  215. response.FailWithMessage(err.Error(), c)
  216. return
  217. }
  218. currentDir, _ := os.Getwd()
  219. parentDir := filepath.Dir(currentDir)
  220. data, _ := ioutil.ReadFile(parentDir + file.SavePath)
  221. c.Data(http.StatusOK, "application/octet-stream", data)
  222. response.OkWithMessage("查看成功", c)
  223. }