A:增加照片任务管理
This commit is contained in:
1 parent
f1d9764d1d
commit
25cc429091
11 files changed
+512
-126
No files matched your search
@@ -22,14 +22,20 @@ type FileInfo struct {
|
||||
Ext string `json:"ext"`
|
||||
}
|
||||
|
||||
// fileUtilsStruct 用于封装文件工具方法
|
||||
type fileUtilsStruct struct{}
|
||||
|
||||
// FileUtils 是对外暴露的文件工具对象
|
||||
var FileUtils = fileUtilsStruct{}
|
||||
|
||||
// 1. 检查文件或目录是否存在
|
||||
func Exists(path string) bool {
|
||||
func (fileUtilsStruct) Exists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
// 2. 检查是否为目录
|
||||
func IsDir(path string) bool {
|
||||
func (fileUtilsStruct) IsDir(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
@@ -38,7 +44,7 @@ func IsDir(path string) bool {
|
||||
}
|
||||
|
||||
// 3. 检查是否为文件
|
||||
func IsFile(path string) bool {
|
||||
func (fileUtilsStruct) IsFile(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
@@ -47,7 +53,7 @@ func IsFile(path string) bool {
|
||||
}
|
||||
|
||||
// 4. 获取指定目录下的所有文件夹
|
||||
func GetDirectories(dirPath string) ([]string, error) {
|
||||
func (fileUtilsStruct) GetDirectories(dirPath string) ([]string, error) {
|
||||
var dirs []string
|
||||
|
||||
entries, err := os.ReadDir(dirPath)
|
||||
@@ -65,7 +71,7 @@ func GetDirectories(dirPath string) ([]string, error) {
|
||||
}
|
||||
|
||||
// 5. 获取指定目录下指定格式的文件
|
||||
func GetFilesByExtension(dirPath string, extensions ...string) ([]string, error) {
|
||||
func (fileUtilsStruct) GetFilesByExtension(dirPath string, extensions ...string) ([]string, error) {
|
||||
var files []string
|
||||
|
||||
// 标准化扩展名(确保以.开头)
|
||||
@@ -97,8 +103,8 @@ func GetFilesByExtension(dirPath string, extensions ...string) ([]string, error)
|
||||
}
|
||||
|
||||
// 6. 找到指定目录下第一个符合格式的文件
|
||||
func GetFirstFileByExtension(dirPath string, extensions ...string) (string, error) {
|
||||
files, err := GetFilesByExtension(dirPath, extensions...)
|
||||
func (f fileUtilsStruct) GetFirstFileByExtension(dirPath string, extensions ...string) (string, error) {
|
||||
files, err := f.GetFilesByExtension(dirPath, extensions...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -111,7 +117,7 @@ func GetFirstFileByExtension(dirPath string, extensions ...string) (string, erro
|
||||
}
|
||||
|
||||
// 7. 递归获取目录下所有文件
|
||||
func GetAllFiles(dirPath string, recursive bool) ([]FileInfo, error) {
|
||||
func (fileUtilsStruct) GetAllFiles(dirPath string, recursive bool) ([]FileInfo, error) {
|
||||
var files []FileInfo
|
||||
|
||||
if recursive {
|
||||
@@ -161,7 +167,7 @@ func GetAllFiles(dirPath string, recursive bool) ([]FileInfo, error) {
|
||||
}
|
||||
|
||||
// 递归获取目录下所有文件夹
|
||||
func GetAllDirs(dirPath string, recursive bool) ([]FileInfo, error) {
|
||||
func (fileUtilsStruct) GetAllDirs(dirPath string, recursive bool) ([]FileInfo, error) {
|
||||
var dirs []FileInfo
|
||||
|
||||
if recursive {
|
||||
@@ -212,8 +218,8 @@ func GetAllDirs(dirPath string, recursive bool) ([]FileInfo, error) {
|
||||
}
|
||||
|
||||
// 8. 删除文件或目录
|
||||
func Delete(path string) error {
|
||||
if !Exists(path) {
|
||||
func (f fileUtilsStruct) Delete(path string) error {
|
||||
if !f.Exists(path) {
|
||||
return fmt.Errorf("路径不存在: %s", path)
|
||||
}
|
||||
|
||||
@@ -221,12 +227,12 @@ func Delete(path string) error {
|
||||
}
|
||||
|
||||
// 9. 创建目录(递归创建)
|
||||
func CreateDir(dirPath string) error {
|
||||
func (fileUtilsStruct) CreateDir(dirPath string) error {
|
||||
return os.MkdirAll(dirPath, 0755)
|
||||
}
|
||||
|
||||
// 10. 复制文件
|
||||
func CopyFile(src, dst string) error {
|
||||
func (f fileUtilsStruct) CopyFile(src, dst string) error {
|
||||
sourceFile, err := os.Open(src)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开源文件失败: %w", err)
|
||||
@@ -241,7 +247,7 @@ func CopyFile(src, dst string) error {
|
||||
|
||||
// 确保目标目录存在
|
||||
dstDir := filepath.Dir(dst)
|
||||
if err := CreateDir(dstDir); err != nil {
|
||||
if err := f.CreateDir(dstDir); err != nil {
|
||||
return fmt.Errorf("创建目标目录失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -261,7 +267,7 @@ func CopyFile(src, dst string) error {
|
||||
}
|
||||
|
||||
// CopyDir 递归复制整个目录
|
||||
func CopyDir(src, dst string) error {
|
||||
func (f fileUtilsStruct) CopyDir(src, dst string) error {
|
||||
// 获取源目录信息
|
||||
srcInfo, err := os.Stat(src)
|
||||
if err != nil {
|
||||
@@ -291,12 +297,12 @@ func CopyDir(src, dst string) error {
|
||||
|
||||
if entry.IsDir() {
|
||||
// 如果是目录,递归复制
|
||||
if err := CopyDir(srcPath, dstPath); err != nil {
|
||||
if err := f.CopyDir(srcPath, dstPath); err != nil {
|
||||
return fmt.Errorf("复制子目录 %s 失败: %w", entry.Name(), err)
|
||||
}
|
||||
} else {
|
||||
// 如果是文件,使用你的 CopyFile 函数
|
||||
if err := CopyFile(srcPath, dstPath); err != nil {
|
||||
if err := f.CopyFile(srcPath, dstPath); err != nil {
|
||||
return fmt.Errorf("复制文件 %s 失败: %w", entry.Name(), err)
|
||||
}
|
||||
}
|
||||
@@ -311,7 +317,7 @@ type CopyOptions struct {
|
||||
SkipSymlinks bool // 是否跳过符号链接
|
||||
}
|
||||
|
||||
func CopyDirWithOptions(src, dst string, opts *CopyOptions) error {
|
||||
func (f fileUtilsStruct) CopyDirWithOptions(src, dst string, opts *CopyOptions) error {
|
||||
if opts == nil {
|
||||
opts = &CopyOptions{Overwrite: true, SkipSymlinks: false}
|
||||
}
|
||||
@@ -358,7 +364,7 @@ func CopyDirWithOptions(src, dst string, opts *CopyOptions) error {
|
||||
if opts.SkipSymlinks {
|
||||
continue
|
||||
}
|
||||
if err := copySymlink(srcPath, dstPath); err != nil {
|
||||
if err := f.copySymlink(srcPath, dstPath); err != nil {
|
||||
return fmt.Errorf("复制符号链接 %s 失败: %w", entry.Name(), err)
|
||||
}
|
||||
continue
|
||||
@@ -366,7 +372,7 @@ func CopyDirWithOptions(src, dst string, opts *CopyOptions) error {
|
||||
|
||||
if entry.IsDir() {
|
||||
// 递归复制子目录
|
||||
if err := CopyDirWithOptions(srcPath, dstPath, opts); err != nil {
|
||||
if err := f.CopyDirWithOptions(srcPath, dstPath, opts); err != nil {
|
||||
return fmt.Errorf("复制子目录 %s 失败: %w", entry.Name(), err)
|
||||
}
|
||||
} else {
|
||||
@@ -378,7 +384,7 @@ func CopyDirWithOptions(src, dst string, opts *CopyOptions) error {
|
||||
}
|
||||
|
||||
// 复制文件
|
||||
if err := CopyFile(srcPath, dstPath); err != nil {
|
||||
if err := f.CopyFile(srcPath, dstPath); err != nil {
|
||||
return fmt.Errorf("复制文件 %s 失败: %w", entry.Name(), err)
|
||||
}
|
||||
}
|
||||
@@ -388,7 +394,7 @@ func CopyDirWithOptions(src, dst string, opts *CopyOptions) error {
|
||||
}
|
||||
|
||||
// copySymlink 复制符号链接
|
||||
func copySymlink(src, dst string) error {
|
||||
func (fileUtilsStruct) copySymlink(src, dst string) error {
|
||||
link, err := os.Readlink(src)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -401,10 +407,10 @@ func copySymlink(src, dst string) error {
|
||||
}
|
||||
|
||||
// 11. 移动/重命名文件
|
||||
func MoveFile(src, dst string) error {
|
||||
func (f fileUtilsStruct) MoveFile(src, dst string) error {
|
||||
// 确保目标目录存在
|
||||
dstDir := filepath.Dir(dst)
|
||||
if err := CreateDir(dstDir); err != nil {
|
||||
if err := f.CreateDir(dstDir); err != nil {
|
||||
return fmt.Errorf("创建目标目录失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -412,7 +418,7 @@ func MoveFile(src, dst string) error {
|
||||
}
|
||||
|
||||
// 12. 读取文件全部内容
|
||||
func ReadFile(filePath string) (string, error) {
|
||||
func (fileUtilsStruct) ReadFile(filePath string) (string, error) {
|
||||
content, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("读取文件失败: %w", err)
|
||||
@@ -421,7 +427,7 @@ func ReadFile(filePath string) (string, error) {
|
||||
}
|
||||
|
||||
// 13. 按行读取文件
|
||||
func ReadFileLines(filePath string) ([]string, error) {
|
||||
func (fileUtilsStruct) ReadFileLines(filePath string) ([]string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("打开文件失败: %w", err)
|
||||
@@ -442,10 +448,10 @@ func ReadFileLines(filePath string) ([]string, error) {
|
||||
}
|
||||
|
||||
// 14. 写入文件内容
|
||||
func WriteFile(filePath, content string) error {
|
||||
func (f fileUtilsStruct) WriteFile(filePath, content string) error {
|
||||
// 确保目录存在
|
||||
dir := filepath.Dir(filePath)
|
||||
if err := CreateDir(dir); err != nil {
|
||||
if err := f.CreateDir(dir); err != nil {
|
||||
return fmt.Errorf("创建目录失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -453,10 +459,10 @@ func WriteFile(filePath, content string) error {
|
||||
}
|
||||
|
||||
// 15. 追加写入文件
|
||||
func AppendFile(filePath, content string) error {
|
||||
func (f fileUtilsStruct) AppendFile(filePath, content string) error {
|
||||
// 确保目录存在
|
||||
dir := filepath.Dir(filePath)
|
||||
if err := CreateDir(dir); err != nil {
|
||||
if err := f.CreateDir(dir); err != nil {
|
||||
return fmt.Errorf("创建目录失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -471,7 +477,7 @@ func AppendFile(filePath, content string) error {
|
||||
}
|
||||
|
||||
// 16. 获取文件大小
|
||||
func GetFileSize(filePath string) (int64, error) {
|
||||
func (fileUtilsStruct) GetFileSize(filePath string) (int64, error) {
|
||||
info, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("获取文件信息失败: %w", err)
|
||||
@@ -480,7 +486,7 @@ func GetFileSize(filePath string) (int64, error) {
|
||||
}
|
||||
|
||||
// 17. 获取文件修改时间
|
||||
func GetModTime(filePath string) (time.Time, error) {
|
||||
func (fileUtilsStruct) GetModTime(filePath string) (time.Time, error) {
|
||||
info, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
return time.Time{}, fmt.Errorf("获取文件信息失败: %w", err)
|
||||
@@ -489,29 +495,29 @@ func GetModTime(filePath string) (time.Time, error) {
|
||||
}
|
||||
|
||||
// 18. 获取文件扩展名
|
||||
func GetExtension(filePath string) string {
|
||||
func (fileUtilsStruct) GetExtension(filePath string) string {
|
||||
return filepath.Ext(filePath)
|
||||
}
|
||||
|
||||
// 19. 获取文件名(不含扩展名)
|
||||
func GetFileName(filePath string) string {
|
||||
func (fileUtilsStruct) GetFileName(filePath string) string {
|
||||
base := filepath.Base(filePath)
|
||||
ext := filepath.Ext(base)
|
||||
return strings.TrimSuffix(base, ext)
|
||||
}
|
||||
|
||||
// 20. 获取文件基本名称(含扩展名)
|
||||
func GetBaseName(filePath string) string {
|
||||
func (fileUtilsStruct) GetBaseName(filePath string) string {
|
||||
return filepath.Base(filePath)
|
||||
}
|
||||
|
||||
// 21. 获取文件目录
|
||||
func GetDir(filePath string) string {
|
||||
func (fileUtilsStruct) GetDir(filePath string) string {
|
||||
return filepath.Dir(filePath)
|
||||
}
|
||||
|
||||
// 22. 计算文件MD5哈希值
|
||||
func GetFileMD5(filePath string) (string, error) {
|
||||
func (fileUtilsStruct) GetFileMD5(filePath string) (string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("打开文件失败: %w", err)
|
||||
@@ -527,7 +533,7 @@ func GetFileMD5(filePath string) (string, error) {
|
||||
}
|
||||
|
||||
// 23. 计算文件SHA256哈希值
|
||||
func GetFileSHA256(filePath string) (string, error) {
|
||||
func (fileUtilsStruct) GetFileSHA256(filePath string) (string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("打开文件失败: %w", err)
|
||||
@@ -543,7 +549,7 @@ func GetFileSHA256(filePath string) (string, error) {
|
||||
}
|
||||
|
||||
// 24. 清空目录内容(保留目录本身)
|
||||
func CleanDir(dirPath string) error {
|
||||
func (fileUtilsStruct) CleanDir(dirPath string) error {
|
||||
entries, err := os.ReadDir(dirPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取目录失败: %w", err)
|
||||
@@ -560,7 +566,7 @@ func CleanDir(dirPath string) error {
|
||||
}
|
||||
|
||||
// 25. 获取目录大小
|
||||
func GetDirSize(dirPath string) (int64, error) {
|
||||
func (fileUtilsStruct) GetDirSize(dirPath string) (int64, error) {
|
||||
var size int64
|
||||
|
||||
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
|
||||
@@ -577,7 +583,7 @@ func GetDirSize(dirPath string) (int64, error) {
|
||||
}
|
||||
|
||||
// 26. 格式化文件大小
|
||||
func FormatFileSize(bytes int64) string {
|
||||
func (fileUtilsStruct) FormatFileSize(bytes int64) string {
|
||||
const unit = 1024
|
||||
if bytes < unit {
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
@@ -591,7 +597,7 @@ func FormatFileSize(bytes int64) string {
|
||||
}
|
||||
|
||||
// 27. 搜索文件(按文件名模式)
|
||||
func SearchFiles(dirPath, pattern string, recursive bool) ([]string, error) {
|
||||
func (fileUtilsStruct) SearchFiles(dirPath, pattern string, recursive bool) ([]string, error) {
|
||||
var matches []string
|
||||
|
||||
if recursive {
|
||||
@@ -634,11 +640,11 @@ func SearchFiles(dirPath, pattern string, recursive bool) ([]string, error) {
|
||||
}
|
||||
|
||||
// 28. 获取当前工作目录
|
||||
func GetCurrentDir() (string, error) {
|
||||
func (fileUtilsStruct) GetCurrentDir() (string, error) {
|
||||
return os.Getwd()
|
||||
}
|
||||
|
||||
// 29. 改变工作目录
|
||||
func ChangeDir(dirPath string) error {
|
||||
func (fileUtilsStruct) ChangeDir(dirPath string) error {
|
||||
return os.Chdir(dirPath)
|
||||
}
|
||||
@@ -14,6 +14,10 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type hashUtilsStruct struct{}
|
||||
|
||||
var HashUtils = hashUtilsStruct{}
|
||||
|
||||
// HashType 定义支持的Hash算法类型
|
||||
type HashType int
|
||||
|
||||
@@ -40,7 +44,7 @@ type HashResult struct {
|
||||
}
|
||||
|
||||
// getHasher 根据类型返回对应的Hash实例
|
||||
func getHasher(hashType HashType) hash.Hash {
|
||||
func (h hashUtilsStruct) getHasher(hashType HashType) hash.Hash {
|
||||
switch hashType {
|
||||
case MD5:
|
||||
return md5.New()
|
||||
@@ -56,7 +60,7 @@ func getHasher(hashType HashType) hash.Hash {
|
||||
}
|
||||
|
||||
// getHasherName 获取Hash算法名称
|
||||
func getHasherName(hashType HashType) string {
|
||||
func (h hashUtilsStruct) getHasherName(hashType HashType) string {
|
||||
switch hashType {
|
||||
case MD5:
|
||||
return "MD5"
|
||||
@@ -72,21 +76,21 @@ func getHasherName(hashType HashType) string {
|
||||
}
|
||||
|
||||
// HashString 计算字符串的Hash值
|
||||
func HashString(data string, hashType HashType) string {
|
||||
hasher := getHasher(hashType)
|
||||
func (h hashUtilsStruct) HashString(data string, hashType HashType) string {
|
||||
hasher := h.getHasher(hashType)
|
||||
hasher.Write([]byte(data))
|
||||
return fmt.Sprintf("%x", hasher.Sum(nil))
|
||||
}
|
||||
|
||||
// HashBytes 计算字节数组的Hash值
|
||||
func HashBytes(data []byte, hashType HashType) string {
|
||||
hasher := getHasher(hashType)
|
||||
func (h hashUtilsStruct) HashBytes(data []byte, hashType HashType) string {
|
||||
hasher := h.getHasher(hashType)
|
||||
hasher.Write(data)
|
||||
return fmt.Sprintf("%x", hasher.Sum(nil))
|
||||
}
|
||||
|
||||
// HashFile 计算单个文件的Hash值(自动选择优化策略)
|
||||
func HashFile(filename string, hashType HashType) (string, error) {
|
||||
func (h hashUtilsStruct) HashFile(filename string, hashType HashType) (string, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -99,20 +103,20 @@ func HashFile(filename string, hashType HashType) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
hasher := getHasher(hashType)
|
||||
hasher := h.getHasher(hashType)
|
||||
|
||||
// 根据文件大小选择不同的处理策略
|
||||
if fileInfo.Size() <= SmallFileThreshold {
|
||||
// 小文件:直接读取
|
||||
return hashSmallFile(file, hasher)
|
||||
return h.hashSmallFile(file, hasher)
|
||||
} else {
|
||||
// 大文件:使用缓冲区
|
||||
return hashLargeFile(file, hasher, fileInfo.Size())
|
||||
return h.hashLargeFile(file, hasher, fileInfo.Size())
|
||||
}
|
||||
}
|
||||
|
||||
// hashSmallFile 处理小文件
|
||||
func hashSmallFile(file *os.File, hasher hash.Hash) (string, error) {
|
||||
func (h hashUtilsStruct) hashSmallFile(file *os.File, hasher hash.Hash) (string, error) {
|
||||
_, err := io.Copy(hasher, file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -121,7 +125,7 @@ func hashSmallFile(file *os.File, hasher hash.Hash) (string, error) {
|
||||
}
|
||||
|
||||
// hashLargeFile 处理大文件
|
||||
func hashLargeFile(file *os.File, hasher hash.Hash, fileSize int64) (string, error) {
|
||||
func (h hashUtilsStruct) hashLargeFile(file *os.File, hasher hash.Hash, fileSize int64) (string, error) {
|
||||
// 根据文件大小动态调整缓冲区
|
||||
bufferSize := DefaultBufferSize
|
||||
if fileSize > 100*1024*1024 { // 大于100MB使用更大缓冲区
|
||||
@@ -147,7 +151,7 @@ func hashLargeFile(file *os.File, hasher hash.Hash, fileSize int64) (string, err
|
||||
}
|
||||
|
||||
// HashFileWithProgress 计算文件Hash值并提供进度回调
|
||||
func HashFileWithProgress(filename string, hashType HashType, progressCallback func(processed, total int64)) (string, error) {
|
||||
func (h hashUtilsStruct) HashFileWithProgress(filename string, hashType HashType, progressCallback func(processed, total int64)) (string, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -159,7 +163,7 @@ func HashFileWithProgress(filename string, hashType HashType, progressCallback f
|
||||
return "", err
|
||||
}
|
||||
|
||||
hasher := getHasher(hashType)
|
||||
hasher := h.getHasher(hashType)
|
||||
buffer := make([]byte, DefaultBufferSize)
|
||||
var processed int64
|
||||
|
||||
@@ -184,7 +188,7 @@ func HashFileWithProgress(filename string, hashType HashType, progressCallback f
|
||||
}
|
||||
|
||||
// HashMultipleFiles 并发计算多个文件的Hash值
|
||||
func HashMultipleFiles(filenames []string, hashType HashType) []HashResult {
|
||||
func (h hashUtilsStruct) HashMultipleFiles(filenames []string, hashType HashType) []HashResult {
|
||||
// 使用CPU核心数作为并发数
|
||||
numWorkers := runtime.NumCPU()
|
||||
jobs := make(chan string, len(filenames))
|
||||
@@ -197,7 +201,7 @@ func HashMultipleFiles(filenames []string, hashType HashType) []HashResult {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for filename := range jobs {
|
||||
hash, err := HashFile(filename, hashType)
|
||||
hash, err := h.HashFile(filename, hashType)
|
||||
|
||||
var size int64
|
||||
if err == nil {
|
||||
@@ -240,13 +244,13 @@ func HashMultipleFiles(filenames []string, hashType HashType) []HashResult {
|
||||
}
|
||||
|
||||
// CompareFiles 比较两个文件是否相同(通过Hash值)
|
||||
func CompareFiles(file1, file2 string, hashType HashType) (bool, error) {
|
||||
hash1, err := HashFile(file1, hashType)
|
||||
func (h hashUtilsStruct) CompareFiles(file1, file2 string, hashType HashType) (bool, error) {
|
||||
hash1, err := h.HashFile(file1, hashType)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
hash2, err := HashFile(file2, hashType)
|
||||
hash2, err := h.HashFile(file2, hashType)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -255,8 +259,8 @@ func CompareFiles(file1, file2 string, hashType HashType) (bool, error) {
|
||||
}
|
||||
|
||||
// FindDuplicates 在文件列表中查找重复文件
|
||||
func FindDuplicates(filenames []string, hashType HashType) map[string][]string {
|
||||
results := HashMultipleFiles(filenames, hashType)
|
||||
func (h hashUtilsStruct) FindDuplicates(filenames []string, hashType HashType) map[string][]string {
|
||||
results := h.HashMultipleFiles(filenames, hashType)
|
||||
duplicates := make(map[string][]string)
|
||||
|
||||
for _, result := range results {
|
||||
@@ -276,8 +280,8 @@ func FindDuplicates(filenames []string, hashType HashType) map[string][]string {
|
||||
}
|
||||
|
||||
// VerifyFileIntegrity 验证文件完整性
|
||||
func VerifyFileIntegrity(filename, expectedHash string, hashType HashType) (bool, error) {
|
||||
actualHash, err := HashFile(filename, hashType)
|
||||
func (h hashUtilsStruct) VerifyFileIntegrity(filename, expectedHash string, hashType HashType) (bool, error) {
|
||||
actualHash, err := h.HashFile(filename, hashType)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -287,7 +291,7 @@ func VerifyFileIntegrity(filename, expectedHash string, hashType HashType) (bool
|
||||
}
|
||||
|
||||
// HashFileMultipleAlgorithms 使用多种算法计算文件Hash
|
||||
func HashFileMultipleAlgorithms(filename string, hashTypes []HashType) (map[string]string, error) {
|
||||
func (h hashUtilsStruct) HashFileMultipleAlgorithms(filename string, hashTypes []HashType) (map[string]string, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -297,7 +301,7 @@ func HashFileMultipleAlgorithms(filename string, hashTypes []HashType) (map[stri
|
||||
// 创建多个hasher
|
||||
hashers := make(map[HashType]hash.Hash)
|
||||
for _, hashType := range hashTypes {
|
||||
hashers[hashType] = getHasher(hashType)
|
||||
hashers[hashType] = h.getHasher(hashType)
|
||||
}
|
||||
|
||||
// 创建MultiWriter
|
||||
@@ -316,7 +320,7 @@ func HashFileMultipleAlgorithms(filename string, hashTypes []HashType) (map[stri
|
||||
// 收集结果
|
||||
results := make(map[string]string)
|
||||
for hashType, hasher := range hashers {
|
||||
hashName := getHasherName(hashType)
|
||||
hashName := h.getHasherName(hashType)
|
||||
results[hashName] = fmt.Sprintf("%x", hasher.Sum(nil))
|
||||
}
|
||||
|
||||
@@ -324,26 +328,26 @@ func HashFileMultipleAlgorithms(filename string, hashTypes []HashType) (map[stri
|
||||
}
|
||||
|
||||
// 便捷函数
|
||||
func MD5File(filename string) (string, error) {
|
||||
return HashFile(filename, MD5)
|
||||
func (h hashUtilsStruct) MD5File(filename string) (string, error) {
|
||||
return h.HashFile(filename, MD5)
|
||||
}
|
||||
|
||||
func SHA1File(filename string) (string, error) {
|
||||
return HashFile(filename, SHA1)
|
||||
func (h hashUtilsStruct) SHA1File(filename string) (string, error) {
|
||||
return h.HashFile(filename, SHA1)
|
||||
}
|
||||
|
||||
func SHA256File(filename string) (string, error) {
|
||||
return HashFile(filename, SHA256)
|
||||
func (h hashUtilsStruct) SHA256File(filename string) (string, error) {
|
||||
return h.HashFile(filename, SHA256)
|
||||
}
|
||||
|
||||
func SHA512File(filename string) (string, error) {
|
||||
return HashFile(filename, SHA512)
|
||||
func (h hashUtilsStruct) SHA512File(filename string) (string, error) {
|
||||
return h.HashFile(filename, SHA512)
|
||||
}
|
||||
|
||||
func MD5String(data string) string {
|
||||
return HashString(data, MD5)
|
||||
func (h hashUtilsStruct) MD5String(data string) string {
|
||||
return h.HashString(data, MD5)
|
||||
}
|
||||
|
||||
func SHA256String(data string) string {
|
||||
return HashString(data, SHA256)
|
||||
func (h hashUtilsStruct) SHA256String(data string) string {
|
||||
return h.HashString(data, SHA256)
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func TestMain(m *testing.M) {
|
||||
fmt.Println("Testing SHA-256...")
|
||||
for i := 0; i < 5; i++ {
|
||||
start256 := time.Now()
|
||||
sha256Time, err := SHA256File(fileName)
|
||||
sha256Time, err := HashUtils.SHA256File(fileName)
|
||||
elapsed256 := time.Since(start256)
|
||||
|
||||
if err != nil {
|
||||
@@ -66,7 +66,7 @@ func TestMain(m *testing.M) {
|
||||
fmt.Println("Testing SHA-512...")
|
||||
for i := 0; i < 5; i++ {
|
||||
start512 := time.Now()
|
||||
sha512Time, err := SHA512File(fileName)
|
||||
sha512Time, err := HashUtils.SHA512File(fileName)
|
||||
elapsed512 := time.Since(start512)
|
||||
if err != nil {
|
||||
fmt.Println("SHA-512 error:", err)
|
||||
|
||||
Reference in new issue
Block a user