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.
102 lines
2.4 KiB
102 lines
2.4 KiB
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"fss/config"
|
|
"fss/util"
|
|
"net"
|
|
"net/http"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func SendZip(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 = 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", remoteAddr))
|
|
if err != nil {
|
|
fmt.Printf("Send file to %s error: %s\n", remoteAddr, err)
|
|
}
|
|
}()
|
|
}
|
|
|