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.
203 lines
4.9 KiB
203 lines
4.9 KiB
package core
|
|
|
|
import (
|
|
"aufs/config"
|
|
"aufs/util"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/rpc/jsonrpc"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// 压缩文件
|
|
type ZipBlock struct {
|
|
Basepath string // 基础路径
|
|
Curpath string // 当前路径
|
|
SelFile []string // 选中的文件
|
|
RemoteAddr string // 远程主机地址,带端口
|
|
RemotePath string // 远程主机的存储路径
|
|
}
|
|
|
|
// 返回的结果结构
|
|
type FsResp struct {
|
|
Status int `json:"status"`
|
|
Message string `json:"message"`
|
|
Data map[string]interface{} `json:"data"`
|
|
}
|
|
|
|
// 远程调用压缩包
|
|
func SendZip(w http.ResponseWriter, r *http.Request) {
|
|
r.ParseForm()
|
|
|
|
// 文件
|
|
zipfarr := r.Form["sfiles"]
|
|
// zipfarr 切割成数组
|
|
zipfarr = strings.Split(zipfarr[0], ",")
|
|
|
|
// 选中的路径,可以为空
|
|
wtculpath := r.Form["curpath"]
|
|
|
|
// 服务器ip地址
|
|
serip := r.Form["serverip"]
|
|
if serip[0] == "" {
|
|
http.Error(w, "remote server ip is blank!", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// 获取远程服务器信息
|
|
remoteaddr := r.Form["remoteaddr"]
|
|
if remoteaddr[0] == "" {
|
|
http.Error(w, "remote server addr is blank!", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// 远程的路径信息
|
|
remotepath := r.Form["remotepath"]
|
|
if remotepath[0] == "" {
|
|
http.Error(w, "remote server path is blank!", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// 构建
|
|
zipblock := ZipBlock{
|
|
Basepath: "/www/wwwroot",
|
|
Curpath: wtculpath[0],
|
|
SelFile: zipfarr,
|
|
RemoteAddr: remoteaddr[0],
|
|
RemotePath: remotepath[0],
|
|
}
|
|
|
|
// 从serip 切割出ip 和端口
|
|
splitip := strings.Split(serip[0], ":")
|
|
if len(splitip) != 2 {
|
|
http.Error(w, "remote server ip is error!", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
srcip := splitip[0]
|
|
sport := splitip[1]
|
|
//
|
|
service := fmt.Sprintf("%v:%v", srcip, sport)
|
|
client, err := jsonrpc.Dial("tcp", service)
|
|
if err != nil {
|
|
// fmt.Fprintf(w, "jsonrpc dial faild %v", err)
|
|
http.Error(w, "jsonrpc dial faild "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// 调用远程方法
|
|
var reply string
|
|
err = client.Call("FileService.Compress", zipblock, &reply)
|
|
// 执行完成后退出
|
|
defer client.Close()
|
|
|
|
if err != nil {
|
|
// fmt.Fprintf(w, "jsonrpc call faild %v", err)
|
|
http.Error(w, "jsonrpc call faild "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// 输出内容
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// w.Write([]byte(reply))
|
|
|
|
// 封装json
|
|
resp := FsResp{
|
|
Status: 200,
|
|
Message: "success",
|
|
Data: map[string]interface{}{"reply": reply},
|
|
}
|
|
json.NewEncoder(w).Encode(resp)
|
|
}
|
|
|
|
// 将文件打包成压缩包
|
|
func SendZip00(w http.ResponseWriter, r *http.Request) {
|
|
r.ParseForm()
|
|
// 选择文件,并生成zip包
|
|
// 文件
|
|
zipfarr := r.Form["sfiles"]
|
|
|
|
// 服务器ip地址
|
|
serip := r.Form["serverip"]
|
|
if serip[0] == "" {
|
|
http.Error(w, "remote server ip is blank!", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
tpath := ""
|
|
// 选中的路径,可以为空
|
|
wtculpath := r.Form["curpath"]
|
|
if wtculpath != nil {
|
|
tpath = util.Base64dec(wtculpath[0])
|
|
}
|
|
// 实际路径
|
|
realFilePath := filepath.Join(config.G.FilePath, tpath)
|
|
|
|
// zip 文件名
|
|
zpFileName := "BIU_" + time.Now().Format("20060102_150405") + ".zip"
|
|
|
|
// 创建zip 异步?
|
|
taskId := make(chan string)
|
|
go func() {
|
|
util.CompressToZip(zpFileName, realFilePath, zipfarr)
|
|
taskId <- "arcok"
|
|
// fmt.Fprintln(w, "create archive:", err)
|
|
}()
|
|
// go util.CompressToZip(zpFileName, realFilePath, zipfarr)
|
|
fmt.Println("archive is createding...")
|
|
|
|
// 当前运行的目录
|
|
|
|
// ZIP 文件的实际路径
|
|
ziprl := path.Join("./sync_zips/", zpFileName)
|
|
|
|
// zip 创建成功后
|
|
rest := <-taskId
|
|
// 有压缩包 才可以操作
|
|
if strings.EqualFold(strings.ToLower(rest), "arcok") {
|
|
fmt.Println("archive is sending...")
|
|
// 创建udp 渠道发送数据
|
|
message := fmt.Sprintf("%s%s%s", config.G.DeviceName, "|", "sender")
|
|
UdpSendFile(serip[0], ziprl, zpFileName, message, w)
|
|
} else {
|
|
fmt.Println("archive is not exist!!!")
|
|
}
|
|
// 执行完 跳转到 首页
|
|
// http.Redirect(w, r, "/", http.StatusFound)
|
|
}
|
|
|
|
// udp 模式发送文件
|
|
/*
|
|
* serip 服务器ip,
|
|
* absfilepath 发送文件的实际路径,
|
|
* fname 文件名
|
|
* message 携带部分信息的消息
|
|
* http.ResponseWriter
|
|
*/
|
|
func UdpSendFile(serip string, absfilepath string, fname string, message string, w http.ResponseWriter) {
|
|
|
|
// 1、获取udp addr
|
|
remoteAddr, err := net.ResolveUDPAddr("udp", serip+":9099")
|
|
if err != nil {
|
|
return
|
|
}
|
|
// 2、 监听端口
|
|
conn, err := net.DialUDP("udp", nil, remoteAddr)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
|
|
// 3、在端口发送数据
|
|
// message := fmt.Sprintf("%s%s%s", config.G.DeviceName, "|", "sender")
|
|
// 向链接通道发送数据 数据包头
|
|
conn.Write([]byte(message))
|
|
// 发送文件
|
|
go func() {
|
|
err := util.SendFiles(absfilepath, fmt.Sprintf("http://%s/rc", remoteAddr))
|
|
if err != nil {
|
|
fmt.Printf("Send file to %s error: %s\n", remoteAddr, err)
|
|
}
|
|
}()
|
|
}
|
|
|