2021-09-11 14:04:09 +08:00
|
|
|
|
package api
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"io/fs"
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
"mayfly-go/base/biz"
|
|
|
|
|
|
"mayfly-go/base/ctx"
|
|
|
|
|
|
"mayfly-go/base/ginx"
|
|
|
|
|
|
"mayfly-go/base/utils"
|
2021-12-02 10:35:48 +08:00
|
|
|
|
"mayfly-go/base/ws"
|
2021-09-11 14:04:09 +08:00
|
|
|
|
"mayfly-go/server/devops/api/form"
|
|
|
|
|
|
"mayfly-go/server/devops/api/vo"
|
2021-06-07 17:22:07 +08:00
|
|
|
|
"mayfly-go/server/devops/application"
|
|
|
|
|
|
"mayfly-go/server/devops/domain/entity"
|
2021-12-02 10:35:48 +08:00
|
|
|
|
sysApplication "mayfly-go/server/sys/application"
|
2021-05-08 18:00:33 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type MachineFile struct {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
MachineFileApp application.MachineFile
|
|
|
|
|
|
MachineApp application.Machine
|
2021-12-02 10:35:48 +08:00
|
|
|
|
MsgApp sysApplication.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
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) MachineFiles(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
condition := &entity.MachineFile{MachineId: GetMachineId(g)}
|
|
|
|
|
|
rc.ResData = m.MachineFileApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineFileVO))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) SaveMachineFiles(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fileForm := new(form.MachineFileForm)
|
|
|
|
|
|
ginx.BindJsonAndValid(g, fileForm)
|
|
|
|
|
|
|
|
|
|
|
|
entity := new(entity.MachineFile)
|
|
|
|
|
|
utils.Copy(entity, fileForm)
|
|
|
|
|
|
|
|
|
|
|
|
entity.SetBaseInfo(rc.LoginAccount)
|
|
|
|
|
|
|
|
|
|
|
|
m.MachineFileApp.Save(entity)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) DeleteFile(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
m.MachineFileApp.Delete(fid)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*** sftp相关操作 */
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) ReadFileContent(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
readPath := g.Query("path")
|
|
|
|
|
|
readType := g.Query("type")
|
|
|
|
|
|
|
2021-05-08 20:50:34 +08:00
|
|
|
|
dataByte, fileInfo := m.MachineFileApp.ReadFile(fid, readPath)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
// 如果是读取文件内容,则校验文件大小
|
|
|
|
|
|
if readType != "1" {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
biz.IsTrue(fileInfo.Size() < max_read_size, "文件超过1m,请使用下载查看")
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
rc.ReqParam = fmt.Sprintf("path: %s", readPath)
|
|
|
|
|
|
// 如果读取类型为下载,则下载文件,否则获取文件内容
|
|
|
|
|
|
if readType == "1" {
|
|
|
|
|
|
// 截取文件名,如/usr/local/test.java -》 test.java
|
|
|
|
|
|
path := strings.Split(readPath, "/")
|
|
|
|
|
|
rc.Download(dataByte, path[len(path)-1])
|
|
|
|
|
|
} else {
|
|
|
|
|
|
rc.ResData = string(dataByte)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) GetDirEntry(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
readPath := g.Query("path")
|
|
|
|
|
|
|
|
|
|
|
|
if !strings.HasSuffix(readPath, "/") {
|
|
|
|
|
|
readPath = readPath + "/"
|
|
|
|
|
|
}
|
2021-05-08 20:50:34 +08:00
|
|
|
|
fis := m.MachineFileApp.ReadDir(fid, readPath)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
fisVO := make([]vo.MachineFileInfo, 0)
|
|
|
|
|
|
for _, fi := range fis {
|
|
|
|
|
|
fisVO = append(fisVO, vo.MachineFileInfo{
|
|
|
|
|
|
Name: fi.Name(),
|
|
|
|
|
|
Size: fi.Size(),
|
|
|
|
|
|
Path: readPath + fi.Name(),
|
|
|
|
|
|
Type: getFileType(fi.Mode()),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
rc.ResData = fisVO
|
|
|
|
|
|
rc.ReqParam = fmt.Sprintf("path: %s", readPath)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) WriteFileContent(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
|
|
|
|
|
|
|
|
|
|
|
form := new(form.MachineFileUpdateForm)
|
|
|
|
|
|
ginx.BindJsonAndValid(g, form)
|
|
|
|
|
|
path := form.Path
|
|
|
|
|
|
|
2021-05-08 20:50:34 +08:00
|
|
|
|
m.MachineFileApp.WriteFileContent(fid, path, []byte(form.Content))
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
rc.ReqParam = fmt.Sprintf("path: %s", path)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) UploadFile(rc *ctx.ReqCtx) {
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
|
|
file, _ := fileheader.Open()
|
2021-11-11 15:56:02 +08:00
|
|
|
|
bytes, _ := ioutil.ReadAll(file)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
rc.ReqParam = fmt.Sprintf("path: %s", path)
|
2021-12-02 10:35:48 +08:00
|
|
|
|
|
|
|
|
|
|
la := rc.LoginAccount
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
|
switch t := err.(type) {
|
|
|
|
|
|
case *biz.BizError:
|
|
|
|
|
|
m.MsgApp.CreateAndSend(la, ws.ErrMsg("文件上传失败", fmt.Sprintf("执行文件上传失败:\n<-e errCode: %d, errMsg: %s", t.Code(), t.Error())))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
m.MachineFileApp.UploadFile(fid, path, fileheader.Filename, bytes)
|
|
|
|
|
|
// 保存消息并发送文件上传成功通知
|
|
|
|
|
|
machine := m.MachineApp.GetById(m.MachineFileApp.GetById(fid).MachineId)
|
|
|
|
|
|
m.MsgApp.CreateAndSend(la, ws.SuccessMsg("文件上传成功", fmt.Sprintf("[%s]文件已成功上传至 %s[%s:%s]", fileheader.Filename, machine.Name, machine.Ip, path)))
|
|
|
|
|
|
}()
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineFile) RemoveFile(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fid := GetMachineFileId(g)
|
2021-05-08 20:50:34 +08:00
|
|
|
|
// mid := GetMachineId(g)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
path := g.Query("path")
|
|
|
|
|
|
|
2021-05-08 20:50:34 +08:00
|
|
|
|
m.MachineFileApp.RemoveFile(fid, path)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
rc.ReqParam = fmt.Sprintf("path: %s", path)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getFileType(fm fs.FileMode) string {
|
|
|
|
|
|
if fm.IsDir() {
|
|
|
|
|
|
return dir
|
|
|
|
|
|
}
|
|
|
|
|
|
return file
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetMachineFileId(g *gin.Context) uint64 {
|
|
|
|
|
|
fileId, _ := strconv.Atoi(g.Param("fileId"))
|
|
|
|
|
|
biz.IsTrue(fileId != 0, "fileId错误")
|
|
|
|
|
|
return uint64(fileId)
|
|
|
|
|
|
}
|