|
|
@ -3,8 +3,6 @@ package handler |
|
|
import ( |
|
|
import ( |
|
|
"fmt" |
|
|
"fmt" |
|
|
"html/template" |
|
|
"html/template" |
|
|
"io" |
|
|
|
|
|
"mime" |
|
|
|
|
|
"net" |
|
|
"net" |
|
|
"path" |
|
|
"path" |
|
|
"strings" |
|
|
"strings" |
|
|
@ -61,73 +59,17 @@ func ReceiveStatus(w http.ResponseWriter, r *http.Request) { |
|
|
// fmt.Fprintf(w, r.Method)
|
|
|
// fmt.Fprintf(w, r.Method)
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 接收 上传
|
|
|
/** |
|
|
func ReceiveHandler(w http.ResponseWriter, r *http.Request) { |
|
|
** 文件更新服务器 |
|
|
|
|
|
**/ |
|
|
|
|
|
func UpServer(w http.ResponseWriter, r *http.Request) { |
|
|
switch r.Method { |
|
|
switch r.Method { |
|
|
case http.MethodGet: |
|
|
case http.MethodGet: |
|
|
// serve upload page for receive
|
|
|
fmt.Fprint(w, "UpServer") |
|
|
// tmpl, err := template.New("index").Parse(web.UploadPage)
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
|
// return
|
|
|
|
|
|
// }
|
|
|
|
|
|
// // 获取当前设备名称
|
|
|
|
|
|
// err = tmpl.Execute(w, config.G.DeviceName)
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
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"]) |
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("Downloading [%s]...\n", filename) |
|
|
|
|
|
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) |
|
|
|
|
|
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(filename) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("[√] Download [%s] Success.\n", filename) |
|
|
|
|
|
//
|
|
|
|
|
|
default: |
|
|
default: |
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 输出接收结果
|
|
|
|
|
|
fmt.Fprintf(w, "接收成功,并已经完成解压缩") |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 文件服务
|
|
|
// 文件服务
|
|
|
@ -271,8 +213,10 @@ func SendHandler(w http.ResponseWriter, r *http.Request) { |
|
|
data.FileName = filepath.Base(realFilePath) |
|
|
data.FileName = filepath.Base(realFilePath) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// add self template function
|
|
|
|
|
|
fmap := template.FuncMap{"b64en": util.Bas64end} |
|
|
// 文件列表模板
|
|
|
// 文件列表模板
|
|
|
tmpl, err := template.New("list").Parse(web.ListPage) |
|
|
tmpl, err := template.New("list").Funcs(fmap).Parse(web.ListPage) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
http.Error(w, err.Error(), http.StatusInternalServerError) |
|
|
http.Error(w, err.Error(), http.StatusInternalServerError) |
|
|
return |
|
|
return |
|
|
|