20260629154839

This commit is contained in:
oneao committed 2026-06-29 15:48:39 +08:00
1 parent e687846eb4
commit bb596f3245
41 files changed
+5735 -86

No files matched your search

+2 -2
View File
@@ -257,9 +257,9 @@ func registerUser(
ext = ".jpg" // 兜底
}
// 年月日 + uuid
// 注册头像没有家庭上下文,单独放到 avatar 前缀下。
now := time.Now()
key := fmt.Sprintf("%d/%02d/%02d/%s%s",
key := fmt.Sprintf("avatar/%d/%02d/%02d/%s%s",
now.Year(),
now.Month(),
now.Day(),
+75 -5
View File
@@ -8,6 +8,7 @@ import (
"allapp-go/pkg/s3store"
"fmt"
"path/filepath"
"strconv"
"time"
"github.com/gofiber/fiber/v3"
@@ -28,11 +29,10 @@ func S3UploadFile(c fiber.Ctx) error {
}
now := time.Now()
folderPath := fmt.Sprintf("%d/%02d/%02d",
now.Year(),
now.Month(),
now.Day(),
)
folderPath, err := uploadFolderPath(c.FormValue("upload_type"), c.FormValue("family_id"), now)
if err != nil {
return httpx.Fail(c, err.Error())
}
var result []string
@@ -68,6 +68,37 @@ func S3UploadFile(c fiber.Ctx) error {
return httpx.OK(c, result)
}
func S3CopyFile(c fiber.Ctx) error {
var req types.S3CopyReq
if err := httpx.BindAndValidate(c, &req); err != nil {
return errors.WithStack(err)
}
if len(req.Keys) == 0 {
return httpx.OK(c, []string{})
}
folderPath := familyDateFolder(req.FamilyID, time.Now())
result := make([]string, 0, len(req.Keys))
for _, sourceKey := range req.Keys {
ext := filepath.Ext(sourceKey)
if ext == "" {
ext = ".bin"
}
targetKey := fmt.Sprintf("%s/%s%s", folderPath, uuid.NewString(), ext)
if err := s3store.CopyObject(c.Context(), sourceKey, targetKey); err != nil {
return errors.WithStack(err)
}
result = append(result, targetKey)
}
return httpx.OK(c, result)
}
func S3DeleteFile(c fiber.Ctx) error {
var req types.S3DeleteReq
@@ -87,3 +118,42 @@ func S3DeleteFile(c fiber.Ctx) error {
return httpx.OK(c, nil)
}
func parseFamilyID(value string) (int64, error) {
familyID, err := strconv.ParseInt(value, 10, 64)
if err != nil || familyID <= 0 {
return 0, fmt.Errorf("invalid family_id")
}
return familyID, nil
}
func familyDateFolder(familyID int64, now time.Time) string {
return fmt.Sprintf("%d/%d/%02d/%02d",
familyID,
now.Year(),
now.Month(),
now.Day(),
)
}
func avatarDateFolder(now time.Time) string {
return fmt.Sprintf("avatar/%d/%02d/%02d",
now.Year(),
now.Month(),
now.Day(),
)
}
func uploadFolderPath(uploadType string, familyIDValue string, now time.Time) (string, error) {
if uploadType == "avatar" {
return avatarDateFolder(now), nil
}
familyID, err := parseFamilyID(familyIDValue)
if err != nil {
return "", fmt.Errorf("family_id不能为空")
}
return familyDateFolder(familyID, now), nil
}