mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
refactor(backend): remove redundant MD5 hash calculation from UploadFileSlice and streamline file upload logic
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user