2 changed files with 232 additions and 0 deletions
@ -0,0 +1,110 @@ |
|||
package core |
|||
|
|||
import ( |
|||
"fmt" |
|||
"fss/config" |
|||
"fss/util" |
|||
"io" |
|||
"mime" |
|||
"net/http" |
|||
"os" |
|||
"path/filepath" |
|||
"strings" |
|||
"time" |
|||
) |
|||
|
|||
// 接收发送来的文件
|
|||
func ReceiveHandler(w http.ResponseWriter, r *http.Request) { |
|||
switch r.Method { |
|||
case http.MethodGet: |
|||
//
|
|||
fmt.Fprintf(w, "%s:接收文件中...", r.Host) |
|||
case http.MethodPost: |
|||
// receive file and save
|
|||
file, header, err := r.FormFile("file") |
|||
if err != nil { |
|||
http.Error(w, err.Error(), http.StatusBadRequest) |
|||
return |
|||
} |
|||
defer file.Close() |
|||
|
|||
_, params, err := mime.ParseMediaType(header.Header.Get("Content-Disposition")) |
|||
if err != nil { |
|||
http.Error(w, err.Error(), http.StatusBadRequest) |
|||
return |
|||
} |
|||
filename := filepath.FromSlash(params["filename"]) |
|||
|
|||
// fpath
|
|||
bfpath := r.Header.Get("Fpath") |
|||
|
|||
//debug
|
|||
// 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) |
|||
// 拼装完整路径
|
|||
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 |
|||
} |
|||
// 检查文件是否存在
|
|||
if util.IsFileExist(nfname) { |
|||
// 当前时间
|
|||
curtime := time.Now().Format("2006/01/02") |
|||
// 创建备份文件夹
|
|||
bkdir := filepath.Join(config.G.FilePath, "backup", curtime) |
|||
// 递归创建文件夹
|
|||
serr := os.MkdirAll(bkdir, os.ModePerm) |
|||
// 创建错误 返回空
|
|||
if serr != nil { |
|||
return |
|||
} |
|||
|
|||
// 文件重命名
|
|||
//ddname := filename + "_backup_" + time.Now().Format("20060102150405")
|
|||
ddname := filename + "_backup_" + time.Now().Format("20060102150405") |
|||
// 文件移动到备份目录下,并做压缩包
|
|||
bkzip := filepath.Join(bkdir, ddname) |
|||
zfarr := []string{nfname} |
|||
util.ZipFiles(bkzip+".zip", zfarr, bkdir) |
|||
//渠道
|
|||
// 加上路径
|
|||
//fnewname := filepath.Join(bkdir, ddname)
|
|||
//os.Rename(nfname, fnewname)
|
|||
} |
|||
|
|||
out, err := os.Create(nfname) |
|||
if err != nil { |
|||
http.Error(w, err.Error(), http.StatusInternalServerError) |
|||
return |
|||
} |
|||
defer out.Close() |
|||
|
|||
_, err = io.Copy(out, file) |
|||
if err != nil { |
|||
http.Error(w, err.Error(), http.StatusInternalServerError) |
|||
return |
|||
} |
|||
// 如果单文件,需要先备份
|
|||
|
|||
// 如果收到的是zip文件,自动给解压缩
|
|||
suf := strings.Split(filename, ".") |
|||
if suf[1] == "zip" { |
|||
// 传入实际路径
|
|||
go util.DecompressZip(nfname) |
|||
} |
|||
|
|||
fmt.Printf("[√] Download [%s] Success.\n", filename) |
|||
//
|
|||
default: |
|||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
|||
} |
|||
|
|||
// 输出接收结果
|
|||
// fmt.Fprintf(w, "接收成功,并已经完成解压缩")
|
|||
} |
|||
Loading…
Reference in new issue