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
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"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)
|
||||
if err != nil {
|
||||
logger.LegacyPrintf("handler.sora_videos", "[CreateVideo] error: %v", err)
|
||||
soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create video task")
|
||||
handleTaskCreateError(c, "CreateVideo", err)
|
||||
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)
|
||||
if err != nil {
|
||||
logger.LegacyPrintf("handler.sora_videos", "[RemixVideo] error: %v", err)
|
||||
soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create remix task")
|
||||
handleTaskCreateError(c, "RemixVideo", err)
|
||||
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)
|
||||
if err != nil {
|
||||
logger.LegacyPrintf("handler.sora_videos", "[CreateImage] error: %v", err)
|
||||
soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create image task")
|
||||
handleTaskCreateError(c, "CreateImage", err)
|
||||
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)
|
||||
if err != nil {
|
||||
logger.LegacyPrintf("handler.sora_videos", "[EditImage] error: %v", err)
|
||||
soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create image edit task")
|
||||
handleTaskCreateError(c, "EditImage", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -334,17 +331,8 @@ func (h *SoraVideosHandler) selectAccount(c *gin.Context, model string) (*servic
|
||||
return apiKey, selection.Account, true
|
||||
}
|
||||
|
||||
func (h *SoraVideosHandler) selectAccountByID(c *gin.Context, _ int64) (*service.Account, error) {
|
||||
selection, err := h.gatewayService.SelectAccountWithLoadAwareness(
|
||||
c.Request.Context(), nil, "", "", nil, "",
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if selection.ReleaseFunc != nil {
|
||||
selection.ReleaseFunc()
|
||||
}
|
||||
return selection.Account, nil
|
||||
func (h *SoraVideosHandler) selectAccountByID(c *gin.Context, accountID int64) (*service.Account, error) {
|
||||
return h.taskService.GetAccountByID(c.Request.Context(), accountID)
|
||||
}
|
||||
|
||||
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 {
|
||||
switch size {
|
||||
case "540x360":
|
||||
|
||||
Reference in new issue
Block a user