refactor: 移除不必要的配置依赖,简化上传URL生成逻辑并添加公开访问URL支持

This commit is contained in:
lan
2025-12-02 11:22:14 +08:00
parent 13bab28926
commit 23be1c563d
12 changed files with 478 additions and 173 deletions

View File

@@ -4,7 +4,6 @@ import (
"carrotskin/internal/model"
"carrotskin/internal/service"
"carrotskin/internal/types"
"carrotskin/pkg/config"
"carrotskin/pkg/database"
"carrotskin/pkg/logger"
"carrotskin/pkg/storage"
@@ -38,11 +37,9 @@ func GenerateTextureUploadURL(c *gin.Context) {
}
storageClient := storage.MustGetClient()
cfg := *config.MustGetRustFSConfig()
result, err := service.GenerateTextureUploadURL(
c.Request.Context(),
storageClient,
cfg,
userID,
req.FileName,
string(req.TextureType),

View File

@@ -3,7 +3,6 @@ package handler
import (
"carrotskin/internal/service"
"carrotskin/internal/types"
"carrotskin/pkg/config"
"carrotskin/pkg/database"
"carrotskin/pkg/logger"
"carrotskin/pkg/redis"
@@ -140,8 +139,7 @@ func GenerateAvatarUploadURL(c *gin.Context) {
}
storageClient := storage.MustGetClient()
cfg := *config.MustGetRustFSConfig()
result, err := service.GenerateAvatarUploadURL(c.Request.Context(), storageClient, cfg, userID, req.FileName)
result, err := service.GenerateAvatarUploadURL(c.Request.Context(), storageClient, userID, req.FileName)
if err != nil {
logger.MustGetLogger().Error("生成头像上传URL失败",
zap.Int64("user_id", userID),

View File

@@ -1,7 +1,6 @@
package service
import (
"carrotskin/pkg/config"
"carrotskin/pkg/storage"
"context"
"fmt"
@@ -76,7 +75,7 @@ func ValidateFileName(fileName string, fileType FileType) error {
}
// GenerateAvatarUploadURL 生成头像上传URL
func GenerateAvatarUploadURL(ctx context.Context, storageClient *storage.StorageClient, cfg config.RustFSConfig, userID int64, fileName string) (*storage.PresignedPostPolicyResult, error) {
func GenerateAvatarUploadURL(ctx context.Context, storageClient *storage.StorageClient, userID int64, fileName string) (*storage.PresignedPostPolicyResult, error) {
// 1. 验证文件名
if err := ValidateFileName(fileName, FileTypeAvatar); err != nil {
return nil, err
@@ -96,7 +95,7 @@ func GenerateAvatarUploadURL(ctx context.Context, storageClient *storage.Storage
timestamp := time.Now().Format("20060102150405")
objectName := fmt.Sprintf("user_%d/%s_%s", userID, timestamp, fileName)
// 5. 生成预签名POST URL
// 5. 生成预签名POST URL (使用存储客户端内置的 PublicURL)
result, err := storageClient.GeneratePresignedPostURL(
ctx,
bucketName,
@@ -104,8 +103,6 @@ func GenerateAvatarUploadURL(ctx context.Context, storageClient *storage.Storage
uploadConfig.MinSize,
uploadConfig.MaxSize,
uploadConfig.Expires,
cfg.UseSSL,
cfg.Endpoint,
)
if err != nil {
return nil, fmt.Errorf("生成上传URL失败: %w", err)
@@ -115,7 +112,7 @@ func GenerateAvatarUploadURL(ctx context.Context, storageClient *storage.Storage
}
// GenerateTextureUploadURL 生成材质上传URL
func GenerateTextureUploadURL(ctx context.Context, storageClient *storage.StorageClient, cfg config.RustFSConfig, userID int64, fileName, textureType string) (*storage.PresignedPostPolicyResult, error) {
func GenerateTextureUploadURL(ctx context.Context, storageClient *storage.StorageClient, userID int64, fileName, textureType string) (*storage.PresignedPostPolicyResult, error) {
// 1. 验证文件名
if err := ValidateFileName(fileName, FileTypeTexture); err != nil {
return nil, err
@@ -141,7 +138,7 @@ func GenerateTextureUploadURL(ctx context.Context, storageClient *storage.Storag
textureTypeFolder := strings.ToLower(textureType)
objectName := fmt.Sprintf("user_%d/%s/%s_%s", userID, textureTypeFolder, timestamp, fileName)
// 6. 生成预签名POST URL
// 6. 生成预签名POST URL (使用存储客户端内置的 PublicURL)
result, err := storageClient.GeneratePresignedPostURL(
ctx,
bucketName,
@@ -149,8 +146,6 @@ func GenerateTextureUploadURL(ctx context.Context, storageClient *storage.Storag
uploadConfig.MinSize,
uploadConfig.MaxSize,
uploadConfig.Expires,
cfg.UseSSL,
cfg.Endpoint,
)
if err != nil {
return nil, fmt.Errorf("生成上传URL失败: %w", err)