Browse Source

调整单文件存放

master
453530270@qq.com 2 years ago
parent
commit
3b13da0321
  1. BIN
      fssc/fssc
  2. BIN
      fsv2/fstc
  3. 170
      fsv2/handler/handler.go
  4. 3
      fsv2/main.go

BIN
fssc/fssc

Binary file not shown.

BIN
fsv2/fstc

Binary file not shown.

170
fsv2/handler/handler.go

@ -6,18 +6,13 @@ import (
"html/template"
"io"
"mime"
"net"
"path"
"strings"
"time"
"net/http"
"net/rpc"
"os"
"path/filepath"
"xtcfs/config"
"xtcfs/transfer"
"xtcfs/util"
"github.com/chyok/st/web"
@ -97,18 +92,25 @@ func ReceiveHandler(w http.ResponseWriter, r *http.Request) {
}
filename := filepath.FromSlash(params["filename"])
// fpath
bfpath := r.Header.Get("Fpath")
//debug
fmt.Printf("header params:%v", r.Header)
fmt.Printf("header params:%v\n", r.Header)
fmt.Printf("fpath is:%s,afbs64 is:%s\n", bfpath, util.Base64dec(bfpath))
fmt.Printf("Downloading [%s]...\n", filename)
dirPath := filepath.Dir(filename)
// 拼装完整路径
nfname := filepath.Join(config.G.FilePath, util.Base64dec(bfpath))
dirPath := filepath.Dir(nfname)
//dirPath := filepath.Dir(filename)
err = os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
out, err := os.Create(filename)
out, err := os.Create(nfname)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@ -135,7 +137,7 @@ func ReceiveHandler(w http.ResponseWriter, r *http.Request) {
}
// 输出接收结果
fmt.Fprintf(w, "接收成功,并已经完成解压缩")
// fmt.Fprintf(w, "接收成功,并已经完成解压缩")
}
// 文件服务
@ -158,156 +160,6 @@ func FileServerHandler(w http.ResponseWriter, r *http.Request) {
}
}
// udp 方式发送zip文件
func SendZip(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
// 选择文件,并生成zip包
// 文件
zipfarr := r.Form["zipfiles"]
// 服务器ip地址
serip := r.Form["serverip"]
if serip[0] == "" {
http.Error(w, "remote server ip is blank!", http.StatusInternalServerError)
return
}
// 选中的路径
wtculpath := r.Form["curpath"]
// 实际路径
realFilePath := filepath.Join(config.G.FilePath, wtculpath[0])
// 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("./files/", zpFileName)
// zip 创建成功后
rest := <-taskId
// 有压缩包 才可以操作
if strings.EqualFold(strings.ToLower(rest), "arcok") {
fmt.Println("archive is sending...")
// 创建udp 渠道发送数据
// 1、获取udp addr
remoteAddr, err := net.ResolveUDPAddr("udp", serip[0]+":9099")
if err != nil {
fmt.Printf("Failed to resolve %s: %v\n", serip[0], err)
return
}
// 2、 监听端口
conn, err := net.DialUDP("udp", nil, remoteAddr)
if err != nil {
fmt.Printf("Failed to dial %s: %v\n", serip[0], err)
return
}
defer conn.Close()
// 3、在端口发送数据
message := fmt.Sprintf("%s%s%s", config.G.DeviceName, "|", "sender")
// 向链接通道发送数据 数据包头
conn.Write([]byte(message))
// 发送文件
go func() {
err := transfer.SendFiles(ziprl, fmt.Sprintf("http://%s", remoteAddr))
if err != nil {
fmt.Printf("Send file to %s error: %s\n", remoteAddr, err)
}
}()
// 页面上显示
fmt.Fprintf(w, "File:%s,been sent successfully.", zpFileName)
} else {
fmt.Println("archive is not exist!!!")
}
}
// 下载zip
func Downzip(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
// 选择文件,并生成zip包
// 文件
zipfarr := r.Form["zipfiles"]
// 服务器ip地址
serip := r.Form["serverip"]
if serip[0] == "" {
http.Error(w, "remote server ip is blank!", http.StatusInternalServerError)
return
}
// fmt.Println(r.Form)
// 选中的路径
wtculpath := r.Form["curpath"]
// 实际路径
realFilePath := filepath.Join(config.G.FilePath, wtculpath[0])
// zip 文件名
zpFileName := "BIU_" + time.Now().Format("20060102_150405") + ".zip"
// 创建zip
err := util.CompressToZip(zpFileName, realFilePath, zipfarr)
if err != nil {
fmt.Println("create zip error", err)
}
// 停止下,检查zip是否创建成功
time.Sleep(1200)
_, err = os.Stat(realFilePath + "/" + zpFileName)
if err != nil {
// 调用httpd rpc
fmt.Println("RPC Server IP:", serip[0])
rpcServer := serip[0] + ":9080"
client, err := rpc.DialHTTP("tcp", rpcServer)
if err != nil {
fmt.Println("Http rpc error :", err)
}
// 组装url 静态文件
// url := "http://" + config.G.LocalIP + ":" + config.G.Port + "/files/" + zpFileName
// 动态
url := "http://" + config.G.LocalIP + ":" + config.G.Port + "/files?zp=" + zpFileName
args := Args{url, zpFileName}
var reply int
err = client.Call("Rbup.DecompressZip", args, &reply)
if err != nil {
fmt.Println("rpc call error :", err)
// 输出执行结果
fmt.Fprintln(w, "rpc call error:", err)
} else {
// 输出下载链接
fmt.Fprintf(w, "<a href='%s'>%s</a><br/>\n", url, url)
fmt.Fprintf(w, "RPC execute result :%d<br/>\n", reply)
}
} else {
fmt.Fprintf(w, "archive is created faild")
}
}
// 文件下载
func Dfiles(w http.ResponseWriter, r *http.Request) {
// url中获取参数
query := r.URL.Query()
zpfile := query.Get("zp")
// 实际路径
zpFileName := filepath.Join(config.G.FilePath, "/files/", zpfile)
// fileInfo, err := os.Stat(zpFileName)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// fmt.Println(fileInfo)
http.ServeFile(w, r, zpFileName)
}
// 发送拦截
func SendHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {

3
fsv2/main.go

@ -6,7 +6,6 @@ import (
"os"
"xtcfs/config"
"xtcfs/discovery"
"xtcfs/handler"
"github.com/urfave/cli/v2"
@ -24,7 +23,7 @@ func initConfig(c *cli.Context) error {
// 目标接收端,接收发送来的文件
func receiveClient() error {
// 加入udp 监听
go discovery.Listen()
//go discovery.Listen()
// 显示状态等
http.HandleFunc("/", handler.ReceiveHandler)

Loading…
Cancel
Save