2021-09-11 14:04:09 +08:00
|
|
|
|
package api
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
2022-08-29 21:43:24 +08:00
|
|
|
|
"io"
|
2021-05-08 18:00:33 +08:00
|
|
|
|
"io/fs"
|
2022-09-09 18:26:08 +08:00
|
|
|
|
"mayfly-go/internal/machine/api/form"
|
|
|
|
|
|
"mayfly-go/internal/machine/api/vo"
|
|
|
|
|
|
"mayfly-go/internal/machine/application"
|
2023-11-12 20:14:44 +08:00
|
|
|
|
"mayfly-go/internal/machine/config"
|
2022-09-09 18:26:08 +08:00
|
|
|
|
"mayfly-go/internal/machine/domain/entity"
|
2023-10-30 17:34:56 +08:00
|
|
|
|
"mayfly-go/internal/machine/mcm"
|
2023-07-03 21:42:04 +08:00
|
|
|
|
msgapp "mayfly-go/internal/msg/application"
|
2023-10-10 17:39:46 +08:00
|
|
|
|
msgdto "mayfly-go/internal/msg/application/dto"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/biz"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/errorx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/ginx"
|
2023-09-08 22:24:45 +08:00
|
|
|
|
"mayfly-go/pkg/logx"
|
2023-01-14 16:29:52 +08:00
|
|
|
|
"mayfly-go/pkg/req"
|
2023-10-20 21:31:46 +08:00
|
|
|
|
"mayfly-go/pkg/utils/anyx"
|
2023-09-08 22:24:45 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2023-09-06 18:06:52 +08:00
|
|
|
|
"mayfly-go/pkg/utils/timex"
|
2023-09-08 22:24:45 +08:00
|
|
|
|
"mime/multipart"
|
|
|
|
|
|
"path/filepath"
|
2022-09-26 18:08:12 +08:00
|
|
|
|
"sort"
|
2021-05-08 18:00:33 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
2023-09-08 22:24:45 +08:00
|
|
|
|
"sync"
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type MachineFile struct {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
MachineFileApp application.MachineFile
|
2023-07-03 21:42:04 +08:00
|
|
|
|
MsgApp msgapp.Msg
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
file = "-"
|
|
|
|
|
|
dir = "d"
|
|
|
|
|
|
link = "l"
|
2021-06-07 17:22:07 +08:00
|
|
|
|
max_read_size = 1 * 1024 * 1024
|
2021-05-08 18:00:33 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *MachineFile) MachineFiles(rc *req.Ctx) {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
condition := &entity.MachineFile{MachineId: GetMachineId(g)}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
res, err := m.MachineFileApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineFileVO))
|
|
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
rc.ResData = res
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *MachineFile) SaveMachineFiles(rc *req.Ctx) {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
fileForm := new(form.MachineFileForm)
|
2023-07-08 20:05:55 +08:00
|
|
|
|
entity := ginx.BindJsonAndCopyTo[*entity.MachineFile](rc.GinCtx, fileForm, new(entity.MachineFile))
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
2023-07-08 20:05:55 +08:00
|
|
|
|
rc.ReqParam = fileForm
|
2023-11-07 21:05:21 +08:00
|
|
|
|
biz.ErrIsNil(m.MachineFileApp.Save(rc.MetaCtx, entity))
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *MachineFile) DeleteFile(rc *req.Ctx) {
|
2023-12-13 14:01:13 +08:00
|
|
|
|
biz.ErrIsNil(m.MachineFileApp.DeleteById(rc.MetaCtx, GetMachineFileId(rc.GinCtx)))
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*** sftp相关操作 */
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *MachineFile) CreateFile(rc *req.Ctx) {
|
2022-07-04 20:21:24 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
|
2023-07-08 20:05:55 +08:00
|
|
|
|
form := ginx.BindJsonAndValid(g, new(form.MachineCreateFileForm))
|
2022-07-04 20:21:24 +08:00
|
|
|
|
path := form.Path
|
|
|
|
|
|
|
2023-10-12 12:14:56 +08:00
|
|
|
|
attrs := collx.Kvs("path", path)
|
2023-10-30 17:34:56 +08:00
|
|
|
|
var mi *mcm.MachineInfo
|
2023-09-11 22:59:13 +08:00
|
|
|
|
var err error
|
2022-07-04 20:21:24 +08:00
|
|
|
|
if form.Type == dir {
|
2023-09-09 23:34:26 +08:00
|
|
|
|
attrs["type"] = "目录"
|
2023-09-11 22:59:13 +08:00
|
|
|
|
mi, err = m.MachineFileApp.MkDir(fid, form.Path)
|
2022-07-04 20:21:24 +08:00
|
|
|
|
} else {
|
2023-09-09 23:34:26 +08:00
|
|
|
|
attrs["type"] = "文件"
|
2023-09-11 22:59:13 +08:00
|
|
|
|
mi, err = m.MachineFileApp.CreateFile(fid, form.Path)
|
2022-07-04 20:21:24 +08:00
|
|
|
|
}
|
2023-09-11 22:59:13 +08:00
|
|
|
|
attrs["machine"] = mi
|
|
|
|
|
|
rc.ReqParam = attrs
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "创建目录失败: %s")
|
2022-07-04 20:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *MachineFile) ReadFileContent(rc *req.Ctx) {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
readPath := g.Query("path")
|
|
|
|
|
|
readType := g.Query("type")
|
|
|
|
|
|
|
2023-09-11 22:59:13 +08:00
|
|
|
|
sftpFile, mi, err := m.MachineFileApp.ReadFile(fid, readPath)
|
2023-10-12 12:14:56 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("machine", mi, "path", readPath)
|
2023-09-11 22:59:13 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "打开文件失败: %s")
|
2022-01-28 11:30:11 +08:00
|
|
|
|
defer sftpFile.Close()
|
|
|
|
|
|
|
|
|
|
|
|
fileInfo, _ := sftpFile.Stat()
|
2023-09-09 23:34:26 +08:00
|
|
|
|
filesize := fileInfo.Size()
|
2021-05-08 18:00:33 +08:00
|
|
|
|
// 如果是读取文件内容,则校验文件大小
|
|
|
|
|
|
if readType != "1" {
|
2023-09-09 23:34:26 +08:00
|
|
|
|
biz.IsTrue(filesize < max_read_size, "文件超过1m,请使用下载查看")
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 如果读取类型为下载,则下载文件,否则获取文件内容
|
|
|
|
|
|
if readType == "1" {
|
|
|
|
|
|
// 截取文件名,如/usr/local/test.java -》 test.java
|
|
|
|
|
|
path := strings.Split(readPath, "/")
|
2022-01-28 11:30:11 +08:00
|
|
|
|
rc.Download(sftpFile, path[len(path)-1])
|
2021-05-08 18:00:33 +08:00
|
|
|
|
} else {
|
2022-08-29 21:43:24 +08:00
|
|
|
|
datas, err := io.ReadAll(sftpFile)
|
2022-01-28 11:30:11 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "读取文件内容失败: %s")
|
|
|
|
|
|
rc.ResData = string(datas)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *MachineFile) GetDirEntry(rc *req.Ctx) {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
readPath := g.Query("path")
|
2023-08-04 12:22:21 +08:00
|
|
|
|
rc.ReqParam = fmt.Sprintf("path: %s", readPath)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
if !strings.HasSuffix(readPath, "/") {
|
|
|
|
|
|
readPath = readPath + "/"
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
fis, err := m.MachineFileApp.ReadDir(fid, readPath)
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "读取目录失败: %s")
|
|
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
fisVO := make([]vo.MachineFileInfo, 0)
|
|
|
|
|
|
for _, fi := range fis {
|
|
|
|
|
|
fisVO = append(fisVO, vo.MachineFileInfo{
|
2023-09-06 18:06:52 +08:00
|
|
|
|
Name: fi.Name(),
|
|
|
|
|
|
Size: fi.Size(),
|
|
|
|
|
|
Path: readPath + fi.Name(),
|
|
|
|
|
|
Type: getFileType(fi.Mode()),
|
|
|
|
|
|
Mode: fi.Mode().String(),
|
|
|
|
|
|
ModTime: timex.DefaultFormat(fi.ModTime()),
|
2021-05-08 18:00:33 +08:00
|
|
|
|
})
|
2023-09-06 18:06:52 +08:00
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
2022-09-26 18:08:12 +08:00
|
|
|
|
sort.Sort(vo.MachineFileInfos(fisVO))
|
2021-05-08 18:00:33 +08:00
|
|
|
|
rc.ResData = fisVO
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-05 00:26:00 +08:00
|
|
|
|
func (m *MachineFile) GetDirSize(rc *req.Ctx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
readPath := g.Query("path")
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
size, err := m.MachineFileApp.GetDirSize(fid, readPath)
|
|
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
rc.ResData = size
|
2023-07-05 00:26:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) GetFileStat(rc *req.Ctx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
readPath := g.Query("path")
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
res, err := m.MachineFileApp.FileStat(fid, readPath)
|
|
|
|
|
|
biz.ErrIsNil(err, res)
|
|
|
|
|
|
rc.ResData = res
|
2023-07-05 00:26:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *MachineFile) WriteFileContent(rc *req.Ctx) {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
|
|
|
|
|
|
form := new(form.MachineFileUpdateForm)
|
|
|
|
|
|
ginx.BindJsonAndValid(g, form)
|
|
|
|
|
|
path := form.Path
|
|
|
|
|
|
|
2023-09-11 22:59:13 +08:00
|
|
|
|
mi, err := m.MachineFileApp.WriteFileContent(fid, path, []byte(form.Content))
|
2023-10-12 12:14:56 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("machine", mi, "path", path)
|
2023-09-11 22:59:13 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "打开文件失败: %s")
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *MachineFile) UploadFile(rc *req.Ctx) {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
path := g.PostForm("path")
|
2021-11-12 14:35:34 +08:00
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
fileheader, err := g.FormFile("file")
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "读取文件失败: %s")
|
2023-11-12 20:14:44 +08:00
|
|
|
|
|
|
|
|
|
|
maxUploadFileSize := config.GetMachine().UploadMaxFileSize
|
|
|
|
|
|
biz.IsTrue(fileheader.Size <= maxUploadFileSize, "文件大小不能超过%d字节", maxUploadFileSize)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
file, _ := fileheader.Open()
|
2023-09-09 23:34:26 +08:00
|
|
|
|
defer file.Close()
|
2021-12-02 10:35:48 +08:00
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
la := rc.GetLoginAccount()
|
2023-09-08 22:24:45 +08:00
|
|
|
|
defer func() {
|
2023-10-20 21:31:46 +08:00
|
|
|
|
if anyx.ToString(recover()) != "" {
|
2023-09-08 22:24:45 +08:00
|
|
|
|
logx.Errorf("文件上传失败: %s", err)
|
2023-10-20 21:31:46 +08:00
|
|
|
|
m.MsgApp.CreateAndSend(la, msgdto.ErrSysMsg("文件上传失败", fmt.Sprintf("执行文件上传失败:\n<-e : %s", err)))
|
2023-09-08 22:24:45 +08:00
|
|
|
|
}
|
2021-12-02 10:35:48 +08:00
|
|
|
|
}()
|
2023-09-08 22:24:45 +08:00
|
|
|
|
|
2023-09-11 22:59:13 +08:00
|
|
|
|
mi, err := m.MachineFileApp.UploadFile(fid, path, fileheader.Filename, file)
|
2023-10-12 12:14:56 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", path, fileheader.Filename))
|
2023-09-11 22:59:13 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "创建文件失败: %s")
|
2023-09-08 22:24:45 +08:00
|
|
|
|
// 保存消息并发送文件上传成功通知
|
2023-10-10 17:39:46 +08:00
|
|
|
|
m.MsgApp.CreateAndSend(la, msgdto.SuccessSysMsg("文件上传成功", fmt.Sprintf("[%s]文件已成功上传至 %s[%s:%s]", fileheader.Filename, mi.Name, mi.Ip, path)))
|
2023-09-08 22:24:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type FolderFile struct {
|
|
|
|
|
|
Dir string
|
|
|
|
|
|
Fileheader *multipart.FileHeader
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) UploadFolder(rc *req.Ctx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
|
|
|
|
|
|
mf, err := g.MultipartForm()
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "获取表单信息失败: %s")
|
|
|
|
|
|
basePath := mf.Value["basePath"][0]
|
|
|
|
|
|
biz.NotEmpty(basePath, "基础路径不能为空")
|
|
|
|
|
|
|
|
|
|
|
|
fileheaders := mf.File["files"]
|
|
|
|
|
|
biz.IsTrue(len(fileheaders) > 0, "文件不能为空")
|
|
|
|
|
|
allFileSize := collx.ArrayReduce(fileheaders, 0, func(i int64, fh *multipart.FileHeader) int64 {
|
|
|
|
|
|
return i + fh.Size
|
|
|
|
|
|
})
|
2023-11-12 20:14:44 +08:00
|
|
|
|
|
|
|
|
|
|
maxUploadFileSize := config.GetMachine().UploadMaxFileSize
|
|
|
|
|
|
biz.IsTrue(allFileSize <= maxUploadFileSize, "文件夹总大小不能超过%d字节", maxUploadFileSize)
|
2023-09-08 22:24:45 +08:00
|
|
|
|
|
|
|
|
|
|
paths := mf.Value["paths"]
|
|
|
|
|
|
|
|
|
|
|
|
folderName := filepath.Dir(paths[0])
|
2023-10-26 17:15:49 +08:00
|
|
|
|
mcli, err := m.MachineFileApp.GetMachineCli(fid, basePath+"/"+folderName)
|
|
|
|
|
|
biz.ErrIsNil(err)
|
2023-10-30 17:34:56 +08:00
|
|
|
|
mi := mcli.Info
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
sftpCli, err := mcli.GetSftpCli()
|
|
|
|
|
|
biz.ErrIsNil(err)
|
2023-10-12 12:14:56 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", basePath, folderName))
|
2023-09-08 22:24:45 +08:00
|
|
|
|
|
|
|
|
|
|
folderFiles := make([]FolderFile, len(paths))
|
|
|
|
|
|
// 先创建目录,并将其包装为folderFile结构
|
|
|
|
|
|
mkdirs := make(map[string]bool, 0)
|
|
|
|
|
|
for i, path := range paths {
|
|
|
|
|
|
dir := filepath.Dir(path)
|
|
|
|
|
|
// 目录已建,则无需重复建
|
|
|
|
|
|
if !mkdirs[dir] {
|
2023-09-11 22:59:13 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(sftpCli.MkdirAll(basePath+"/"+dir), "创建目录失败: %s")
|
2023-09-08 22:24:45 +08:00
|
|
|
|
mkdirs[dir] = true
|
|
|
|
|
|
}
|
|
|
|
|
|
folderFiles[i] = FolderFile{
|
|
|
|
|
|
Dir: dir,
|
|
|
|
|
|
Fileheader: fileheaders[i],
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 分组处理
|
2023-09-11 22:59:13 +08:00
|
|
|
|
groupNum := 30
|
2023-09-08 22:24:45 +08:00
|
|
|
|
chunks := collx.ArraySplit(folderFiles, groupNum)
|
|
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
// 设置要等待的协程数量
|
|
|
|
|
|
wg.Add(len(chunks))
|
|
|
|
|
|
|
2023-10-30 17:34:56 +08:00
|
|
|
|
isSuccess := true
|
2023-11-07 21:05:21 +08:00
|
|
|
|
la := rc.GetLoginAccount()
|
2023-09-08 22:24:45 +08:00
|
|
|
|
for _, chunk := range chunks {
|
|
|
|
|
|
go func(files []FolderFile, wg *sync.WaitGroup) {
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
// 协程执行完成后调用Done方法
|
|
|
|
|
|
wg.Done()
|
|
|
|
|
|
if err := recover(); err != nil {
|
2023-10-30 17:34:56 +08:00
|
|
|
|
isSuccess = false
|
2023-09-08 22:24:45 +08:00
|
|
|
|
logx.Errorf("文件上传失败: %s", err)
|
|
|
|
|
|
switch t := err.(type) {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
case errorx.BizError:
|
2023-10-10 17:39:46 +08:00
|
|
|
|
m.MsgApp.CreateAndSend(la, msgdto.ErrSysMsg("文件上传失败", fmt.Sprintf("执行文件上传失败:\n<-e errCode: %d, errMsg: %s", t.Code(), t.Error())))
|
2023-09-08 22:24:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
|
|
fileHeader := file.Fileheader
|
|
|
|
|
|
dir := file.Dir
|
|
|
|
|
|
file, _ := fileHeader.Open()
|
|
|
|
|
|
defer file.Close()
|
2023-09-11 22:59:13 +08:00
|
|
|
|
|
2023-09-08 22:24:45 +08:00
|
|
|
|
logx.Debugf("上传文件夹: dir=%s -> filename=%s", dir, fileHeader.Filename)
|
2023-09-11 22:59:13 +08:00
|
|
|
|
|
|
|
|
|
|
createfile, err := sftpCli.Create(fmt.Sprintf("%s/%s/%s", basePath, dir, fileHeader.Filename))
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "创建文件失败: %s")
|
|
|
|
|
|
defer createfile.Close()
|
|
|
|
|
|
io.Copy(createfile, file)
|
2023-09-08 22:24:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
}(chunk, &wg)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 等待所有协程执行完成
|
|
|
|
|
|
wg.Wait()
|
2023-10-30 17:34:56 +08:00
|
|
|
|
if isSuccess {
|
|
|
|
|
|
// 保存消息并发送文件上传成功通知
|
2023-11-07 21:05:21 +08:00
|
|
|
|
m.MsgApp.CreateAndSend(la, msgdto.SuccessSysMsg("文件上传成功", fmt.Sprintf("[%s]文件夹已成功上传至 %s[%s:%s]", folderName, mi.Name, mi.Ip, basePath)))
|
2023-10-30 17:34:56 +08:00
|
|
|
|
}
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *MachineFile) RemoveFile(rc *req.Ctx) {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
|
2023-09-07 16:33:53 +08:00
|
|
|
|
rmForm := new(form.MachineFileOpForm)
|
|
|
|
|
|
ginx.BindJsonAndValid(g, rmForm)
|
|
|
|
|
|
|
2023-09-11 22:59:13 +08:00
|
|
|
|
mi, err := m.MachineFileApp.RemoveFile(fid, rmForm.Path...)
|
2023-10-12 12:14:56 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("machine", mi, "path", rmForm.Path)
|
2023-09-11 22:59:13 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "删除文件失败: %s")
|
2023-09-07 16:33:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) CopyFile(rc *req.Ctx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
|
|
|
|
|
|
cpForm := new(form.MachineFileOpForm)
|
|
|
|
|
|
ginx.BindJsonAndValid(g, cpForm)
|
2023-09-11 22:59:13 +08:00
|
|
|
|
mi, err := m.MachineFileApp.Copy(fid, cpForm.ToPath, cpForm.Path...)
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "文件拷贝失败: %s")
|
2023-10-12 12:14:56 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("machine", mi, "cp", cpForm)
|
2023-09-07 16:33:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) MvFile(rc *req.Ctx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
|
|
|
|
|
|
cpForm := new(form.MachineFileOpForm)
|
|
|
|
|
|
ginx.BindJsonAndValid(g, cpForm)
|
2023-09-11 22:59:13 +08:00
|
|
|
|
mi, err := m.MachineFileApp.Mv(fid, cpForm.ToPath, cpForm.Path...)
|
2023-10-12 12:14:56 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("machine", mi, "mv", cpForm)
|
2023-09-11 22:59:13 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "文件移动失败: %s")
|
2023-09-07 16:33:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) Rename(rc *req.Ctx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
|
|
|
|
|
|
rename := new(form.MachineFileRename)
|
|
|
|
|
|
ginx.BindJsonAndValid(g, rename)
|
2023-09-11 22:59:13 +08:00
|
|
|
|
mi, err := m.MachineFileApp.Rename(fid, rename.Oldname, rename.Newname)
|
2023-10-12 12:14:56 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("machine", mi, "rename", rename)
|
2023-09-11 22:59:13 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "文件重命名失败: %s")
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getFileType(fm fs.FileMode) string {
|
|
|
|
|
|
if fm.IsDir() {
|
|
|
|
|
|
return dir
|
|
|
|
|
|
}
|
2022-07-04 20:21:24 +08:00
|
|
|
|
if fm.IsRegular() {
|
|
|
|
|
|
return file
|
|
|
|
|
|
}
|
|
|
|
|
|
return dir
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetMachineFileId(g *gin.Context) uint64 {
|
|
|
|
|
|
fileId, _ := strconv.Atoi(g.Param("fileId"))
|
|
|
|
|
|
biz.IsTrue(fileId != 0, "fileId错误")
|
|
|
|
|
|
return uint64(fileId)
|
|
|
|
|
|
}
|