chore(be): 上传对象按月份分文件夹 uploads/YYYY-MM/

MD5 去重本来就有(相同内容直接返回已有记录不重传);对象名从
files/<md5> 改成 uploads/2026-07/<md5>,跨月的同内容仍走去重不重复占用。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-31 14:16:53 +08:00
parent a91f5e39ca
commit 0f3efbc895
+4 -1
View File
@@ -8,6 +8,7 @@ import (
"io"
"path"
"strings"
"time"
"github.com/sundynix/pets-be/internal/model"
)
@@ -41,7 +42,9 @@ func (s *Service) UploadFile(reader io.Reader, filename, contentType string) (*m
if contentType == "" {
contentType = "application/octet-stream"
}
objectName := "files/" + md5hex + ext
// 按上传月份分文件夹:uploads/2026-07/<md5>.jpg。文件名仍用 md5 保证唯一。
// 同内容命中上面的去重直接复用,不会因为跨月再传一份
objectName := "uploads/" + time.Now().Format("2006-01") + "/" + md5hex + ext
url, err := s.storage.Upload(objectName, bytes.NewReader(data), int64(len(data)), contentType)
if err != nil {
return nil, err