feat(file): unify file handling with a new FileSource abstraction for URL and base64 data

This commit is contained in:
CaIon
2026-02-04 17:15:24 +08:00
parent 89b2782675
commit 9ef7740fe7
28 changed files with 1119 additions and 357 deletions

View File

@@ -89,7 +89,8 @@ func GetAllChannels(c *gin.Context) {
if enableTagMode {
tags, err := model.GetPaginatedTags(pageInfo.GetStartIdx(), pageInfo.GetPageSize())
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to get paginated tags: " + err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取标签失败,请稍后重试"})
return
}
for _, tag := range tags {
@@ -136,7 +137,8 @@ func GetAllChannels(c *gin.Context) {
err := baseQuery.Order(order).Limit(pageInfo.GetPageSize()).Offset(pageInfo.GetStartIdx()).Omit("key").Find(&channelData).Error
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to get channels: " + err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取渠道列表失败,请稍后重试"})
return
}
}
@@ -641,7 +643,8 @@ func RefreshCodexChannelCredential(c *gin.Context) {
oauthKey, ch, err := service.RefreshCodexChannelCredential(ctx, channelId, service.CodexCredentialRefreshOptions{ResetCaches: true})
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to refresh codex channel credential: " + err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "刷新凭证失败,请稍后重试"})
return
}
@@ -1315,7 +1318,8 @@ func CopyChannel(c *gin.Context) {
// fetch original channel with key
origin, err := model.GetChannelById(id, true)
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to get channel by id: " + err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取渠道信息失败,请稍后重试"})
return
}
@@ -1333,7 +1337,8 @@ func CopyChannel(c *gin.Context) {
// insert
if err := model.BatchInsertChannels([]model.Channel{clone}); err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to clone channel: " + err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "复制渠道失败,请稍后重试"})
return
}
model.InitChannelCache()

View File

@@ -132,7 +132,8 @@ func completeCodexOAuthWithChannelID(c *gin.Context, channelID int) {
code, state, err := parseCodexAuthorizationInput(req.Input)
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to parse codex authorization input: " + err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "解析授权信息失败,请检查输入格式"})
return
}
if strings.TrimSpace(code) == "" {
@@ -177,7 +178,8 @@ func completeCodexOAuthWithChannelID(c *gin.Context, channelID int) {
tokenRes, err := service.ExchangeCodexAuthorizationCode(ctx, code, verifier)
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to exchange codex authorization code: " + err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "授权码交换失败,请重试"})
return
}

View File

@@ -45,7 +45,8 @@ func GetCodexChannelUsage(c *gin.Context) {
oauthKey, err := codex.ParseOAuthKey(strings.TrimSpace(ch.Key))
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to parse oauth key: " + err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "解析凭证失败,请检查渠道配置"})
return
}
accessToken := strings.TrimSpace(oauthKey.AccessToken)
@@ -70,7 +71,8 @@ func GetCodexChannelUsage(c *gin.Context) {
statusCode, body, err := service.FetchCodexWhamUsage(ctx, client, ch.GetBaseURL(), accessToken, accountID)
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to fetch codex usage: " + err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取用量信息失败,请稍后重试"})
return
}
@@ -99,7 +101,8 @@ func GetCodexChannelUsage(c *gin.Context) {
defer cancel2()
statusCode, body, err = service.FetchCodexWhamUsage(ctx2, client, ch.GetBaseURL(), oauthKey.AccessToken, accountID)
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to fetch codex usage after refresh: " + err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取用量信息失败,请稍后重试"})
return
}
}

View File

@@ -17,7 +17,8 @@ func MigrateConsoleSetting(c *gin.Context) {
// 读取全部 option
opts, err := model.AllOption()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to get all options: " + err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "获取配置失败,请稍后重试"})
return
}
// 建立 map

View File

@@ -272,7 +272,8 @@ func SyncUpstreamModels(c *gin.Context) {
// 1) 获取未配置模型列表
missing, err := model.GetMissingModels()
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to get missing models: " + err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取模型列表失败,请稍后重试"})
return
}

View File

@@ -3,7 +3,6 @@ package controller
import (
"net/http"
"os"
"path/filepath"
"runtime"
"github.com/QuantumNous/new-api/common"
@@ -78,6 +77,9 @@ type PerformanceConfig struct {
// GetPerformanceStats 获取性能统计信息
func GetPerformanceStats(c *gin.Context) {
// 先同步磁盘缓存统计,确保显示准确
common.SyncDiskCacheStats()
// 获取缓存统计
cacheStats := common.GetDiskCacheStats()
@@ -123,11 +125,8 @@ func GetPerformanceStats(c *gin.Context) {
// ClearDiskCache 清理磁盘缓存
func ClearDiskCache(c *gin.Context) {
cachePath := common.GetDiskCachePath()
if cachePath == "" {
cachePath = os.TempDir()
}
dir := filepath.Join(cachePath, "new-api-body-cache")
// 使用统一的缓存目录
dir := common.GetDiskCacheDir()
// 删除缓存目录
err := os.RemoveAll(dir)
@@ -136,8 +135,9 @@ func ClearDiskCache(c *gin.Context) {
return
}
// 重置统计
// 重置统计(包括命中次数和使用量)
common.ResetDiskCacheStats()
common.ResetDiskCacheUsage()
c.JSON(http.StatusOK, gin.H{
"success": true,
@@ -167,11 +167,8 @@ func ForceGC(c *gin.Context) {
// getDiskCacheInfo 获取磁盘缓存目录信息
func getDiskCacheInfo() DiskCacheInfo {
cachePath := common.GetDiskCachePath()
if cachePath == "" {
cachePath = os.TempDir()
}
dir := filepath.Join(cachePath, "new-api-body-cache")
// 使用统一的缓存目录
dir := common.GetDiskCacheDir()
info := DiskCacheInfo{
Path: dir,

View File

@@ -56,7 +56,8 @@ type upstreamResult struct {
func FetchUpstreamRatios(c *gin.Context) {
var req dto.UpstreamRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": err.Error()})
common.SysError("failed to bind upstream request: " + err.Error())
c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "请求参数格式错误"})
return
}

View File

@@ -103,9 +103,10 @@ func AddRedemption(c *gin.Context) {
}
err = cleanRedemption.Insert()
if err != nil {
common.SysError("failed to insert redemption: " + err.Error())
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
"message": "创建兑换码失败,请稍后重试",
"data": keys,
})
return

View File

@@ -107,9 +107,10 @@ func GetTokenUsage(c *gin.Context) {
token, err := model.GetTokenByKey(strings.TrimPrefix(tokenKey, "sk-"), false)
if err != nil {
common.SysError("failed to get token by key: " + err.Error())
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
"message": "获取令牌信息失败,请稍后重试",
})
return
}