自动更新管控端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

89 lines
2.3 KiB

package core
import (
"io"
"os"
"path/filepath"
"scagent/util"
"go.uber.org/zap"
)
type UpFileService struct{}
// FileInfo 包含文件的元信息
type FileInfo struct {
FileName string
FileSize int64
}
// FileChunk 表示文件的一部分
type FileChunk struct {
Data []byte
FileName string
Offset int64
IsLast bool
}
// 发送文件的RPC
// SendFile 接收文件信息(名称和大小)
func (f *UpFileService) SendFileInfo(info FileInfo, reply *string) error {
logger := util.NewProductionLogger()
defer logger.Sync()
// 创建保存文件的目录
// if err := os.MkdirAll("received_files", 0755); err != nil {
// return err
// }
// 提取路径 /www/wwwroot/bidemo.com/BIU_20250918_183144.zip
// fmt.Printf("SendFileInfo fileName: %s\n", info.FileName)
dirPath := filepath.Dir(info.FileName)
logger.Info("SendFileInfo", zap.String("dirPath", dirPath))
// *reply = fmt.Sprintf("folder: %s is exists", dirPath)
// 检查文件夹是否存在
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
// 文件夹不存在,创建它
if err := os.MkdirAll(dirPath, 0755); err != nil {
logger.Error("SendFileInfo mkdir failed", zap.Error(err))
// *reply = fmt.Sprintf("mkdir folder: %s failed", dirPath)
// return err
}
}
// *reply = fmt.Sprintf("folder: %s is exists", dirPath)
// 返回true的字符串
*reply = "true"
return nil
}
// SendFileChunk 接收文件块并写入文件
func (f *UpFileService) SendFileChunk(chunk FileChunk, reply *bool) error {
// 日志
logger := util.NewProductionLogger()
defer logger.Sync()
filePath := filepath.Join("received_files", chunk.FileName)
// fmt.Printf("recive file :%s", chunk.FileName)
// 合并为实际路径
// filePath := filepath.Join(dirPath, chunk.FileName)
// filePath := chunk.FileName
// 打开文件,使用追加模式
file, err := os.OpenFile(filePath, os.O_RDWR, 0644)
if err != nil {
logger.Error("SendFileChunk OpenFile failed", zap.Error(err))
}
defer file.Close()
// 移动到指定偏移量
if _, err := file.Seek(chunk.Offset, io.SeekStart); err != nil {
logger.Error("SendFileChunk Seek failed", zap.Error(err))
}
// 写入数据
if _, err := file.Write(chunk.Data); err != nil {
logger.Error("SendFileChunk Write failed", zap.Error(err))
}
*reply = true
return nil
}