refactor(backend): remove redundant MD5 hash calculation from UploadFileSlice and streamline file upload logic

This commit is contained in:
keven1024
2025-04-28 20:46:53 +08:00
parent cd81cead2a
commit 94e634619f
2 changed files with 1 additions and 18 deletions

View File

@@ -4,11 +4,8 @@ import (
"backend/internal/models"
"backend/internal/services"
"backend/internal/utils"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"time"
@@ -91,19 +88,6 @@ func UploadFileSlice(c echo.Context) error {
}
defer file.Close()
// 读取文件内容
fileBytes, err := io.ReadAll(file)
if err != nil {
return utils.HTTPErrorHandler(c, err)
}
// 计算 MD5 哈希
hash := md5.Sum(fileBytes)
// 如果需要十六进制字符串形式
hashString := fmt.Sprintf("%x", hash)
fmt.Printf("hash: %s\n", hashString)
if err := services.CreateFileSlice(file, r.FileId, r.FileIndex); err != nil {
return utils.HTTPErrorHandler(c, err)
}

View File

@@ -5,14 +5,13 @@ import (
"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) {
func GetFileMd5(file io.Reader) (string, error) {
const bufferSize = 1024 * 1000 // 1MB