Browse Source

模板自定义函数多个参数

master
453530270@qq.com 2 years ago
parent
commit
dc5bb67903
  1. 8
      fssc/by.bat
  2. 5
      fssc/internal/handler/webconsole.go
  3. 30
      fssc/internal/util/util.go
  4. 1
      fssc/web/wbconsole.tmpl

8
fssc/by.bat

@ -0,0 +1,8 @@
@echo off
title 控制台中心服务FOR LINUX
color 16
set GOOS=linux
set GOARCH=amd64
set CGO_ENABLED=0
go build -o fstc main.go

5
fssc/internal/handler/webconsole.go

@ -84,16 +84,17 @@ func WebConsole(w http.ResponseWriter, r *http.Request) {
data.FileName = filepath.Base(realFilePath)
}
// add self template function
fmap := template.FuncMap{"b64en": util.Bas64end}
fmap := template.FuncMap{"b64en": util.Bas64end, "hshcal": util.CalacHash}
// 文件列表模板
tmpl, err := template.New("console").Funcs(fmap).Parse(web.WbConsole)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
// http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

30
fssc/internal/util/util.go

@ -2,6 +2,7 @@ package util
import (
"archive/zip"
"crypto/sha256"
"encoding/base64"
"fmt"
"fssc/config"
@ -250,3 +251,32 @@ func Base64dec(bsstr string) string {
dedc, _ := base64.URLEncoding.DecodeString(bsstr)
return string(dedc)
}
// 计算文件的hash
func CalacHash(rfile string, relpath string) string {
// 获取到真实地址
rpath := filepath.Join(config.G.FilePath, rfile)
// fmt.Printf("hash file calac is :%s\n", rpath)
// if os.IsNotExist(rpath) {
// return "0x00"
// }
//
file, err := os.Open(rpath)
if err != nil {
panic(err)
}
defer file.Close()
// initlize hash object
hash := sha256.New()
// write file into hash object
if _, err := io.Copy(hash, file); err != nil {
panic(err)
}
// get hash value
hashBytes := hash.Sum(nil)
//converto to hash string
hastString := fmt.Sprintf("%x", hashBytes)
return hastString
}

1
fssc/web/wbconsole.tmpl

@ -105,6 +105,7 @@
{{ else }}
<span class="icon file-icon"></span>
<a href="javascript:void(0);">{{.Name}}</a>
HASH256: {{.Name $.DownloadPath|hshcal}}
{{ end }}
</div>

Loading…
Cancel
Save