diff --git a/fssc/internal/handler/handler.go b/fssc/internal/handler/handler.go index 393441c..b020f73 100644 --- a/fssc/internal/handler/handler.go +++ b/fssc/internal/handler/handler.go @@ -3,8 +3,6 @@ package handler import ( "fmt" "html/template" - "io" - "mime" "net" "path" "strings" @@ -61,73 +59,17 @@ func ReceiveStatus(w http.ResponseWriter, r *http.Request) { // fmt.Fprintf(w, r.Method) } -// 接收 上传 -func ReceiveHandler(w http.ResponseWriter, r *http.Request) { +/** +** 文件更新服务器 +**/ +func UpServer(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: - // serve upload page for receive - // 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) - // + fmt.Fprint(w, "UpServer") default: 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) } + // 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 { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/fssc/internal/util/util.go b/fssc/internal/util/util.go index 2ab5699..1ef9ed2 100644 --- a/fssc/internal/util/util.go +++ b/fssc/internal/util/util.go @@ -2,6 +2,7 @@ package util import ( "archive/zip" + "encoding/base64" "fmt" "io" "io/fs" @@ -231,3 +232,16 @@ func Zip(dst io.Writer, src string) error { return nil }) } + +// base64 encode +// url safe +func Bas64end(str string) string { + // bdata:= + return base64.URLEncoding.EncodeToString([]byte(str)) +} + +// base64 url safe uneconde +func Base64dec(bsstr string) string { + dedc, _ := base64.URLEncoding.DecodeString(bsstr) + return string(dedc) +} diff --git a/fssc/main.go b/fssc/main.go index 79a1d84..efb34f8 100644 --- a/fssc/main.go +++ b/fssc/main.go @@ -38,6 +38,8 @@ func sendClient() error { http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(web.StaticFs)))) http.HandleFunc("/download/", handler.FileServerHandler) + // 文件更新包 + http.HandleFunc("/up", handler.UpServer) // udp 传送文件 http.HandleFunc("/sendZip", handler.SendZip) diff --git a/fssc/web/embed.go b/fssc/web/embed.go index fc08ed1..c112e0b 100644 --- a/fssc/web/embed.go +++ b/fssc/web/embed.go @@ -2,8 +2,9 @@ package web import "embed" -//go:embed static // var CssFs embed.FS +// +//go:embed static var StaticFs embed.FS //go:embed list.tmpl diff --git a/fssc/web/list.tmpl b/fssc/web/list.tmpl index 1323656..54b3e59 100644 --- a/fssc/web/list.tmpl +++ b/fssc/web/list.tmpl @@ -107,7 +107,7 @@

{{ .DownloadPath }}

-

运行目录:{{ .DownloadPath }}

+

运行目录:{{ .Rundir }}

@@ -135,16 +135,14 @@
- 单文件模式发送 +
- -
-
    +
      {{ range .Files }} -
    • +
    • {{ if .IsDir }} @@ -155,7 +153,9 @@ - {{ .Name }} {{.Info.Size}} {{.Info.ModTime.Format "2006-01-02 15:04:05"}} + {{ .Name }} + {{.Info.ModTime.Format "2006-01-02 15:04:05"}} + {{ end }}