filemanager.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package service
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "crazy-fox-backend-api/global"
  7. "crazy-fox-backend-api/model"
  8. "crazy-fox-backend-api/utils"
  9. "crazy-fox-backend-api/utils/fileop"
  10. "crazy-fox-backend-api/utils/zipfile"
  11. "github.com/gin-gonic/gin"
  12. "github.com/pkg/errors"
  13. )
  14. // fileManagerService 文件管理器服务
  15. type fileManagerService struct{}
  16. // GetList 获取文件列表
  17. func (This *fileManagerService) GetList(req model.FileManagerListReq) ([]model.FileInfo, int64) {
  18. req.Dir = This.buildAP(req.Dir, "")
  19. req.Key = strings.ToLower(req.Key)
  20. list, err := fileop.DirList(req.Dir)
  21. if err != nil {
  22. panic(errors.Wrap(err, "文件列表获取出错"))
  23. }
  24. filterList := make([]model.FileInfo, 0, len(list))
  25. for _, info := range list {
  26. if strings.Contains(strings.ToLower(info.Name), req.Key) {
  27. filterList = append(filterList, info)
  28. }
  29. }
  30. return filterList, int64(len(list))
  31. }
  32. // buildAP 构建绝对路径
  33. func (This *fileManagerService) buildAP(dir, fileName string) string {
  34. return filepath.Join(global.Config.SrcRootPath, dir, fileName)
  35. }
  36. // FileRename 文件重命名
  37. func (This *fileManagerService) FileRename(req model.FileRenameReq) error {
  38. oldName := This.buildAP(req.Dir, req.Old)
  39. newName := This.buildAP(req.Dir, req.New)
  40. return fileop.FileRename(oldName, newName)
  41. }
  42. // FileNew 新建文件夹
  43. func (This *fileManagerService) FileNew(req model.FileNameReq) error {
  44. filePath := This.buildAP(req.Dir, req.FileName)
  45. if fileop.PathExists(filePath) {
  46. return errors.New("文件名已存在")
  47. }
  48. return fileop.DirCreate(filePath)
  49. }
  50. // FileDel 删除文件
  51. func (This *fileManagerService) FileDel(req model.FileNameReq) error {
  52. filePath := This.buildAP(req.Dir, req.FileName)
  53. return fileop.FileDel(filePath)
  54. }
  55. // FileUpload 获取文件上传的完整路径
  56. func (This *fileManagerService) FileUpload(req model.FileUploadReq, c *gin.Context) error {
  57. file, err := c.FormFile("file")
  58. if err != nil {
  59. return errors.New("文件读取失败")
  60. }
  61. APDir := This.buildAP(req.Dir, "")
  62. filePath := filepath.Join(APDir, file.Filename)
  63. // 上传文件到指定的路径
  64. if err = c.SaveUploadedFile(file, filePath); err != nil {
  65. return errors.Wrap(err, "文件保存失败")
  66. }
  67. // 未开启自动解压
  68. if !req.AutoUnzip {
  69. return nil
  70. }
  71. // 文件后缀不是zip
  72. split := strings.Split(file.Filename, ".")
  73. fileType := strings.ToLower(split[len(split)-1])
  74. if fileType != "zip" {
  75. return nil
  76. }
  77. // 解压缩
  78. if err = zipfile.Decompress(filePath, APDir); err != nil {
  79. return errors.Wrap(err, "文件解压失败")
  80. }
  81. // 删除源压缩文件
  82. if err = fileop.FileDel(filePath); err != nil {
  83. return errors.Wrap(err, "文件删除失败")
  84. }
  85. return nil
  86. }
  87. // FileDownload 文件下载
  88. func (This *fileManagerService) FileDownload(req model.FileNameReq, isDir *bool) (string, error) {
  89. path := This.buildAP(req.Dir, req.FileName)
  90. if fileop.IsFile(path) {
  91. return path, nil
  92. }
  93. *isDir = true
  94. // 目录压缩后发送
  95. zipPath := fmt.Sprintf("%s_%d_%s", path, utils.CurTimestamp(), ".zip")
  96. if err := zipfile.Compress(zipPath, path); err != nil {
  97. return "", errors.Wrap(err, "目录压缩失败")
  98. }
  99. return zipPath, nil
  100. }
  101. // FileCompress 文件压缩
  102. func (This *fileManagerService) FileCompress(req model.FileZipReq) error {
  103. srcPath := This.buildAP(req.Dir, req.FileName)
  104. zipPath := This.buildAP(req.Dir, req.ZipName)
  105. if err := zipfile.Compress(zipPath, srcPath); err != nil {
  106. return errors.Wrap(err, "目录压缩失败")
  107. }
  108. return nil
  109. }
  110. // FileDecompress 文件解压
  111. func (This *fileManagerService) FileDecompress(req model.FileZipReq) error {
  112. destPath := This.buildAP(req.Dir, req.FileName)
  113. zipPath := This.buildAP(req.Dir, req.ZipName)
  114. if err := zipfile.Decompress(zipPath, destPath); err != nil {
  115. return errors.Wrap(err, "目录解压失败")
  116. }
  117. return nil
  118. }