Files
junhong_cmp_fiber/pkg/storage/service.go
break 70e680eb0a
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m2s
feat(手机号资产关联): AUG26-009 手机号—资产关联、十项上限与后台解绑
- 新增成对迁移 000223(tb_phone_asset_association,含有效关系部分唯一索引与 down 守卫)与 000224(解绑导入任务表),不回填历史
- H5:need_bind_phone 三支判定(开关关闭完全短路);已有主号幂等建联;十项上限按手机号 advisory 串行化(含换绑到全新号的并发场景);换绑原子迁移与冲突整单回滚;不写遗留列
- 后台:关联列表、单项/批量解绑、CSV 导入解绑(B1–B16),超管/平台 gate + 资产数据范围复核,三态统一文案
- 读侧:卡/设备列表与详情按页一次 IN 聚合;两类导出补「关联手机号」列并保留历史表头反解兼容
- 脱敏:关联审计走独立动作/资源只写脱敏手机号;访问日志手机号类字段脱敏
- 同步主 Spec openspec/specs/phone-asset-association 并归档 AUG26-009,补齐 requirement-evidence 与入口矩阵,context-health 通过
2026-09-15 11:54:56 +08:00

118 lines
2.7 KiB
Go

package storage
import (
"context"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/google/uuid"
"github.com/break/junhong_cmp_fiber/pkg/config"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors"
)
type Service struct {
provider Provider
config *config.StorageConfig
}
func NewService(provider Provider, cfg *config.StorageConfig) *Service {
return &Service{
provider: provider,
config: cfg,
}
}
func (s *Service) GenerateFileKey(purpose, fileName string) (string, error) {
mapping, ok := PurposeMappings[purpose]
if !ok {
return "", errors.New(errors.CodeInvalidParam, "不支持的文件用途: %s", purpose)
}
ext := filepath.Ext(fileName)
if ext == "" {
ext = ".bin"
}
if (purpose == constants.StoragePurposeAssetPackageBatchOrder || purpose == constants.StoragePurposeDeviceBatchAllocation || purpose == constants.StoragePurposeShopBusinessOwnerImport || purpose == constants.StoragePurposePhoneAssetUnbindImport) && !strings.EqualFold(ext, ".csv") {
return "", errors.New(errors.CodeInvalidParam, "批量业务文件必须为CSV格式")
}
now := time.Now()
id := uuid.New().String()
key := fmt.Sprintf("%s/%04d/%02d/%02d/%s%s",
mapping.Prefix,
now.Year(),
now.Month(),
now.Day(),
id,
strings.ToLower(ext),
)
return key, nil
}
func (s *Service) GetUploadURL(ctx context.Context, purpose, fileName, contentType string) (*PresignResult, error) {
fileKey, err := s.GenerateFileKey(purpose, fileName)
if err != nil {
return nil, err
}
if contentType == "" {
if mapping, ok := PurposeMappings[purpose]; ok && mapping.ContentType != "" {
contentType = mapping.ContentType
} else {
contentType = "application/octet-stream"
}
}
expires := s.config.Presign.UploadExpires
if expires == 0 {
expires = 15 * time.Minute
}
url, err := s.provider.GetUploadURL(ctx, fileKey, contentType, expires)
if err != nil {
return nil, err
}
return &PresignResult{
URL: url,
FileKey: fileKey,
ExpiresIn: int(expires.Seconds()),
}, nil
}
func (s *Service) GetDownloadURL(ctx context.Context, fileKey string) (*PresignResult, error) {
expires := s.config.Presign.DownloadExpires
if expires == 0 {
expires = 24 * time.Hour
}
url, err := s.provider.GetDownloadURL(ctx, fileKey, expires)
if err != nil {
return nil, err
}
return &PresignResult{
URL: url,
FileKey: fileKey,
ExpiresIn: int(expires.Seconds()),
}, nil
}
func (s *Service) DownloadToTemp(ctx context.Context, fileKey string) (string, func(), error) {
return s.provider.DownloadToTemp(ctx, fileKey)
}
func (s *Service) Provider() Provider {
return s.provider
}
func (s *Service) Bucket() string {
return s.config.S3.Bucket
}