A:更新工具类
This commit is contained in:
1 parent
39823db70a
commit
bae2721935
14 files changed
+598
-26
No files matched your search
@@ -34,7 +34,7 @@ type fileUtilsStruct struct{}
|
||||
// FileUtils 是对外暴露的文件工具对象
|
||||
var FileUtils = fileUtilsStruct{}
|
||||
|
||||
// 1. 检查文件或目录是否存在
|
||||
// Exists 1. 检查文件或目录是否存在
|
||||
func (fileUtilsStruct) Exists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return !os.IsNotExist(err)
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"hash"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -327,6 +328,31 @@ func (h hashUtilsStruct) HashFileMultipleAlgorithms(filename string, hashTypes [
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// HashThumbPath 将 Hash 转换为具体路径
|
||||
func (h hashUtilsStruct) HashThumbPath(
|
||||
basePath string,
|
||||
hash string,
|
||||
filename string, // 文件名,不带扩展名
|
||||
ext string, // 扩展名,不带点,如 "jpg", "webp"
|
||||
) string {
|
||||
var parts []string
|
||||
start := 0
|
||||
segments := []int{2, 2, 2}
|
||||
for _, length := range segments {
|
||||
end := start + length
|
||||
if end > len(hash) {
|
||||
break
|
||||
}
|
||||
parts = append(parts, hash[start:end])
|
||||
start = end
|
||||
}
|
||||
parts = append(parts, hash) // 最后一级目录为完整 hash
|
||||
|
||||
dir := filepath.Join(append([]string{basePath}, parts...)...)
|
||||
fullName := fmt.Sprintf("%s.%s", filename, ext)
|
||||
return filepath.Join(dir, fullName)
|
||||
}
|
||||
|
||||
// 便捷函数
|
||||
func (h hashUtilsStruct) MD5File(filename string) (string, error) {
|
||||
return h.HashFile(filename, MD5)
|
||||
|
||||
Reference in new issue
Block a user