mirror of
https://gitee.com/dromara/mayfly-go
synced 2026-03-31 09:48:26 +08:00
feat: 新增统一文件模块,统一文件操作
This commit is contained in:
@@ -3,6 +3,7 @@ package application
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
fileapp "mayfly-go/internal/file/application"
|
||||
"mayfly-go/internal/machine/config"
|
||||
"mayfly-go/internal/machine/domain/entity"
|
||||
"mayfly-go/internal/machine/domain/repository"
|
||||
@@ -15,8 +16,7 @@ import (
|
||||
"mayfly-go/pkg/scheduler"
|
||||
"mayfly-go/pkg/utils/jsonx"
|
||||
"mayfly-go/pkg/utils/stringx"
|
||||
"os"
|
||||
"path"
|
||||
"mayfly-go/pkg/utils/timex"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
@@ -38,6 +38,7 @@ type machineTermOpAppImpl struct {
|
||||
base.AppImpl[*entity.MachineTermOp, repository.MachineTermOp]
|
||||
|
||||
machineCmdConfApp MachineCmdConf `inject:"MachineCmdConfApp"`
|
||||
fileApp fileapp.File `inject:"FileApp"`
|
||||
}
|
||||
|
||||
// 注入MachineTermOpRepo
|
||||
@@ -63,20 +64,14 @@ func (m *machineTermOpAppImpl) TermConn(ctx context.Context, cli *mcm.Cli, wsCon
|
||||
termOpRecord.MachineId = cli.Info.Id
|
||||
termOpRecord.Username = cli.Info.Username
|
||||
|
||||
// 回放文件路径为: 基础配置路径/机器编号/操作日期(202301)/day/hour/randstr.cast
|
||||
recRelPath := path.Join(cli.Info.Code, now.Format("200601"), fmt.Sprintf("%d", now.Day()), fmt.Sprintf("%d", now.Hour()))
|
||||
// 文件绝对路径
|
||||
recAbsPath := path.Join(config.GetMachine().TerminalRecPath, recRelPath)
|
||||
os.MkdirAll(recAbsPath, 0766)
|
||||
filename := fmt.Sprintf("%s.cast", stringx.RandByChars(18, stringx.LowerChars))
|
||||
f, err := os.OpenFile(path.Join(recAbsPath, filename), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0766)
|
||||
fileKey, wc, saveFileFunc, err := m.fileApp.NewWriter(ctx, "", fmt.Sprintf("mto_%d_%s.cast", termOpRecord.MachineId, timex.TimeNo()))
|
||||
if err != nil {
|
||||
return errorx.NewBiz("创建终端回放记录文件失败: %s", err.Error())
|
||||
}
|
||||
defer f.Close()
|
||||
defer saveFileFunc()
|
||||
|
||||
termOpRecord.RecordFilePath = path.Join(recRelPath, filename)
|
||||
recorder = mcm.NewRecorder(f)
|
||||
termOpRecord.FileKey = fileKey
|
||||
recorder = mcm.NewRecorder(wc)
|
||||
}
|
||||
|
||||
createTsParam := &mcm.CreateTerminalSessionParam{
|
||||
@@ -134,9 +129,8 @@ func (m *machineTermOpAppImpl) TimerDeleteTermOp() {
|
||||
return
|
||||
}
|
||||
|
||||
basePath := config.GetMachine().TerminalRecPath
|
||||
for _, termOp := range termOps {
|
||||
if err := m.DeleteTermOp(basePath, termOp); err != nil {
|
||||
if err := m.DeleteTermOp(termOp); err != nil {
|
||||
logx.Warnf("删除终端操作记录失败: %s", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -144,10 +138,10 @@ func (m *machineTermOpAppImpl) TimerDeleteTermOp() {
|
||||
}
|
||||
|
||||
// 删除终端记录即对应文件
|
||||
func (m *machineTermOpAppImpl) DeleteTermOp(basePath string, termOp *entity.MachineTermOp) error {
|
||||
func (m *machineTermOpAppImpl) DeleteTermOp(termOp *entity.MachineTermOp) error {
|
||||
if err := m.DeleteById(context.Background(), termOp.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.Remove(path.Join(basePath, termOp.RecordFilePath))
|
||||
return m.fileApp.Remove(context.TODO(), termOp.FileKey)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user