Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

54 linhas
1.8 KiB

  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/QuantumNous/new-api/common"
  8. "github.com/QuantumNous/new-api/constant"
  9. "github.com/QuantumNous/new-api/model"
  10. )
  11. // IsDoubaoVideoAssetChannel reports whether the channel is an official
  12. // Volcengine (DoubaoVideo) channel whose assets are managed by the platform.
  13. func IsDoubaoVideoAssetChannel(channel *model.Channel) bool {
  14. return channel != nil && channel.Type == constant.ChannelTypeDoubaoVideo
  15. }
  16. // CreateDoubaoVideoUserAssetGroup creates a per-user upstream asset group.
  17. // The Ark API derives the group type internally; passing GroupType is
  18. // rejected (InvalidParameter), so only the name is sent. The response
  19. // carries the new group id in Result.Id (Result.GroupId is tolerated too).
  20. func CreateDoubaoVideoUserAssetGroup(ctx context.Context, userId int, adapter AssetAdapter, channel *model.Channel) (string, *AssetError) {
  21. spec, ok := ParseAssetAction("CreateAssetGroup")
  22. if !ok {
  23. return "", newAssetError(AssetErrorServer, "CreateAssetGroup action is not registered", http.StatusInternalServerError)
  24. }
  25. resp, assetErr := adapter.DoAssetRequest(ctx, channel, AssetRequest{
  26. Action: spec,
  27. Version: "2024-01-01",
  28. Body: map[string]any{
  29. "Name": fmt.Sprintf("new-api-user-%d-channel-%d", userId, channel.Id),
  30. },
  31. })
  32. if assetErr != nil {
  33. return "", assetErr
  34. }
  35. var payload struct {
  36. Result struct {
  37. Id string `json:"Id"`
  38. GroupId string `json:"GroupId"`
  39. } `json:"Result"`
  40. }
  41. if err := common.Unmarshal(resp.Body, &payload); err != nil {
  42. return "", newAssetError(AssetErrorUpstream, fmt.Sprintf("invalid Volcengine asset group response: %v", err), http.StatusBadGateway)
  43. }
  44. groupId := strings.TrimSpace(payload.Result.Id)
  45. if groupId == "" {
  46. groupId = strings.TrimSpace(payload.Result.GroupId)
  47. }
  48. return groupId, nil
  49. }