|
- package service
-
- import (
- "context"
- "fmt"
- "net/http"
- "strings"
-
- "github.com/QuantumNous/new-api/common"
- "github.com/QuantumNous/new-api/constant"
- "github.com/QuantumNous/new-api/model"
- )
-
- // IsDoubaoVideoAssetChannel reports whether the channel is an official
- // Volcengine (DoubaoVideo) channel whose assets are managed by the platform.
- func IsDoubaoVideoAssetChannel(channel *model.Channel) bool {
- return channel != nil && channel.Type == constant.ChannelTypeDoubaoVideo
- }
-
- // CreateDoubaoVideoUserAssetGroup creates a per-user upstream asset group.
- // The Ark API derives the group type internally; passing GroupType is
- // rejected (InvalidParameter), so only the name is sent. The response
- // carries the new group id in Result.Id (Result.GroupId is tolerated too).
- func CreateDoubaoVideoUserAssetGroup(ctx context.Context, userId int, adapter AssetAdapter, channel *model.Channel) (string, *AssetError) {
- spec, ok := ParseAssetAction("CreateAssetGroup")
- if !ok {
- return "", newAssetError(AssetErrorServer, "CreateAssetGroup action is not registered", http.StatusInternalServerError)
- }
- resp, assetErr := adapter.DoAssetRequest(ctx, channel, AssetRequest{
- Action: spec,
- Version: "2024-01-01",
- Body: map[string]any{
- "Name": fmt.Sprintf("new-api-user-%d-channel-%d", userId, channel.Id),
- },
- })
- if assetErr != nil {
- return "", assetErr
- }
- var payload struct {
- Result struct {
- Id string `json:"Id"`
- GroupId string `json:"GroupId"`
- } `json:"Result"`
- }
- if err := common.Unmarshal(resp.Body, &payload); err != nil {
- return "", newAssetError(AssetErrorUpstream, fmt.Sprintf("invalid Volcengine asset group response: %v", err), http.StatusBadGateway)
- }
- groupId := strings.TrimSpace(payload.Result.Id)
- if groupId == "" {
- groupId = strings.TrimSpace(payload.Result.GroupId)
- }
- return groupId, nil
- }
|