自动更新管控端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

83 lines
1.8 KiB

package util
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"os"
"path/filepath"
)
// 遍历目录下的文件
// 是否只扫相对路径下
func GetDirFilePaths(dirPath string, relativeOnly bool) ([]string, error) {
var filePaths []string
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
if relativeOnly {
fileName := filepath.Base(path)
relativePath := filepath.ToSlash(filepath.Join(filepath.Base(dirPath), fileName))
filePaths = append(filePaths, relativePath)
} else {
filePaths = append(filePaths, filepath.ToSlash(path))
}
}
return nil
})
if err != nil {
return nil, err
}
return filePaths, nil
}
// 计算文件的hash
func CalacHash(rfile string) string {
// 排除软连接的情况
// Lstat 用于获取文件的状态,而不是文件的属性
finfo, err := os.Lstat(rfile)
if err != nil {
return ""
}
// 排除软连接
if finfo.Mode()&os.ModeSymlink != 0 {
fmt.Printf("file:%s,mode:%v\n", rfile, finfo.Mode())
return ""
}
// 获取到真实地址
// rpath := filepath.Join(config.G.FilePath, rfile)
//
file, err := os.Open(rfile)
if err != nil {
// panic(err)
fmt.Printf("err %s\n", 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
}
// url safe
func Bas64end(str string) string {
return base64.URLEncoding.EncodeToString([]byte(str))
}
// base64 url safe uneconde
func Base64dec(bsstr string) string {
dedc, _ := base64.URLEncoding.DecodeString(bsstr)
return string(dedc)
}