58 lines
1.8 KiB
Go
58 lines
1.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func ensureCompositeTargetPlatform(c *gin.Context, apiKey *service.APIKey, model string) {
|
|
if c == nil || c.Request == nil || apiKey == nil || apiKey.Group == nil || apiKey.Group.Platform != service.PlatformComposite {
|
|
return
|
|
}
|
|
if _, ok := service.ResolvedTargetPlatformFromContext(c.Request.Context()); ok {
|
|
return
|
|
}
|
|
if platform, ok := service.DetectModelPlatform(model); ok {
|
|
c.Request = c.Request.WithContext(service.WithResolvedTargetPlatform(c.Request.Context(), platform))
|
|
}
|
|
}
|
|
|
|
func compositeTargetPlatformAllowed(c *gin.Context, apiKey *service.APIKey, model string, allowed ...string) bool {
|
|
if c == nil || c.Request == nil || apiKey == nil || apiKey.Group == nil || apiKey.Group.Platform != service.PlatformComposite {
|
|
return true
|
|
}
|
|
ensureCompositeTargetPlatform(c, apiKey, model)
|
|
platform, ok := service.ResolvedTargetPlatformFromContext(c.Request.Context())
|
|
if !ok {
|
|
return false
|
|
}
|
|
for _, allowedPlatform := range allowed {
|
|
if platform == allowedPlatform {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func compositeTargetPlatformResolved(c *gin.Context, apiKey *service.APIKey, model string) bool {
|
|
if c == nil || c.Request == nil || apiKey == nil || apiKey.Group == nil || apiKey.Group.Platform != service.PlatformComposite {
|
|
return true
|
|
}
|
|
ensureCompositeTargetPlatform(c, apiKey, model)
|
|
_, ok := service.ResolvedTargetPlatformFromContext(c.Request.Context())
|
|
return ok
|
|
}
|
|
|
|
func effectiveAPIKeyPlatform(c *gin.Context, apiKey *service.APIKey) string {
|
|
if c != nil && c.Request != nil {
|
|
if platform, ok := service.ResolvedTargetPlatformFromContext(c.Request.Context()); ok {
|
|
return platform
|
|
}
|
|
}
|
|
if apiKey == nil || apiKey.Group == nil {
|
|
return ""
|
|
}
|
|
return apiKey.Group.Platform
|
|
}
|