Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复发送图片语音时随机生成文件名 #168

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions server/uploadpic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package server
import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
Expand Down Expand Up @@ -71,7 +70,7 @@ func UploadBase64ImageHandler(rateLimiter *RateLimiter) gin.HandlerFunc {
return
}

fileName := generateRandomMd5() + "." + fileExt
fileName := getFileMd5(imageBytes) + "." + fileExt
directoryPath := "./channel_temp/"
savePath := directoryPath + fileName

Expand Down Expand Up @@ -137,7 +136,7 @@ func UploadBase64RecordHandler(rateLimiter *RateLimiter) gin.HandlerFunc {
return
}

fileName := generateRandomMd5() + ".silk"
fileName := getFileMd5(RecordBytes) + ".silk"
directoryPath := "./channel_temp/"
savePath := directoryPath + fileName

Expand Down Expand Up @@ -226,13 +225,8 @@ func getFileExtensionFromImageFormat(format string) string {
}

// 生成随机md5图片名,防止碰撞
func generateRandomMd5() string {
randomBytes := make([]byte, 16)
_, err := rand.Read(randomBytes)
if err != nil {
return ""
}
md5Hash := md5.Sum(randomBytes)
func getFileMd5(base64file []byte) string {
md5Hash := md5.Sum(base64file)
return hex.EncodeToString(md5Hash[:])
}

Expand Down
Loading