Skip to content

Commit 70f8a59

Browse files
committed
fix: improve error handling and validation in Ali video request conversion
1 parent a4cf9bb commit 70f8a59

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

relay/channel/task/ali/adaptor.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"strconv"
89
"strings"
910

1011
"github.com/QuantumNous/new-api/common"
1112
"github.com/QuantumNous/new-api/dto"
13+
"github.com/QuantumNous/new-api/logger"
1214
"github.com/QuantumNous/new-api/model"
1315
"github.com/QuantumNous/new-api/relay/channel"
1416
relaycommon "github.com/QuantumNous/new-api/relay/common"
@@ -123,7 +125,12 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
123125
if err := common.UnmarshalBodyReusable(c, &taskReq); err != nil {
124126
return service.TaskErrorWrapper(err, "unmarshal_task_request_failed", http.StatusBadRequest)
125127
}
126-
a.aliReq = a.convertToAliRequest(info, taskReq)
128+
aliReq, err := a.convertToAliRequest(info, taskReq)
129+
if err != nil {
130+
return service.TaskErrorWrapper(err, "convert_to_ali_request_failed", http.StatusInternalServerError)
131+
}
132+
a.aliReq = aliReq
133+
logger.LogJson(c, "ali video request body", aliReq)
127134
return relaycommon.ValidateMultipartDirect(c, info)
128135
}
129136

@@ -148,7 +155,7 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.RelayIn
148155
return bytes.NewReader(bodyBytes), nil
149156
}
150157

151-
func (a *TaskAdaptor) convertToAliRequest(info *relaycommon.RelayInfo, req relaycommon.TaskSubmitReq) *AliVideoRequest {
158+
func (a *TaskAdaptor) convertToAliRequest(info *relaycommon.RelayInfo, req relaycommon.TaskSubmitReq) (*AliVideoRequest, error) {
152159
otherRatios := map[string]map[string]float64{
153160
"wan2.5-i2v-preview": {
154161
"480P": 1,
@@ -209,20 +216,35 @@ func (a *TaskAdaptor) convertToAliRequest(info *relaycommon.RelayInfo, req relay
209216
// 处理时长
210217
if req.Duration > 0 {
211218
aliReq.Parameters.Duration = req.Duration
219+
} else if req.Seconds != "" {
220+
seconds, err := strconv.Atoi(req.Seconds)
221+
if err != nil {
222+
return nil, errors.Wrap(err, "convert seconds to int failed")
223+
} else {
224+
aliReq.Parameters.Duration = seconds
225+
}
212226
} else {
213227
aliReq.Parameters.Duration = 5 // 默认5秒
214228
}
215229

216230
// 从 metadata 中提取额外参数
217231
if req.Metadata != nil {
218232
if metadataBytes, err := common.Marshal(req.Metadata); err == nil {
219-
_ = common.Unmarshal(metadataBytes, aliReq)
233+
err = common.Unmarshal(metadataBytes, aliReq)
234+
if err != nil {
235+
return nil, errors.Wrap(err, "unmarshal metadata failed")
236+
}
237+
} else {
238+
return nil, errors.Wrap(err, "marshal metadata failed")
220239
}
221240
}
222241

242+
if aliReq.Model != req.Model {
243+
return nil, errors.New("can't change model with metadata")
244+
}
245+
223246
info.PriceData.OtherRatios = map[string]float64{
224247
"seconds": float64(aliReq.Parameters.Duration),
225-
//"size": 1,
226248
}
227249

228250
if otherRatio, ok := otherRatios[req.Model]; ok {
@@ -233,7 +255,7 @@ func (a *TaskAdaptor) convertToAliRequest(info *relaycommon.RelayInfo, req relay
233255

234256
// println(fmt.Sprintf("other ratios: %v", info.PriceData.OtherRatios))
235257

236-
return aliReq
258+
return aliReq, nil
237259
}
238260

239261
// DoRequest delegates to common helper

0 commit comments

Comments
 (0)