* test(relayconvert): add golden snapshot matrix and relaykit boundary guard Phase 0 of the relaykit extraction plan: pin byte-level output of every registered (from,to) request/response/stream conversion route, and forbid kit-bound packages from growing host-only imports. * wip(relayconvert): drop gin.Context from converter signatures; add convmeta draft Phase 1 in progress: relayconvert now takes context.Context; host media resolver adapts gin.Context back at the service boundary. * refactor(relayconvert): decouple converters from RelayInfo, gin, and settings Phase 1 of the relaykit extraction plan: - converters now depend on convmeta.Meta (implemented by RelayInfo) instead of *relaycommon.RelayInfo; ClaudeConvertInfo and the format guesser move to convmeta with aliases left behind - host settings reach converters via a convmeta.Options snapshot built in RelayInfo.ConvOptions; no more model_setting/reasoning global reads inside the conversion layer - effort-suffix helpers move to service/relayconvert/reasoning (old package forwards); chat-to-responses upgrade policy moves to service (host routing logic, not conversion) - golden conversion matrix unchanged * test(relayconvert): tighten boundary — kit packages now free of gin/setting imports * refactor(dto): drop gin and logger dependencies Phase 2 (part 1): dto.Request.IsStream now takes *http.Request instead of *gin.Context (Gemini's impl reads query/path off the std request); dto's three logger calls become common.SysError. Boundary test allowlist is now empty — kit-bound packages import no gin/setting/logger/model. * refactor(kit): extract dependency-free kitutil; dto/types/relayconvert stop importing common Phase 2 of the relaykit extraction plan: - new service/relayconvert/kitutil holds the pure helpers the kit needs (JSON wrappers, pointer/string/uuid/timestamp utils, MaskSensitiveInfo, pluggable LogInfo/LogError hooks, Debug flag) - dto, types, and all relayconvert packages now use kitutil; their only remaining internal deps are dto/types/constant - common keeps every original symbol (MaskSensitiveInfo delegates to kitutil) so host code is untouched; main.go routes kit logging into common.SysLog/SysError and mirrors DebugEnabled - golden conversion matrix unchanged * refactor(kit): move EndpointType/FinishReason to types; OpenRouter dialect via Options Kit packages (dto/types/relayconvert/reasonmap) no longer import constant: - EndpointType and finish-reason values live in types; constant re-exports - the OpenRouter special-case in claude->openai request conversion reads Options.OpenRouterDialect, set by the host from the channel type; InitChannelMeta invalidates the cached snapshot on channel switch * refactor: extract relaykit submodule (dto/types/relayconvert/reasonmap) Phase 3 of the relaykit extraction plan: - new go module github.com/QuantumNous/new-api/relaykit containing dto (minus task family), types, relayconvert (with convmeta/kitutil/reasoning), and reasonmap; host consumes it via require + replace, go.work for dev - task-family dto (task/suno/midjourney/video) stays in the host dto package; dual-consumer host files alias it as taskdto - relaykit builds and tests standalone (GOWORK=off): no host imports, no gin, no DB, no settings - golden conversion matrix unchanged * build(docker): copy relaykit/go.mod before go mod download The local-replace submodule's go.mod must exist inside the build context for the main module graph to resolve. * fix: address relaykit extraction regressions * fix: address relaykit review regressions * docs: document Meta nil receiver contract * fix(relaykit): fail OpenAI→Claude conversion without max_tokens; reject negative default_max_tokens The Claude Messages API requires max_tokens (omitting it is a 400 "Field required"), but with a nil Options.Claude.DefaultMaxTokens hook the converters silently emitted a request the upstream is guaranteed to reject. Both OpenAI Chat and Responses → Claude conversions now return sharedclaude.ErrMissingMaxTokens when no path (client value, default hook, thinking-adapter floor) supplied one. Unreachable in the host, which always configures the hook. Host side, claude.default_max_tokens now rejects negative values at the option API before persisting — they would wrap into huge unsigned values during conversion. Zero stays allowed: the current API treats max_tokens: 0 as cache pre-warming. * fix: make Gemini safety settings read path race-free
213 lines
5.4 KiB
Go
213 lines
5.4 KiB
Go
package service
|
||
|
||
import (
|
||
"bytes"
|
||
"fmt"
|
||
"image"
|
||
_ "image/gif"
|
||
_ "image/jpeg"
|
||
_ "image/png"
|
||
"io"
|
||
"net/http"
|
||
"strings"
|
||
|
||
"github.com/QuantumNous/new-api/common"
|
||
"github.com/QuantumNous/new-api/logger"
|
||
"github.com/QuantumNous/new-api/relaykit/types"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// GetFileTypeFromUrl 获取文件类型,返回 mime type, 例如 image/jpeg, image/png, image/gif, image/bmp, image/tiff, application/pdf
|
||
// 如果获取失败,返回 application/octet-stream
|
||
func GetFileTypeFromUrl(c *gin.Context, url string, reason ...string) (string, error) {
|
||
response, err := DoDownloadRequest(url, []string{"get_mime_type", strings.Join(reason, ", ")}...)
|
||
if err != nil {
|
||
common.SysLog(fmt.Sprintf("fail to get file type from url: %s, error: %s", url, err.Error()))
|
||
return "", err
|
||
}
|
||
defer response.Body.Close()
|
||
|
||
if response.StatusCode != 200 {
|
||
logger.LogError(c, fmt.Sprintf("failed to download file from %s, status code: %d", url, response.StatusCode))
|
||
return "", fmt.Errorf("failed to download file, status code: %d", response.StatusCode)
|
||
}
|
||
|
||
if headerType := strings.TrimSpace(response.Header.Get("Content-Type")); headerType != "" {
|
||
if i := strings.Index(headerType, ";"); i != -1 {
|
||
headerType = headerType[:i]
|
||
}
|
||
if headerType != "application/octet-stream" {
|
||
return headerType, nil
|
||
}
|
||
}
|
||
|
||
if cd := response.Header.Get("Content-Disposition"); cd != "" {
|
||
parts := strings.Split(cd, ";")
|
||
for _, part := range parts {
|
||
part = strings.TrimSpace(part)
|
||
if strings.HasPrefix(strings.ToLower(part), "filename=") {
|
||
name := strings.TrimSpace(strings.TrimPrefix(part, "filename="))
|
||
if len(name) > 2 && name[0] == '"' && name[len(name)-1] == '"' {
|
||
name = name[1 : len(name)-1]
|
||
}
|
||
if dot := strings.LastIndex(name, "."); dot != -1 && dot+1 < len(name) {
|
||
ext := strings.ToLower(name[dot+1:])
|
||
if ext != "" {
|
||
mt := GetMimeTypeByExtension(ext)
|
||
if mt != "application/octet-stream" {
|
||
return mt, nil
|
||
}
|
||
}
|
||
}
|
||
break
|
||
}
|
||
}
|
||
}
|
||
|
||
cleanedURL := url
|
||
if q := strings.Index(cleanedURL, "?"); q != -1 {
|
||
cleanedURL = cleanedURL[:q]
|
||
}
|
||
if slash := strings.LastIndex(cleanedURL, "/"); slash != -1 && slash+1 < len(cleanedURL) {
|
||
last := cleanedURL[slash+1:]
|
||
if dot := strings.LastIndex(last, "."); dot != -1 && dot+1 < len(last) {
|
||
ext := strings.ToLower(last[dot+1:])
|
||
if ext != "" {
|
||
mt := GetMimeTypeByExtension(ext)
|
||
if mt != "application/octet-stream" {
|
||
return mt, nil
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
var readData []byte
|
||
limits := []int{512, 8 * 1024, 24 * 1024, 64 * 1024}
|
||
for _, limit := range limits {
|
||
logger.LogDebug(c, "Trying to read %d bytes to determine file type", limit)
|
||
if len(readData) < limit {
|
||
need := limit - len(readData)
|
||
tmp := make([]byte, need)
|
||
n, _ := io.ReadFull(response.Body, tmp)
|
||
if n > 0 {
|
||
readData = append(readData, tmp[:n]...)
|
||
}
|
||
}
|
||
|
||
if len(readData) == 0 {
|
||
continue
|
||
}
|
||
|
||
sniffed := http.DetectContentType(readData)
|
||
if sniffed != "" && sniffed != "application/octet-stream" {
|
||
return sniffed, nil
|
||
}
|
||
|
||
// Try HEIF/HEIC detection (Go standard library doesn't recognize it)
|
||
if heifMime := detectHEIF(readData); heifMime != "" {
|
||
return heifMime, nil
|
||
}
|
||
|
||
if _, format, err := image.DecodeConfig(bytes.NewReader(readData)); err == nil {
|
||
switch strings.ToLower(format) {
|
||
case "jpeg", "jpg":
|
||
return "image/jpeg", nil
|
||
case "png":
|
||
return "image/png", nil
|
||
case "gif":
|
||
return "image/gif", nil
|
||
case "bmp":
|
||
return "image/bmp", nil
|
||
case "tiff":
|
||
return "image/tiff", nil
|
||
default:
|
||
if format != "" {
|
||
return "image/" + strings.ToLower(format), nil
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// Fallback
|
||
return "application/octet-stream", nil
|
||
}
|
||
|
||
// GetFileBase64FromUrl 从 URL 获取文件的 base64 编码数据
|
||
// Deprecated: 请使用 GetBase64Data 配合 types.NewURLFileSource 替代
|
||
// 此函数保留用于向后兼容,内部已重构为调用统一的文件服务
|
||
func GetFileBase64FromUrl(c *gin.Context, url string, reason ...string) (*types.LocalFileData, error) {
|
||
source := types.NewURLFileSource(url)
|
||
cachedData, err := LoadFileSource(c, source, reason...)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 转换为旧的 LocalFileData 格式以保持兼容
|
||
base64Data, err := cachedData.GetBase64Data()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &types.LocalFileData{
|
||
Base64Data: base64Data,
|
||
MimeType: cachedData.MimeType,
|
||
Size: cachedData.Size,
|
||
Url: url,
|
||
}, nil
|
||
}
|
||
|
||
func GetMimeTypeByExtension(ext string) string {
|
||
// Convert to lowercase for case-insensitive comparison
|
||
ext = strings.ToLower(ext)
|
||
switch ext {
|
||
// Text files
|
||
case "txt", "md", "markdown", "csv", "json", "xml", "html", "htm":
|
||
return "text/plain"
|
||
|
||
// Image files
|
||
case "jpg", "jpeg":
|
||
return "image/jpeg"
|
||
case "png":
|
||
return "image/png"
|
||
case "gif":
|
||
return "image/gif"
|
||
case "jfif":
|
||
return "image/jpeg"
|
||
case "heic":
|
||
return "image/heic"
|
||
case "heif":
|
||
return "image/heif"
|
||
|
||
// Audio files
|
||
case "mp3":
|
||
return "audio/mp3"
|
||
case "wav":
|
||
return "audio/wav"
|
||
case "mpeg":
|
||
return "audio/mpeg"
|
||
|
||
// Video files
|
||
case "mp4":
|
||
return "video/mp4"
|
||
case "wmv":
|
||
return "video/wmv"
|
||
case "flv":
|
||
return "video/flv"
|
||
case "mov":
|
||
return "video/mov"
|
||
case "mpg":
|
||
return "video/mpg"
|
||
case "avi":
|
||
return "video/avi"
|
||
case "mpegps":
|
||
return "video/mpegps"
|
||
|
||
// Document files
|
||
case "pdf":
|
||
return "application/pdf"
|
||
|
||
default:
|
||
return "application/octet-stream" // Default for unknown types
|
||
}
|
||
}
|