4 changed files with 103 additions and 124 deletions
@ -0,0 +1,80 @@ |
|||||
|
package core |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"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 *bool) 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
|
||||
|
dirPath := filepath.Dir(info.FileName) |
||||
|
logger.Info("SendFileInfo", zap.String("dirPath", dirPath)) |
||||
|
// 检查文件夹是否存在
|
||||
|
if _, err := os.Stat(dirPath); os.IsNotExist(err) { |
||||
|
// 文件夹不存在,创建它
|
||||
|
if err := os.MkdirAll(dirPath, 0755); err != nil { |
||||
|
return err |
||||
|
} |
||||
|
} |
||||
|
*reply = true |
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
// SendFileChunk 接收文件块并写入文件
|
||||
|
func (f *UpFileService) SendFileChunk(chunk FileChunk, reply *bool) error { |
||||
|
// filePath := filepath.Join("received_files", chunk.FileName)
|
||||
|
fmt.Printf("recive file :%s", chunk.FileName) |
||||
|
// 合并为实际路径
|
||||
|
// filePath := filepath.Join("received_files", chunk.FileName)
|
||||
|
filePath := chunk.FileName |
||||
|
|
||||
|
// 打开文件,使用追加模式
|
||||
|
file, err := os.OpenFile(filePath, os.O_WRONLY, 0644) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
defer file.Close() |
||||
|
|
||||
|
// 移动到指定偏移量
|
||||
|
if _, err := file.Seek(chunk.Offset, io.SeekStart); err != nil { |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
// 写入数据
|
||||
|
if _, err := file.Write(chunk.Data); err != nil { |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
*reply = true |
||||
|
return nil |
||||
|
} |
||||
Loading…
Reference in new issue