feat(backend): add GetFileMd5 function for calculating MD5 hash of files to enhance file integrity verification

This commit is contained in:
keven1024
2025-04-28 20:40:59 +08:00
parent 54a0179ef1
commit cd81cead2a

View File

@@ -1,7 +1,33 @@
package utils
import "fmt"
import (
"bufio"
"crypto/md5"
"fmt"
"io"
"os"
)
func GetFileId(fileHash string, fileSize int64) string {
return fmt.Sprintf("%s_%d", fileHash, fileSize)
}
func GetFileMd5(file *os.File) (string, error) {
const bufferSize = 1024 * 1000 // 1MB
hash := md5.New()
for buf, reader := make([]byte, bufferSize), bufio.NewReader(file); ; {
n, err := reader.Read(buf)
if err != nil {
if err == io.EOF {
break
}
return "", err
}
hash.Write(buf[:n])
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}