mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 15:06:36 +00:00
revert: remove Google Drive storage backend
Remove all GDrive/Google Drive storage extension code: - Delete 7 GDrive-specific files (handler, service, tests, router, frontend) - Remove GDrive fields from setting service and DTOs - Remove GDrive routes, wire dependencies, i18n entries - Revert DataManagementView to S3-only storage management
This commit is contained in:
1 parent
fc5e96aa9f
commit
950ac48f9f
20 files changed
+101
-1842
No files matched your search
@@ -227,49 +227,6 @@ func (s *SettingService) SetOnS3UpdateCallback(callback func()) {
|
||||
s.onS3Update = callback
|
||||
}
|
||||
|
||||
// SetOnStorageUpdateCallback 设置存储配置变更时的回调函数(用于刷新所有存储客户端缓存)。
|
||||
// 替代 SetOnS3UpdateCallback,支持 S3 + GDrive 统一刷新。
|
||||
func (s *SettingService) SetOnStorageUpdateCallback(callback func()) {
|
||||
s.onS3Update = callback
|
||||
}
|
||||
|
||||
// --- 统一存储 Profile 方法别名 ---
|
||||
|
||||
// ListSoraStorageProfiles 获取 Sora 存储多配置列表(统一方法名)。
|
||||
func (s *SettingService) ListSoraStorageProfiles(ctx context.Context) (*SoraS3ProfileList, error) {
|
||||
return s.ListSoraS3Profiles(ctx)
|
||||
}
|
||||
|
||||
// CreateSoraStorageProfile 创建 Sora 存储配置(统一方法名)。
|
||||
func (s *SettingService) CreateSoraStorageProfile(ctx context.Context, profile *SoraS3Profile, setActive bool) (*SoraS3Profile, error) {
|
||||
return s.CreateSoraS3Profile(ctx, profile, setActive)
|
||||
}
|
||||
|
||||
// UpdateSoraStorageProfile 更新 Sora 存储配置(统一方法名)。
|
||||
func (s *SettingService) UpdateSoraStorageProfile(ctx context.Context, profileID string, profile *SoraS3Profile) (*SoraS3Profile, error) {
|
||||
return s.UpdateSoraS3Profile(ctx, profileID, profile)
|
||||
}
|
||||
|
||||
// DeleteSoraStorageProfile 删除 Sora 存储配置(统一方法名)。
|
||||
func (s *SettingService) DeleteSoraStorageProfile(ctx context.Context, profileID string) error {
|
||||
return s.DeleteSoraS3Profile(ctx, profileID)
|
||||
}
|
||||
|
||||
// SetActiveSoraStorageProfile 设置激活的 Sora 存储配置(统一方法名)。
|
||||
func (s *SettingService) SetActiveSoraStorageProfile(ctx context.Context, profileID string) (*SoraS3Profile, error) {
|
||||
return s.SetActiveSoraS3Profile(ctx, profileID)
|
||||
}
|
||||
|
||||
// GetActiveStorageProfile 获取当前激活的存储配置 profile。
|
||||
func (s *SettingService) GetActiveStorageProfile(ctx context.Context) (*SoraS3Profile, error) {
|
||||
profiles, err := s.ListSoraS3Profiles(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
active := pickActiveSoraS3Profile(profiles.Items, profiles.ActiveProfileID)
|
||||
return active, nil
|
||||
}
|
||||
|
||||
// SetVersion sets the application version for injection into public settings
|
||||
func (s *SettingService) SetVersion(version string) {
|
||||
s.version = version
|
||||
@@ -1635,8 +1592,6 @@ type soraS3ProfilesStore struct {
|
||||
type soraS3ProfileStoreItem struct {
|
||||
ProfileID string `json:"profile_id"`
|
||||
Name string `json:"name"`
|
||||
Provider string `json:"provider,omitempty"` // "s3" / "gdrive",空值视为 "s3"
|
||||
AccessMode string `json:"access_mode,omitempty"` // "direct" / "proxy",空值视为 "direct"
|
||||
Enabled bool `json:"enabled"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Region string `json:"region"`
|
||||
@@ -1648,14 +1603,6 @@ type soraS3ProfileStoreItem struct {
|
||||
CDNURL string `json:"cdn_url"`
|
||||
DefaultStorageQuotaBytes int64 `json:"default_storage_quota_bytes"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
|
||||
// --- Google Drive 专属 ---
|
||||
AuthType string `json:"auth_type,omitempty"`
|
||||
ClientID string `json:"client_id,omitempty"`
|
||||
ClientSecret string `json:"client_secret,omitempty"`
|
||||
RefreshToken string `json:"refresh_token,omitempty"`
|
||||
ServiceAccountJSON string `json:"service_account_json,omitempty"`
|
||||
FolderID string `json:"folder_id,omitempty"`
|
||||
}
|
||||
|
||||
// GetSoraS3Settings 获取 Sora S3 存储配置(兼容旧单配置语义:返回当前激活配置)
|
||||
@@ -1767,8 +1714,6 @@ func (s *SettingService) CreateSoraS3Profile(ctx context.Context, profile *SoraS
|
||||
store.Items = append(store.Items, soraS3ProfileStoreItem{
|
||||
ProfileID: profileID,
|
||||
Name: name,
|
||||
Provider: profile.Provider,
|
||||
AccessMode: profile.AccessMode,
|
||||
Enabled: profile.Enabled,
|
||||
Endpoint: strings.TrimSpace(profile.Endpoint),
|
||||
Region: strings.TrimSpace(profile.Region),
|
||||
@@ -1780,13 +1725,6 @@ func (s *SettingService) CreateSoraS3Profile(ctx context.Context, profile *SoraS
|
||||
CDNURL: strings.TrimSpace(profile.CDNURL),
|
||||
DefaultStorageQuotaBytes: maxInt64(profile.DefaultStorageQuotaBytes, 0),
|
||||
UpdatedAt: now,
|
||||
// Google Drive 专属
|
||||
AuthType: profile.AuthType,
|
||||
ClientID: strings.TrimSpace(profile.ClientID),
|
||||
ClientSecret: profile.ClientSecret,
|
||||
RefreshToken: profile.RefreshToken,
|
||||
ServiceAccountJSON: profile.ServiceAccountJSON,
|
||||
FolderID: strings.TrimSpace(profile.FolderID),
|
||||
})
|
||||
|
||||
if setActive || store.ActiveProfileID == "" {
|
||||
@@ -1832,8 +1770,6 @@ func (s *SettingService) UpdateSoraS3Profile(ctx context.Context, profileID stri
|
||||
return nil, infraerrors.BadRequest("SORA_S3_PROFILE_NAME_REQUIRED", "name is required")
|
||||
}
|
||||
target.Name = name
|
||||
target.Provider = profile.Provider
|
||||
target.AccessMode = profile.AccessMode
|
||||
target.Enabled = profile.Enabled
|
||||
target.Endpoint = strings.TrimSpace(profile.Endpoint)
|
||||
target.Region = strings.TrimSpace(profile.Region)
|
||||
@@ -1846,19 +1782,6 @@ func (s *SettingService) UpdateSoraS3Profile(ctx context.Context, profileID stri
|
||||
if profile.SecretAccessKey != "" {
|
||||
target.SecretAccessKey = profile.SecretAccessKey
|
||||
}
|
||||
// Google Drive 专属
|
||||
target.AuthType = profile.AuthType
|
||||
target.ClientID = strings.TrimSpace(profile.ClientID)
|
||||
if profile.ClientSecret != "" {
|
||||
target.ClientSecret = profile.ClientSecret
|
||||
}
|
||||
if profile.RefreshToken != "" {
|
||||
target.RefreshToken = profile.RefreshToken
|
||||
}
|
||||
if profile.ServiceAccountJSON != "" {
|
||||
target.ServiceAccountJSON = profile.ServiceAccountJSON
|
||||
}
|
||||
target.FolderID = strings.TrimSpace(profile.FolderID)
|
||||
target.UpdatedAt = time.Now().UTC().Format(time.RFC3339)
|
||||
store.Items[targetIndex] = target
|
||||
|
||||
@@ -2161,8 +2084,6 @@ func convertSoraS3ProfilesStore(store *soraS3ProfilesStore) *SoraS3ProfileList {
|
||||
ProfileID: item.ProfileID,
|
||||
Name: item.Name,
|
||||
IsActive: item.ProfileID == store.ActiveProfileID,
|
||||
Provider: item.Provider,
|
||||
AccessMode: item.AccessMode,
|
||||
Enabled: item.Enabled,
|
||||
Endpoint: item.Endpoint,
|
||||
Region: item.Region,
|
||||
@@ -2175,16 +2096,6 @@ func convertSoraS3ProfilesStore(store *soraS3ProfilesStore) *SoraS3ProfileList {
|
||||
CDNURL: item.CDNURL,
|
||||
DefaultStorageQuotaBytes: item.DefaultStorageQuotaBytes,
|
||||
UpdatedAt: item.UpdatedAt,
|
||||
// Google Drive 专属
|
||||
AuthType: item.AuthType,
|
||||
ClientID: item.ClientID,
|
||||
ClientSecret: item.ClientSecret,
|
||||
ClientSecretConfigured: item.ClientSecret != "",
|
||||
RefreshToken: item.RefreshToken,
|
||||
RefreshTokenConfigured: item.RefreshToken != "",
|
||||
ServiceAccountJSON: item.ServiceAccountJSON,
|
||||
ServiceAccountConfigured: item.ServiceAccountJSON != "",
|
||||
FolderID: item.FolderID,
|
||||
})
|
||||
}
|
||||
return &SoraS3ProfileList{
|
||||
|
||||
@@ -136,8 +136,6 @@ type SoraS3Profile struct {
|
||||
ProfileID string `json:"profile_id"`
|
||||
Name string `json:"name"`
|
||||
IsActive bool `json:"is_active"`
|
||||
Provider string `json:"provider"` // "s3" / "gdrive",空值视为 "s3"
|
||||
AccessMode string `json:"access_mode"` // "direct" / "proxy",空值视为 "direct"
|
||||
Enabled bool `json:"enabled"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Region string `json:"region"`
|
||||
@@ -150,25 +148,6 @@ type SoraS3Profile struct {
|
||||
CDNURL string `json:"cdn_url"`
|
||||
DefaultStorageQuotaBytes int64 `json:"default_storage_quota_bytes"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
|
||||
// --- Google Drive 专属 ---
|
||||
AuthType string `json:"auth_type,omitempty"` // "oauth2" / "service_account"
|
||||
ClientID string `json:"client_id,omitempty"`
|
||||
ClientSecret string `json:"-"`
|
||||
ClientSecretConfigured bool `json:"client_secret_configured"`
|
||||
RefreshToken string `json:"-"`
|
||||
RefreshTokenConfigured bool `json:"refresh_token_configured"`
|
||||
ServiceAccountJSON string `json:"-"`
|
||||
ServiceAccountConfigured bool `json:"service_account_configured"`
|
||||
FolderID string `json:"folder_id,omitempty"`
|
||||
}
|
||||
|
||||
// GetProvider 返回 Provider,空值视为 "s3"。
|
||||
func (p *SoraS3Profile) GetProvider() string {
|
||||
if p.Provider == "" {
|
||||
return SoraStorageTypeS3
|
||||
}
|
||||
return p.Provider
|
||||
}
|
||||
|
||||
// SoraS3ProfileList Sora S3 多配置列表
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/google"
|
||||
"google.golang.org/api/drive/v3"
|
||||
)
|
||||
|
||||
// SoraGDriveOAuthService 处理 Google Drive OAuth2 授权流程。
|
||||
type SoraGDriveOAuthService struct{}
|
||||
|
||||
// NewSoraGDriveOAuthService 创建 GDrive OAuth 服务。
|
||||
func NewSoraGDriveOAuthService(_ *SettingService) *SoraGDriveOAuthService {
|
||||
return &SoraGDriveOAuthService{}
|
||||
}
|
||||
|
||||
// GenerateAuthURL 生成 Google OAuth 授权 URL。
|
||||
func (s *SoraGDriveOAuthService) GenerateAuthURL(clientID, clientSecret, redirectURI string) (authURL, state string, err error) {
|
||||
if clientID == "" || clientSecret == "" || redirectURI == "" {
|
||||
return "", "", fmt.Errorf("client_id, client_secret, redirect_uri are required")
|
||||
}
|
||||
|
||||
config := &oauth2.Config{
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
Endpoint: google.Endpoint,
|
||||
Scopes: []string{drive.DriveFileScope},
|
||||
RedirectURL: redirectURI,
|
||||
}
|
||||
|
||||
// 生成随机 state
|
||||
stateBytes := make([]byte, 16)
|
||||
if _, err := rand.Read(stateBytes); err != nil {
|
||||
return "", "", fmt.Errorf("generate state: %w", err)
|
||||
}
|
||||
state = hex.EncodeToString(stateBytes)
|
||||
|
||||
authURL = config.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.ApprovalForce)
|
||||
return authURL, state, nil
|
||||
}
|
||||
|
||||
// ExchangeCode 用授权码换取 refresh_token。
|
||||
func (s *SoraGDriveOAuthService) ExchangeCode(ctx context.Context, clientID, clientSecret, redirectURI, code string) (string, error) {
|
||||
if code == "" {
|
||||
return "", fmt.Errorf("authorization code is required")
|
||||
}
|
||||
|
||||
config := &oauth2.Config{
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
Endpoint: google.Endpoint,
|
||||
Scopes: []string{drive.DriveFileScope},
|
||||
RedirectURL: redirectURI,
|
||||
}
|
||||
|
||||
token, err := config.Exchange(ctx, code)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("exchange code: %w", err)
|
||||
}
|
||||
|
||||
if token.RefreshToken == "" {
|
||||
return "", fmt.Errorf("no refresh_token received, please revoke app access and try again")
|
||||
}
|
||||
|
||||
return token.RefreshToken, nil
|
||||
}
|
||||
@@ -1,433 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/google"
|
||||
"google.golang.org/api/drive/v3"
|
||||
"google.golang.org/api/option"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/logger"
|
||||
)
|
||||
|
||||
// SoraGDriveStorage 负责 Sora 媒体文件的 Google Drive 存储操作。
|
||||
type SoraGDriveStorage struct {
|
||||
settingService *SettingService
|
||||
|
||||
mu sync.RWMutex
|
||||
srv *drive.Service
|
||||
cfg *SoraS3Profile // 缓存当前 GDrive 配置
|
||||
healthCheckedAt time.Time
|
||||
healthErr error
|
||||
healthTTL time.Duration
|
||||
}
|
||||
|
||||
const defaultGDriveHealthTTL = 30 * time.Second
|
||||
|
||||
// NewSoraGDriveStorage 创建 Google Drive 存储服务实例。
|
||||
func NewSoraGDriveStorage(settingService *SettingService) *SoraGDriveStorage {
|
||||
return &SoraGDriveStorage{
|
||||
settingService: settingService,
|
||||
healthTTL: defaultGDriveHealthTTL,
|
||||
}
|
||||
}
|
||||
|
||||
// StorageType 返回存储类型标识。
|
||||
func (s *SoraGDriveStorage) StorageType() string {
|
||||
return SoraStorageTypeGDrive
|
||||
}
|
||||
|
||||
// Enabled 返回 Google Drive 存储是否已启用。
|
||||
func (s *SoraGDriveStorage) Enabled(ctx context.Context) bool {
|
||||
profile := s.getActiveGDriveProfile(ctx)
|
||||
if profile == nil {
|
||||
return false
|
||||
}
|
||||
return profile.Enabled && s.hasValidCredentials(profile)
|
||||
}
|
||||
|
||||
// getActiveGDriveProfile 获取当前激活的 GDrive 配置。
|
||||
func (s *SoraGDriveStorage) getActiveGDriveProfile(ctx context.Context) *SoraS3Profile {
|
||||
if s.settingService == nil {
|
||||
return nil
|
||||
}
|
||||
profile, err := s.settingService.GetActiveStorageProfile(ctx)
|
||||
if err != nil || profile == nil {
|
||||
return nil
|
||||
}
|
||||
if profile.GetProvider() != SoraStorageTypeGDrive {
|
||||
return nil
|
||||
}
|
||||
return profile
|
||||
}
|
||||
|
||||
// hasValidCredentials 检查 GDrive 配置是否有有效凭证。
|
||||
func (s *SoraGDriveStorage) hasValidCredentials(profile *SoraS3Profile) bool {
|
||||
switch profile.AuthType {
|
||||
case "oauth2":
|
||||
return profile.ClientID != "" && profile.ClientSecret != "" && profile.RefreshToken != ""
|
||||
case "service_account":
|
||||
return profile.ServiceAccountJSON != ""
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// getService 获取或初始化 Drive 服务(带缓存)。
|
||||
func (s *SoraGDriveStorage) getService(ctx context.Context) (*drive.Service, *SoraS3Profile, error) {
|
||||
s.mu.RLock()
|
||||
if s.srv != nil && s.cfg != nil {
|
||||
srv, cfg := s.srv, s.cfg
|
||||
s.mu.RUnlock()
|
||||
return srv, cfg, nil
|
||||
}
|
||||
s.mu.RUnlock()
|
||||
|
||||
return s.initService(ctx)
|
||||
}
|
||||
|
||||
func (s *SoraGDriveStorage) initService(ctx context.Context) (*drive.Service, *SoraS3Profile, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
// 双重检查
|
||||
if s.srv != nil && s.cfg != nil {
|
||||
return s.srv, s.cfg, nil
|
||||
}
|
||||
|
||||
profile := s.getActiveGDriveProfile(ctx)
|
||||
if profile == nil {
|
||||
return nil, nil, fmt.Errorf("no active gdrive profile found")
|
||||
}
|
||||
if !profile.Enabled {
|
||||
return nil, nil, fmt.Errorf("gdrive storage is disabled")
|
||||
}
|
||||
|
||||
srv, err := s.buildDriveService(ctx, profile)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("build gdrive service: %w", err)
|
||||
}
|
||||
|
||||
s.srv = srv
|
||||
s.cfg = profile
|
||||
logger.LegacyPrintf("service.sora_gdrive", "[SoraGDrive] 客户端已初始化 auth_type=%s folder_id=%s", profile.AuthType, profile.FolderID)
|
||||
return srv, profile, nil
|
||||
}
|
||||
|
||||
// buildDriveService 根据认证类型创建 Google Drive 服务。
|
||||
func (s *SoraGDriveStorage) buildDriveService(ctx context.Context, profile *SoraS3Profile) (*drive.Service, error) {
|
||||
switch profile.AuthType {
|
||||
case "oauth2":
|
||||
return s.buildOAuth2Service(ctx, profile)
|
||||
case "service_account":
|
||||
return s.buildServiceAccountService(ctx, profile)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported auth_type: %s", profile.AuthType)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SoraGDriveStorage) buildOAuth2Service(ctx context.Context, profile *SoraS3Profile) (*drive.Service, error) {
|
||||
config := &oauth2.Config{
|
||||
ClientID: profile.ClientID,
|
||||
ClientSecret: profile.ClientSecret,
|
||||
Endpoint: google.Endpoint,
|
||||
Scopes: []string{drive.DriveFileScope},
|
||||
}
|
||||
token := &oauth2.Token{
|
||||
RefreshToken: profile.RefreshToken,
|
||||
}
|
||||
tokenSource := config.TokenSource(ctx, token)
|
||||
srv, err := drive.NewService(ctx, option.WithTokenSource(tokenSource))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create gdrive oauth2 service: %w", err)
|
||||
}
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
func (s *SoraGDriveStorage) buildServiceAccountService(ctx context.Context, profile *SoraS3Profile) (*drive.Service, error) {
|
||||
srv, err := drive.NewService(ctx, option.WithCredentialsJSON([]byte(profile.ServiceAccountJSON))) //nolint:staticcheck // SA1019: admin-controlled service account JSON, safe to use
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create gdrive service account service: %w", err)
|
||||
}
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
// RefreshClient 清除缓存的 Drive 客户端。
|
||||
func (s *SoraGDriveStorage) RefreshClient() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.srv = nil
|
||||
s.cfg = nil
|
||||
s.healthCheckedAt = time.Time{}
|
||||
s.healthErr = nil
|
||||
logger.LegacyPrintf("service.sora_gdrive", "[SoraGDrive] 客户端缓存已清除")
|
||||
}
|
||||
|
||||
// GDriveQuotaInfo 包含 Google Drive 配额信息。
|
||||
type GDriveQuotaInfo struct {
|
||||
LimitBytes int64 `json:"limit_bytes"`
|
||||
UsedBytes int64 `json:"used_bytes"`
|
||||
}
|
||||
|
||||
// TestConnection 测试 Google Drive 连接。
|
||||
func (s *SoraGDriveStorage) TestConnection(ctx context.Context) error {
|
||||
srv, _, err := s.getService(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = srv.About.Get().Fields("storageQuota").Context(ctx).Do()
|
||||
if err != nil {
|
||||
return fmt.Errorf("gdrive About.Get failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetQuotaInfo 获取 Google Drive 配额信息(总量和已用量)。
|
||||
func (s *SoraGDriveStorage) GetQuotaInfo(ctx context.Context) (*GDriveQuotaInfo, error) {
|
||||
srv, _, err := s.getService(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
about, err := srv.About.Get().Fields("storageQuota").Context(ctx).Do()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("gdrive About.Get failed: %w", err)
|
||||
}
|
||||
if about.StorageQuota == nil {
|
||||
return nil, fmt.Errorf("storageQuota not available")
|
||||
}
|
||||
return &GDriveQuotaInfo{
|
||||
LimitBytes: about.StorageQuota.Limit,
|
||||
UsedBytes: about.StorageQuota.Usage,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// TestFullCycle 执行完整的上传→获取链接→删除测试。
|
||||
func (s *SoraGDriveStorage) TestFullCycle(ctx context.Context) (map[string]any, error) {
|
||||
srv, cfg, err := s.getService(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("init client: %w", err)
|
||||
}
|
||||
|
||||
result := map[string]any{}
|
||||
|
||||
// 1. 测试 API 连接
|
||||
about, err := srv.About.Get().Fields("storageQuota").Context(ctx).Do()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("API connection failed: %w", err)
|
||||
}
|
||||
if about.StorageQuota != nil {
|
||||
result["quota_limit_bytes"] = about.StorageQuota.Limit
|
||||
result["quota_used_bytes"] = about.StorageQuota.Usage
|
||||
}
|
||||
|
||||
// 2. 上传测试文件
|
||||
testContent := "sub2api GDrive test file - " + time.Now().Format(time.RFC3339)
|
||||
fileMeta := &drive.File{
|
||||
Name: "sub2api_test_" + uuid.NewString()[:8] + ".txt",
|
||||
MimeType: "text/plain",
|
||||
}
|
||||
if cfg.FolderID != "" {
|
||||
fileMeta.Parents = []string{cfg.FolderID}
|
||||
}
|
||||
uploaded, err := srv.Files.Create(fileMeta).
|
||||
Media(strings.NewReader(testContent)).
|
||||
Fields("id,name,size,webViewLink").
|
||||
Context(ctx).Do()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("upload test file failed: %w", err)
|
||||
}
|
||||
result["uploaded_file_id"] = uploaded.Id
|
||||
result["uploaded_file_name"] = uploaded.Name
|
||||
result["uploaded_file_size"] = uploaded.Size
|
||||
result["web_view_link"] = uploaded.WebViewLink
|
||||
|
||||
// 3. 获取访问链接
|
||||
accessURL, err := s.GetAccessURL(ctx, uploaded.Id)
|
||||
if err != nil {
|
||||
// 即使获取链接失败,仍尝试清理
|
||||
_ = srv.Files.Delete(uploaded.Id).Context(ctx).Do()
|
||||
return nil, fmt.Errorf("get access URL failed: %w", err)
|
||||
}
|
||||
result["access_url"] = accessURL
|
||||
|
||||
// 4. 删除测试文件
|
||||
if err := srv.Files.Delete(uploaded.Id).Context(ctx).Do(); err != nil {
|
||||
result["delete_warning"] = fmt.Sprintf("delete failed (manual cleanup needed): %v", err)
|
||||
} else {
|
||||
result["deleted"] = true
|
||||
}
|
||||
|
||||
result["status"] = "ok"
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// IsHealthy 返回 Google Drive 健康状态(带短缓存)。
|
||||
func (s *SoraGDriveStorage) IsHealthy(ctx context.Context) bool {
|
||||
if s == nil {
|
||||
return false
|
||||
}
|
||||
now := time.Now()
|
||||
s.mu.RLock()
|
||||
lastCheck := s.healthCheckedAt
|
||||
lastErr := s.healthErr
|
||||
ttl := s.healthTTL
|
||||
s.mu.RUnlock()
|
||||
|
||||
if ttl <= 0 {
|
||||
ttl = defaultGDriveHealthTTL
|
||||
}
|
||||
if !lastCheck.IsZero() && now.Sub(lastCheck) < ttl {
|
||||
return lastErr == nil
|
||||
}
|
||||
|
||||
err := s.TestConnection(ctx)
|
||||
s.mu.Lock()
|
||||
s.healthCheckedAt = time.Now()
|
||||
s.healthErr = err
|
||||
s.mu.Unlock()
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// UploadFromURL 从上游 URL 下载并上传到 Google Drive。
|
||||
// 返回 Google Drive 文件 ID 作为 objectKey、文件大小、存储类型。
|
||||
func (s *SoraGDriveStorage) UploadFromURL(ctx context.Context, userID int64, sourceURL string) (string, int64, string, error) {
|
||||
srv, cfg, err := s.getService(ctx)
|
||||
if err != nil {
|
||||
return "", 0, "", err
|
||||
}
|
||||
|
||||
// 下载源文件
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, sourceURL, nil)
|
||||
if err != nil {
|
||||
return "", 0, "", fmt.Errorf("create download request: %w", err)
|
||||
}
|
||||
httpClient := &http.Client{Timeout: 5 * time.Minute}
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return "", 0, "", fmt.Errorf("download from upstream: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", 0, "", &UpstreamDownloadError{StatusCode: resp.StatusCode}
|
||||
}
|
||||
|
||||
// 推断文件扩展名和 MIME
|
||||
ext := fileExtFromURL(sourceURL)
|
||||
if ext == "" {
|
||||
ext = fileExtFromContentType(resp.Header.Get("Content-Type"))
|
||||
}
|
||||
if ext == "" {
|
||||
ext = ".bin"
|
||||
}
|
||||
contentType := resp.Header.Get("Content-Type")
|
||||
if contentType == "" {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
|
||||
// 生成文件名
|
||||
datePath := time.Now().Format("2006-01-02")
|
||||
fileName := fmt.Sprintf("sora_%d_%s_%s%s", userID, datePath, uuid.NewString()[:8], ext)
|
||||
|
||||
// 创建文件元数据
|
||||
fileMeta := &drive.File{
|
||||
Name: fileName,
|
||||
MimeType: contentType,
|
||||
}
|
||||
if cfg.FolderID != "" {
|
||||
fileMeta.Parents = []string{cfg.FolderID}
|
||||
}
|
||||
|
||||
// 使用 CountingReader 统计大小
|
||||
cr := &countingReader{Reader: resp.Body}
|
||||
|
||||
// 上传到 Google Drive
|
||||
created, err := srv.Files.Create(fileMeta).
|
||||
Media(cr).
|
||||
Fields("id, size").
|
||||
Context(ctx).
|
||||
Do()
|
||||
if err != nil {
|
||||
return "", 0, "", fmt.Errorf("gdrive upload: %w", err)
|
||||
}
|
||||
|
||||
fileSize := cr.BytesRead
|
||||
if created.Size > 0 {
|
||||
fileSize = created.Size
|
||||
}
|
||||
|
||||
// 根据 access_mode 设置权限
|
||||
if cfg.AccessMode == "" || cfg.AccessMode == "direct" {
|
||||
// 设为任何人可读
|
||||
_, permErr := srv.Permissions.Create(created.Id, &drive.Permission{
|
||||
Type: "anyone",
|
||||
Role: "reader",
|
||||
}).Context(ctx).Do()
|
||||
if permErr != nil {
|
||||
logger.LegacyPrintf("service.sora_gdrive", "[SoraGDrive] 设置公开权限失败 fileID=%s err=%v", created.Id, permErr)
|
||||
}
|
||||
}
|
||||
|
||||
logger.LegacyPrintf("service.sora_gdrive", "[SoraGDrive] 上传完成 fileID=%s size=%d", created.Id, fileSize)
|
||||
return created.Id, fileSize, SoraStorageTypeGDrive, nil
|
||||
}
|
||||
|
||||
// DeleteObjects 删除一组 Google Drive 文件。
|
||||
func (s *SoraGDriveStorage) DeleteObjects(ctx context.Context, objectKeys []string) error {
|
||||
if len(objectKeys) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
srv, _, err := s.getService(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var lastErr error
|
||||
for _, fileID := range objectKeys {
|
||||
if err := srv.Files.Delete(fileID).Context(ctx).Do(); err != nil {
|
||||
logger.LegacyPrintf("service.sora_gdrive", "[SoraGDrive] 删除失败 fileID=%s err=%v", fileID, err)
|
||||
lastErr = err
|
||||
}
|
||||
}
|
||||
return lastErr
|
||||
}
|
||||
|
||||
// GetAccessURL 获取 Google Drive 文件的访问 URL。
|
||||
func (s *SoraGDriveStorage) GetAccessURL(ctx context.Context, objectKey string) (string, error) {
|
||||
_, cfg, err := s.getService(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// CDN URL 优先
|
||||
if cfg.CDNURL != "" {
|
||||
cdnBase := strings.TrimRight(cfg.CDNURL, "/")
|
||||
return cdnBase + "/" + objectKey, nil
|
||||
}
|
||||
|
||||
// 默认使用 Google Drive 直链
|
||||
return fmt.Sprintf("https://drive.google.com/uc?export=download&id=%s", objectKey), nil
|
||||
}
|
||||
|
||||
// countingReader 包装 io.Reader 以统计读取的字节数。
|
||||
type countingReader struct {
|
||||
Reader io.Reader
|
||||
BytesRead int64
|
||||
}
|
||||
|
||||
func (r *countingReader) Read(p []byte) (int, error) {
|
||||
n, err := r.Reader.Read(p)
|
||||
r.BytesRead += int64(n)
|
||||
return n, err
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
//go:build unit
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSoraGDriveStorage_StorageType(t *testing.T) {
|
||||
s := NewSoraGDriveStorage(nil)
|
||||
assert.Equal(t, SoraStorageTypeGDrive, s.StorageType())
|
||||
}
|
||||
|
||||
func TestSoraGDriveStorage_EnabledWithNilSettingService(t *testing.T) {
|
||||
s := NewSoraGDriveStorage(nil)
|
||||
assert.False(t, s.Enabled(context.Background()))
|
||||
}
|
||||
|
||||
func TestSoraGDriveStorage_IsHealthyWithNilReceiver(t *testing.T) {
|
||||
var s *SoraGDriveStorage
|
||||
assert.False(t, s.IsHealthy(context.Background()))
|
||||
}
|
||||
|
||||
func TestSoraGDriveStorage_GetServiceWithoutProfile(t *testing.T) {
|
||||
s := NewSoraGDriveStorage(nil)
|
||||
_, _, err := s.getService(context.Background())
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "no active gdrive profile")
|
||||
}
|
||||
|
||||
func TestSoraGDriveStorage_DeleteObjectsEmpty(t *testing.T) {
|
||||
s := NewSoraGDriveStorage(nil)
|
||||
err := s.DeleteObjects(context.Background(), []string{})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestSoraGDriveStorage_RefreshClient(t *testing.T) {
|
||||
s := NewSoraGDriveStorage(nil)
|
||||
// 不应 panic
|
||||
s.RefreshClient()
|
||||
assert.Nil(t, s.srv)
|
||||
assert.Nil(t, s.cfg)
|
||||
}
|
||||
|
||||
func TestSoraGDriveStorage_HasValidCredentials(t *testing.T) {
|
||||
s := NewSoraGDriveStorage(nil)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
profile *SoraS3Profile
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "oauth2 with all fields",
|
||||
profile: &SoraS3Profile{
|
||||
AuthType: "oauth2",
|
||||
ClientID: "id",
|
||||
ClientSecret: "secret",
|
||||
RefreshToken: "token",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "oauth2 missing refresh token",
|
||||
profile: &SoraS3Profile{
|
||||
AuthType: "oauth2",
|
||||
ClientID: "id",
|
||||
ClientSecret: "secret",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "service_account with json",
|
||||
profile: &SoraS3Profile{
|
||||
AuthType: "service_account",
|
||||
ServiceAccountJSON: `{"type":"service_account"}`,
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "service_account without json",
|
||||
profile: &SoraS3Profile{
|
||||
AuthType: "service_account",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "unknown auth type",
|
||||
profile: &SoraS3Profile{
|
||||
AuthType: "unknown",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := s.hasValidCredentials(tt.profile)
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSoraStorageRouter_DefaultsToS3(t *testing.T) {
|
||||
s3 := NewSoraS3Storage(nil)
|
||||
router := NewSoraStorageRouter(nil, s3, nil)
|
||||
// settingService 为 nil,应返回 s3Storage
|
||||
backend := router.activeBackend(context.Background())
|
||||
assert.Equal(t, s3, backend)
|
||||
}
|
||||
|
||||
func TestSoraStorageRouter_StorageType(t *testing.T) {
|
||||
router := NewSoraStorageRouter(nil, nil, nil)
|
||||
assert.Equal(t, SoraStorageTypeS3, router.StorageType())
|
||||
}
|
||||
|
||||
func TestSoraStorageRouter_RefreshAllNoPanic(t *testing.T) {
|
||||
router := NewSoraStorageRouter(nil, nil, nil)
|
||||
// 不应 panic
|
||||
router.RefreshAll()
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package service
|
||||
import "context"
|
||||
|
||||
// SoraObjectStorage 是 Sora 媒体文件的通用对象存储接口。
|
||||
// S3 和 Google Drive 等存储后端均实现此接口。
|
||||
type SoraObjectStorage interface {
|
||||
// Enabled 返回存储是否已启用且配置有效。
|
||||
Enabled(ctx context.Context) bool
|
||||
@@ -15,7 +14,7 @@ type SoraObjectStorage interface {
|
||||
TestConnection(ctx context.Context) error
|
||||
|
||||
// UploadFromURL 从上游 URL 下载并上传到存储。
|
||||
// 返回 object key(S3 key 或 GDrive file ID)、文件大小、实际使用的存储类型。
|
||||
// 返回 object key、文件大小、实际使用的存储类型。
|
||||
UploadFromURL(ctx context.Context, userID int64, sourceURL string) (objectKey string, sizeBytes int64, storageType string, err error)
|
||||
|
||||
// DeleteObjects 删除一组存储对象。
|
||||
@@ -27,11 +26,11 @@ type SoraObjectStorage interface {
|
||||
// RefreshClient 清除缓存客户端,配置变更时调用。
|
||||
RefreshClient()
|
||||
|
||||
// StorageType 返回存储类型标识("s3" / "gdrive")。
|
||||
// StorageType 返回存储类型标识。
|
||||
StorageType() string
|
||||
}
|
||||
|
||||
// IsObjectStorageType 判断是否为对象存储类型(S3 或 Google Drive)。
|
||||
// IsObjectStorageType 判断是否为对象存储类型。
|
||||
func IsObjectStorageType(t string) bool {
|
||||
return t == SoraStorageTypeS3 || t == SoraStorageTypeGDrive
|
||||
return t == SoraStorageTypeS3
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/logger"
|
||||
)
|
||||
|
||||
// SoraStorageRouter 根据激活 profile 的 provider 字段路由到对应存储实现。
|
||||
// 实现 SoraObjectStorage 接口。
|
||||
type SoraStorageRouter struct {
|
||||
settingService *SettingService
|
||||
s3Storage *SoraS3Storage
|
||||
gdriveStorage SoraObjectStorage // 可为 nil(GDrive 未实现时)
|
||||
}
|
||||
|
||||
// NewSoraStorageRouter 创建存储路由。
|
||||
func NewSoraStorageRouter(
|
||||
settingService *SettingService,
|
||||
s3Storage *SoraS3Storage,
|
||||
gdriveStorage SoraObjectStorage,
|
||||
) *SoraStorageRouter {
|
||||
return &SoraStorageRouter{
|
||||
settingService: settingService,
|
||||
s3Storage: s3Storage,
|
||||
gdriveStorage: gdriveStorage,
|
||||
}
|
||||
}
|
||||
|
||||
// activeBackend 返回当前激活 profile 对应的存储后端。
|
||||
func (r *SoraStorageRouter) activeBackend(ctx context.Context) SoraObjectStorage {
|
||||
if r.settingService == nil {
|
||||
return r.s3Storage // 默认 S3
|
||||
}
|
||||
|
||||
profile, err := r.settingService.GetActiveStorageProfile(ctx)
|
||||
if err != nil || profile == nil {
|
||||
return r.s3Storage // 默认 S3
|
||||
}
|
||||
|
||||
switch profile.GetProvider() {
|
||||
case SoraStorageTypeGDrive:
|
||||
if r.gdriveStorage != nil {
|
||||
return r.gdriveStorage
|
||||
}
|
||||
logger.LegacyPrintf("service.storage_router", "[StorageRouter] GDrive 后端未初始化,降级到 S3")
|
||||
return r.s3Storage
|
||||
default:
|
||||
return r.s3Storage
|
||||
}
|
||||
}
|
||||
|
||||
func (r *SoraStorageRouter) Enabled(ctx context.Context) bool {
|
||||
backend := r.activeBackend(ctx)
|
||||
if backend == nil {
|
||||
return false
|
||||
}
|
||||
return backend.Enabled(ctx)
|
||||
}
|
||||
|
||||
func (r *SoraStorageRouter) IsHealthy(ctx context.Context) bool {
|
||||
backend := r.activeBackend(ctx)
|
||||
if backend == nil {
|
||||
return false
|
||||
}
|
||||
return backend.IsHealthy(ctx)
|
||||
}
|
||||
|
||||
func (r *SoraStorageRouter) TestConnection(ctx context.Context) error {
|
||||
backend := r.activeBackend(ctx)
|
||||
if backend == nil {
|
||||
return fmt.Errorf("no storage backend available")
|
||||
}
|
||||
return backend.TestConnection(ctx)
|
||||
}
|
||||
|
||||
func (r *SoraStorageRouter) UploadFromURL(ctx context.Context, userID int64, sourceURL string) (string, int64, string, error) {
|
||||
backend := r.activeBackend(ctx)
|
||||
if backend == nil {
|
||||
return "", 0, "", fmt.Errorf("no storage backend available")
|
||||
}
|
||||
return backend.UploadFromURL(ctx, userID, sourceURL)
|
||||
}
|
||||
|
||||
func (r *SoraStorageRouter) DeleteObjects(ctx context.Context, objectKeys []string) error {
|
||||
backend := r.activeBackend(ctx)
|
||||
if backend == nil {
|
||||
return fmt.Errorf("no storage backend available")
|
||||
}
|
||||
return backend.DeleteObjects(ctx, objectKeys)
|
||||
}
|
||||
|
||||
func (r *SoraStorageRouter) GetAccessURL(ctx context.Context, objectKey string) (string, error) {
|
||||
backend := r.activeBackend(ctx)
|
||||
if backend == nil {
|
||||
return "", fmt.Errorf("no storage backend available")
|
||||
}
|
||||
return backend.GetAccessURL(ctx, objectKey)
|
||||
}
|
||||
|
||||
func (r *SoraStorageRouter) RefreshClient() {
|
||||
if r.s3Storage != nil {
|
||||
r.s3Storage.RefreshClient()
|
||||
}
|
||||
if r.gdriveStorage != nil {
|
||||
r.gdriveStorage.RefreshClient()
|
||||
}
|
||||
}
|
||||
|
||||
// RefreshAll 刷新所有后端客户端(用作配置变更回调)。
|
||||
func (r *SoraStorageRouter) RefreshAll() {
|
||||
r.RefreshClient()
|
||||
}
|
||||
|
||||
func (r *SoraStorageRouter) StorageType() string {
|
||||
// 不带 context 的方法,返回默认值
|
||||
// 真实的 StorageType 在 activeBackend 中动态确定
|
||||
return SoraStorageTypeS3
|
||||
}
|
||||
|
||||
// StorageTypeWithContext 返回当前激活后端的存储类型。
|
||||
func (r *SoraStorageRouter) StorageTypeWithContext(ctx context.Context) string {
|
||||
backend := r.activeBackend(ctx)
|
||||
if backend == nil {
|
||||
return SoraStorageTypeS3
|
||||
}
|
||||
return backend.StorageType()
|
||||
}
|
||||
Reference in new issue
Block a user