Browse Source

增加base64的自定义函数

master
453530270@qq.com 2 years ago
parent
commit
427aedece6
  1. 72
      fssc/internal/handler/handler.go
  2. 14
      fssc/internal/util/util.go
  3. 2
      fssc/main.go
  4. 3
      fssc/web/embed.go
  5. 14
      fssc/web/list.tmpl

72
fssc/internal/handler/handler.go

@ -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

14
fssc/internal/util/util.go

@ -2,6 +2,7 @@ package util
import ( import (
"archive/zip" "archive/zip"
"encoding/base64"
"fmt" "fmt"
"io" "io"
"io/fs" "io/fs"
@ -231,3 +232,16 @@ func Zip(dst io.Writer, src string) error {
return nil 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)
}

2
fssc/main.go

@ -38,6 +38,8 @@ func sendClient() error {
http.Handle("/static/", http.StripPrefix("/static/", http.Handle("/static/", http.StripPrefix("/static/",
http.FileServer(http.FS(web.StaticFs)))) http.FileServer(http.FS(web.StaticFs))))
http.HandleFunc("/download/", handler.FileServerHandler) http.HandleFunc("/download/", handler.FileServerHandler)
// 文件更新包
http.HandleFunc("/up", handler.UpServer)
// udp 传送文件 // udp 传送文件
http.HandleFunc("/sendZip", handler.SendZip) http.HandleFunc("/sendZip", handler.SendZip)

3
fssc/web/embed.go

@ -2,8 +2,9 @@ package web
import "embed" import "embed"
//go:embed static
// var CssFs embed.FS // var CssFs embed.FS
//
//go:embed static
var StaticFs embed.FS var StaticFs embed.FS
//go:embed list.tmpl //go:embed list.tmpl

14
fssc/web/list.tmpl

@ -107,7 +107,7 @@
<h2>{{ .DownloadPath }}</h2> <h2>{{ .DownloadPath }}</h2>
<form action="/sendZip" method="post" class="form-inline"> <form action="/sendZip" method="post" class="form-inline">
<div class="row"> <div class="row">
<p class="lead">运行目录:{{ .DownloadPath }}</p> <p class="lead">运行目录:{{ .Rundir }}</p>
</div> </div>
<div class="row"> <div class="row">
@ -135,16 +135,14 @@
<div class="row"> <div class="row">
<div class="form-group"> <div class="form-group">
<button class="btn btn-primary" type="submit">以压缩包模式发送文件</button> <button class="btn btn-primary" type="submit">以压缩包模式发送文件</button>
<a class="btn btn-primary" type="buttton">单文件模式发送</a>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<ul> <ul class="list-group">
{{ range .Files }} {{ range .Files }}
<li> <li class="list-group-item">
<input class="mfile" type="checkbox" name="zipfiles" value="{{.Name}}"> <input class="mfile" type="checkbox" name="zipfiles" value="{{.Name}}">
{{ if .IsDir }} {{ if .IsDir }}
<svg class="folder-icon" viewBox="0 0 24 24"> <svg class="folder-icon" viewBox="0 0 24 24">
@ -155,7 +153,9 @@
<svg class="file-icon" viewBox="0 0 24 24"> <svg class="file-icon" viewBox="0 0 24 24">
<path fill="currentColor" d="M14 2H6C4.9 2 4 2.9 4 4V20C4 21.1 4.9 22 6 22H18C19.1 22 20 21.1 20 20V8L14 2M13 9V3.5L18.5 9H13Z" /> <path fill="currentColor" d="M14 2H6C4.9 2 4 2.9 4 4V20C4 21.1 4.9 22 6 22H18C19.1 22 20 21.1 20 20V8L14 2M13 9V3.5L18.5 9H13Z" />
</svg> </svg>
{{ .Name }} <span class="tips">{{.Info.Size}}</span><span class="tips"> {{.Info.ModTime.Format "2006-01-02 15:04:05"}} </span> <a href="/up?p={{b64en $.DownloadPath}}&f={{b64en .Name}}" class="button" title="更新{{ .Name }}">{{ .Name }}</a>
<span style="display:none" class="badge">{{.Info.Size}}</span><span class="badge" > {{.Info.ModTime.Format "2006-01-02 15:04:05"}} </span>
{{ end }} {{ end }}
</li> </li>

Loading…
Cancel
Save