Browse Source

服务端增加压缩RPC函数

master
xyiege 4 months ago
parent
commit
eb0716f6e0
  1. 5
      aufs/core/sendzip.go
  2. 40
      scagent/core/FileRpc.go

5
aufs/core/sendzip.go

@ -1,9 +1,9 @@
package core
import (
"fmt"
"aufs/config"
"aufs/util"
"fmt"
"net"
"net/http"
"path"
@ -12,6 +12,7 @@ import (
"time"
)
// 将文件打包成压缩包
func SendZip(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
// 选择文件,并生成zip包
@ -64,7 +65,7 @@ func SendZip(w http.ResponseWriter, r *http.Request) {
fmt.Println("archive is not exist!!!")
}
// 执行完 跳转到 首页
http.Redirect(w, r, "/", http.StatusFound)
// http.Redirect(w, r, "/", http.StatusFound)
}
// udp 模式发送文件

40
scagent/core/FileRpc.go

@ -3,6 +3,12 @@ package core
import (
"io"
"os"
"path"
"path/filepath"
"scagent/config"
"scagent/util"
"strings"
"time"
)
// FileService 提供文件传输服务
@ -16,6 +22,12 @@ type FileChunk struct {
IsLast bool // 是否为最后一个分片
}
// 压缩文件
type ZipBlock struct {
Curpath string // 当前路径
SelFile []string // 选中的文件
}
// Upload 接收文件分片并写入到本地文件
func (f *FileService) Upload(chunk FileChunk, reply *bool) error {
// 打开或创建文件,使用追加模式
@ -38,3 +50,31 @@ func (f *FileService) Upload(chunk FileChunk, reply *bool) error {
*reply = true
return nil
}
// 压缩文件
// 返回压缩包的名称
func (f *FileService) Compress(args *ZipBlock, reply *string) error {
// 实际路径
realFilePath := filepath.Join(config.G.FilePath, args.Curpath)
// zip 文件名
zpFileName := "BIU_" + time.Now().Format("20060102_150405") + ".zip"
// 创建zip 异步?
taskId := make(chan string)
// 异步压缩
go func() {
util.CompressToZip(zpFileName, realFilePath, args.SelFile)
taskId <- "arcok"
// fmt.Fprintln(w, "create archive:", err)
}()
// ZIP 文件的实际路径
ziprl := path.Join("./sync_zips/", zpFileName)
// zip 创建成功后
rest := <-taskId
// 如果压缩包生成成功以后再执行
if strings.EqualFold(strings.ToLower(rest), "arcok") {
// 返回压缩包名称
*reply = ziprl
}
return nil
}

Loading…
Cancel
Save