mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 15:06:36 +00:00
fix: Sora task API improvements
- selectAccountByID: use original task's account for remix instead of random selection, preventing permission issues in OAuth path - Upstream error passthrough: forward HTTP status code and body from upstream instead of always returning 500 - Add accountRepo to SoraTaskService for direct account lookup Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
4e14409d04
commit
0af1f5841d
3 files changed
+28
-21
No files matched your search
@@ -214,7 +214,7 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|||||||
soraGatewayService := service.NewSoraGatewayService(soraSDKClient, rateLimitService, httpUpstream, configConfig)
|
soraGatewayService := service.NewSoraGatewayService(soraSDKClient, rateLimitService, httpUpstream, configConfig)
|
||||||
soraClientHandler := handler.NewSoraClientHandler(soraGenerationService, soraQuotaService, soraStorageRouter, soraGatewayService, gatewayService, soraMediaStorage, apiKeyService)
|
soraClientHandler := handler.NewSoraClientHandler(soraGenerationService, soraQuotaService, soraStorageRouter, soraGatewayService, gatewayService, soraMediaStorage, apiKeyService)
|
||||||
soraTaskRepository := repository.NewSoraTaskRepository(db)
|
soraTaskRepository := repository.NewSoraTaskRepository(db)
|
||||||
soraTaskService := service.NewSoraTaskService(soraTaskRepository, soraSDKClient, httpUpstream)
|
soraTaskService := service.NewSoraTaskService(soraTaskRepository, accountRepository, soraSDKClient, httpUpstream)
|
||||||
soraTaskWorker := service.NewSoraTaskWorker(soraTaskService, accountRepository, soraStorageRouter, soraMediaStorage, 60*time.Second)
|
soraTaskWorker := service.NewSoraTaskWorker(soraTaskService, accountRepository, soraStorageRouter, soraMediaStorage, 60*time.Second)
|
||||||
soraTaskWorker.Start()
|
soraTaskWorker.Start()
|
||||||
soraVideosHandler := handler.NewSoraVideosHandler(soraTaskService, gatewayService, soraStorageRouter, soraMediaStorage, soraGatewayService)
|
soraVideosHandler := handler.NewSoraVideosHandler(soraTaskService, gatewayService, soraStorageRouter, soraMediaStorage, soraGatewayService)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -64,8 +65,7 @@ func (h *SoraVideosHandler) CreateVideo(c *gin.Context) {
|
|||||||
|
|
||||||
task, err := h.taskService.CreateVideoTask(c.Request.Context(), apiKey.ID, account, &req, body)
|
task, err := h.taskService.CreateVideoTask(c.Request.Context(), apiKey.ID, account, &req, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.LegacyPrintf("handler.sora_videos", "[CreateVideo] error: %v", err)
|
handleTaskCreateError(c, "CreateVideo", err)
|
||||||
soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create video task")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,8 +146,7 @@ func (h *SoraVideosHandler) RemixVideo(c *gin.Context) {
|
|||||||
|
|
||||||
task, err := h.taskService.CreateVideoTask(c.Request.Context(), apiKey.ID, account, videoReq, reqBody)
|
task, err := h.taskService.CreateVideoTask(c.Request.Context(), apiKey.ID, account, videoReq, reqBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.LegacyPrintf("handler.sora_videos", "[RemixVideo] error: %v", err)
|
handleTaskCreateError(c, "RemixVideo", err)
|
||||||
soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create remix task")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,8 +249,7 @@ func (h *SoraVideosHandler) CreateImage(c *gin.Context) {
|
|||||||
|
|
||||||
task, err := h.taskService.CreateImageGeneration(c.Request.Context(), apiKey.ID, account, &req, body)
|
task, err := h.taskService.CreateImageGeneration(c.Request.Context(), apiKey.ID, account, &req, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.LegacyPrintf("handler.sora_videos", "[CreateImage] error: %v", err)
|
handleTaskCreateError(c, "CreateImage", err)
|
||||||
soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create image task")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,8 +294,7 @@ func (h *SoraVideosHandler) EditImage(c *gin.Context) {
|
|||||||
|
|
||||||
task, err := h.taskService.CreateImageGeneration(c.Request.Context(), apiKey.ID, account, imageReq, body)
|
task, err := h.taskService.CreateImageGeneration(c.Request.Context(), apiKey.ID, account, imageReq, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.LegacyPrintf("handler.sora_videos", "[EditImage] error: %v", err)
|
handleTaskCreateError(c, "EditImage", err)
|
||||||
soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create image edit task")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -334,17 +331,8 @@ func (h *SoraVideosHandler) selectAccount(c *gin.Context, model string) (*servic
|
|||||||
return apiKey, selection.Account, true
|
return apiKey, selection.Account, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SoraVideosHandler) selectAccountByID(c *gin.Context, _ int64) (*service.Account, error) {
|
func (h *SoraVideosHandler) selectAccountByID(c *gin.Context, accountID int64) (*service.Account, error) {
|
||||||
selection, err := h.gatewayService.SelectAccountWithLoadAwareness(
|
return h.taskService.GetAccountByID(c.Request.Context(), accountID)
|
||||||
c.Request.Context(), nil, "", "", nil, "",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if selection.ReleaseFunc != nil {
|
|
||||||
selection.ReleaseFunc()
|
|
||||||
}
|
|
||||||
return selection.Account, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func readBody(c *gin.Context) ([]byte, error) {
|
func readBody(c *gin.Context) ([]byte, error) {
|
||||||
@@ -373,6 +361,18 @@ func soraErrorResponse(c *gin.Context, status int, errType, message string) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handleTaskCreateError writes the appropriate error response, transparently
|
||||||
|
// forwarding upstream HTTP status codes when available.
|
||||||
|
func handleTaskCreateError(c *gin.Context, logTag string, err error) {
|
||||||
|
logger.LegacyPrintf("handler.sora_videos", "[%s] error: %v", logTag, err)
|
||||||
|
var ue *service.SoraUpstreamError
|
||||||
|
if errors.As(err, &ue) {
|
||||||
|
c.Data(ue.StatusCode, "application/json", ue.Body)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create task")
|
||||||
|
}
|
||||||
|
|
||||||
func inferImageModel(size string) string {
|
func inferImageModel(size string) string {
|
||||||
switch size {
|
switch size {
|
||||||
case "540x360":
|
case "540x360":
|
||||||
|
|||||||
@@ -18,17 +18,20 @@ import (
|
|||||||
// SoraTaskService manages Sora async task creation and status queries.
|
// SoraTaskService manages Sora async task creation and status queries.
|
||||||
type SoraTaskService struct {
|
type SoraTaskService struct {
|
||||||
repo SoraTaskRepository
|
repo SoraTaskRepository
|
||||||
|
accountRepo AccountRepository
|
||||||
soraClient SoraClient
|
soraClient SoraClient
|
||||||
httpUpstream HTTPUpstream
|
httpUpstream HTTPUpstream
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSoraTaskService(
|
func NewSoraTaskService(
|
||||||
repo SoraTaskRepository,
|
repo SoraTaskRepository,
|
||||||
|
accountRepo AccountRepository,
|
||||||
soraClient SoraClient,
|
soraClient SoraClient,
|
||||||
httpUpstream HTTPUpstream,
|
httpUpstream HTTPUpstream,
|
||||||
) *SoraTaskService {
|
) *SoraTaskService {
|
||||||
return &SoraTaskService{
|
return &SoraTaskService{
|
||||||
repo: repo,
|
repo: repo,
|
||||||
|
accountRepo: accountRepo,
|
||||||
soraClient: soraClient,
|
soraClient: soraClient,
|
||||||
httpUpstream: httpUpstream,
|
httpUpstream: httpUpstream,
|
||||||
}
|
}
|
||||||
@@ -259,6 +262,10 @@ func (s *SoraTaskService) GetTaskByID(ctx context.Context, taskID string) (*Sora
|
|||||||
return s.repo.GetByID(ctx, taskID)
|
return s.repo.GetByID(ctx, taskID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SoraTaskService) GetAccountByID(ctx context.Context, accountID int64) (*Account, error) {
|
||||||
|
return s.accountRepo.GetByID(ctx, accountID)
|
||||||
|
}
|
||||||
|
|
||||||
// ── Internal methods ──
|
// ── Internal methods ──
|
||||||
|
|
||||||
func (s *SoraTaskService) createViaSdk(
|
func (s *SoraTaskService) createViaSdk(
|
||||||
@@ -324,7 +331,7 @@ func (s *SoraTaskService) forwardCreateToUpstream(
|
|||||||
return nil, fmt.Errorf("read upstream response: %w", err)
|
return nil, fmt.Errorf("read upstream response: %w", err)
|
||||||
}
|
}
|
||||||
if resp.StatusCode >= 400 {
|
if resp.StatusCode >= 400 {
|
||||||
return nil, fmt.Errorf("upstream error %d: %s", resp.StatusCode, string(respBody))
|
return nil, &SoraUpstreamError{StatusCode: resp.StatusCode, Body: respBody}
|
||||||
}
|
}
|
||||||
|
|
||||||
var result map[string]any
|
var result map[string]any
|
||||||
|
|||||||
Reference in new issue
Block a user