i18n翻译

This commit is contained in:
hu xiaotong
2025-07-14 17:31:48 +08:00
parent b8c9817cb3
commit 8657bc4eea
4 changed files with 150 additions and 5 deletions

View File

@@ -17,4 +17,11 @@ const (
// 官方数据接口示例https://static.smilegatemegaport.com/event/live/epic7/guide/images/hero/c2027_s.png
// 角色图片基础 URL
GfHeroPngURL = "https://static.smilegatemegaport.com/event/live/epic7/guide/images/hero/"
// S3/OSS 配置
S3AccessKey = "s5iWm6wXVvhCNN9nJlXwgWRf"
S3SecretKey = "91sTurpFtugXijPg0uSof3JcJma0HED"
S3Bucket = "epic"
S3Region = "cn-east-1"
S3Endpoint = "https://s3.bitiful.net"
)

View File

@@ -2,6 +2,18 @@ package util
import (
"context"
"epic/internal/consts"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
// DownloadAndUploadToOSS 下载网络图片并上传到OSS返回OSS路径
@@ -9,9 +21,78 @@ import (
// 返回: OSS上的图片URL或错误
func DownloadAndUploadToOSS(ctx context.Context, imageUrl string) (string, error) {
// 1. 下载 imageUrl 到本地临时文件
resp, err := http.Get(imageUrl)
if err != nil {
return "", fmt.Errorf("failed to download image: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to download image: status %d", resp.StatusCode)
}
tmpFile, err := os.CreateTemp("", "ossimg-*.tmp")
if err != nil {
return "", fmt.Errorf("failed to create temp file: %w", err)
}
tmpFilePath := tmpFile.Name()
defer func() {
tmpFile.Close()
os.Remove(tmpFilePath)
}()
_, err = io.Copy(tmpFile, resp.Body)
if err != nil {
return "", fmt.Errorf("failed to save image to temp file: %w", err)
}
// 2. 上传临时文件到OSS获取OSS路径
// 3. 删除临时文件
// 4. 返回OSS路径
// TODO: 实现具体逻辑
return "", nil
}
accessKey := consts.S3AccessKey
secretKey := consts.S3SecretKey
bucket := consts.S3Bucket
region := consts.S3Region
endpoint := consts.S3Endpoint
if accessKey == "" || secretKey == "" || bucket == "" || endpoint == "" {
return "", fmt.Errorf("missing S3 credentials or bucket info in consts")
}
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == s3.ServiceID {
return aws.Endpoint{URL: endpoint}, nil
}
return aws.Endpoint{}, fmt.Errorf("unknown service requested")
})
customProvider := credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")
cfg, err := config.LoadDefaultConfig(ctx,
config.WithCredentialsProvider(customProvider),
config.WithEndpointResolverWithOptions(customResolver),
)
if err != nil {
return "", fmt.Errorf("failed to load S3 config: %w", err)
}
cfg.Region = region
s3Client := s3.NewFromConfig(cfg)
// 生成唯一的 object key
fileName := filepath.Base(tmpFilePath)
objectKey := fmt.Sprintf("images/%d_%s", time.Now().UnixNano(), fileName)
tmpFile.Seek(0, io.SeekStart)
_, err = tmpFile.Stat()
if err != nil {
return "", fmt.Errorf("failed to stat temp file: %w", err)
}
// 上传
_, err = s3Client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(objectKey),
Body: tmpFile,
})
if err != nil {
return "", fmt.Errorf("failed to upload to S3: %w", err)
}
// 4. 返回OSS路径拼接URL
ossUrl := fmt.Sprintf("%s/%s/%s", endpoint, bucket, objectKey)
return ossUrl, nil
}