123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package service
- import (
- "fmt"
- "path/filepath"
- "strings"
- "crazy-fox-backend-api/global"
- "crazy-fox-backend-api/model"
- "crazy-fox-backend-api/utils"
- "crazy-fox-backend-api/utils/fileop"
- "crazy-fox-backend-api/utils/zipfile"
- "github.com/gin-gonic/gin"
- "github.com/pkg/errors"
- )
- // fileManagerService 文件管理器服务
- type fileManagerService struct{}
- // GetList 获取文件列表
- func (This *fileManagerService) GetList(req model.FileManagerListReq) ([]model.FileInfo, int64) {
- req.Dir = This.buildAP(req.Dir, "")
- req.Key = strings.ToLower(req.Key)
- list, err := fileop.DirList(req.Dir)
- if err != nil {
- panic(errors.Wrap(err, "文件列表获取出错"))
- }
- filterList := make([]model.FileInfo, 0, len(list))
- for _, info := range list {
- if strings.Contains(strings.ToLower(info.Name), req.Key) {
- filterList = append(filterList, info)
- }
- }
- return filterList, int64(len(list))
- }
- // buildAP 构建绝对路径
- func (This *fileManagerService) buildAP(dir, fileName string) string {
- return filepath.Join(global.Config.SrcRootPath, dir, fileName)
- }
- // FileRename 文件重命名
- func (This *fileManagerService) FileRename(req model.FileRenameReq) error {
- oldName := This.buildAP(req.Dir, req.Old)
- newName := This.buildAP(req.Dir, req.New)
- return fileop.FileRename(oldName, newName)
- }
- // FileNew 新建文件夹
- func (This *fileManagerService) FileNew(req model.FileNameReq) error {
- filePath := This.buildAP(req.Dir, req.FileName)
- if fileop.PathExists(filePath) {
- return errors.New("文件名已存在")
- }
- return fileop.DirCreate(filePath)
- }
- // FileDel 删除文件
- func (This *fileManagerService) FileDel(req model.FileNameReq) error {
- filePath := This.buildAP(req.Dir, req.FileName)
- return fileop.FileDel(filePath)
- }
- // FileUpload 获取文件上传的完整路径
- func (This *fileManagerService) FileUpload(req model.FileUploadReq, c *gin.Context) error {
- file, err := c.FormFile("file")
- if err != nil {
- return errors.New("文件读取失败")
- }
- APDir := This.buildAP(req.Dir, "")
- filePath := filepath.Join(APDir, file.Filename)
- // 上传文件到指定的路径
- if err = c.SaveUploadedFile(file, filePath); err != nil {
- return errors.Wrap(err, "文件保存失败")
- }
- // 未开启自动解压
- if !req.AutoUnzip {
- return nil
- }
- // 文件后缀不是zip
- split := strings.Split(file.Filename, ".")
- fileType := strings.ToLower(split[len(split)-1])
- if fileType != "zip" {
- return nil
- }
- // 解压缩
- if err = zipfile.Decompress(filePath, APDir); err != nil {
- return errors.Wrap(err, "文件解压失败")
- }
- // 删除源压缩文件
- if err = fileop.FileDel(filePath); err != nil {
- return errors.Wrap(err, "文件删除失败")
- }
- return nil
- }
- // FileDownload 文件下载
- func (This *fileManagerService) FileDownload(req model.FileNameReq, isDir *bool) (string, error) {
- path := This.buildAP(req.Dir, req.FileName)
- if fileop.IsFile(path) {
- return path, nil
- }
- *isDir = true
- // 目录压缩后发送
- zipPath := fmt.Sprintf("%s_%d_%s", path, utils.CurTimestamp(), ".zip")
- if err := zipfile.Compress(zipPath, path); err != nil {
- return "", errors.Wrap(err, "目录压缩失败")
- }
- return zipPath, nil
- }
- // FileCompress 文件压缩
- func (This *fileManagerService) FileCompress(req model.FileZipReq) error {
- srcPath := This.buildAP(req.Dir, req.FileName)
- zipPath := This.buildAP(req.Dir, req.ZipName)
- if err := zipfile.Compress(zipPath, srcPath); err != nil {
- return errors.Wrap(err, "目录压缩失败")
- }
- return nil
- }
- // FileDecompress 文件解压
- func (This *fileManagerService) FileDecompress(req model.FileZipReq) error {
- destPath := This.buildAP(req.Dir, req.FileName)
- zipPath := This.buildAP(req.Dir, req.ZipName)
- if err := zipfile.Decompress(zipPath, destPath); err != nil {
- return errors.Wrap(err, "目录解压失败")
- }
- return nil
- }
|