فهرست منبع

refactor: restore original logic, make Gemini File API fully isolated

- Restore original auth logic in middleware/auth.go
- Restore original model listing logic in router/relay-router.go
- Restore using official Docker image in docker-compose.yml
- Update documentation to clarify optional configuration
- Gemini File API now uses completely isolated middleware and routes
- Minimal changes to existing codebase (only added SetGeminiFileRouter call)
supeng 1 ماه پیش
والد
کامیت
602545869c
4فایلهای تغییر یافته به همراه15 افزوده شده و 12 حذف شده
  1. 2 4
      docker-compose.yml
  2. 8 1
      docs/GEMINI_FILE_API.md
  3. 3 5
      middleware/auth.go
  4. 2 2
      router/relay-router.go

+ 2 - 4
docker-compose.yml

@@ -16,9 +16,7 @@ version: '3.4' # For compatibility with older Docker versions
 
 services:
   new-api:
-    build:
-      context: .
-      dockerfile: Dockerfile
+    image: calciumion/new-api:latest
     container_name: new-api
     restart: always
     command: --log-dir /app/logs
@@ -34,7 +32,7 @@ services:
       - TZ=Asia/Shanghai
       - ERROR_LOG_ENABLED=true # 是否启用错误日志记录 (Whether to enable error log recording)
       - BATCH_UPDATE_ENABLED=true  # 是否启用批量更新 (Whether to enable batch update)
-      - MAX_REQUEST_BODY_MB=500  # 请求体最大大小(MB),用于支持大文件上传 (Max request body size in MB for large file uploads)
+#      - MAX_REQUEST_BODY_MB=500  # 请求体最大大小(MB),用于支持大文件上传 (Max request body size in MB for large file uploads)
 #      - STREAMING_TIMEOUT=300  # 流模式无响应超时时间,单位秒,默认120秒,如果出现空补全可以尝试改为更大值 (Streaming timeout in seconds, default is 120s. Increase if experiencing empty completions)
 #      - SESSION_SECRET=random_string  # 多机部署时设置,必须修改这个随机字符串!! (multi-node deployment, set this to a random string!!!!!!!)
 #      - SYNC_FREQUENCY=60  # Uncomment if regular database syncing is needed

+ 8 - 1
docs/GEMINI_FILE_API.md

@@ -114,13 +114,20 @@ curl "http://your-api.com/v1beta/files?key=YOUR_API_KEY"
 
 ## 配置
 
-在 `docker-compose.yml` 中配置:
+### 环境变量配置(可选)
+
+如果需要支持大文件上传(超过默认限制),可以在 `docker-compose.yml` 或环境变量中配置:
 
 ```yaml
 environment:
   - MAX_REQUEST_BODY_MB=500  # 支持最大 500MB 文件
 ```
 
+或在启动时设置环境变量:
+```bash
+export MAX_REQUEST_BODY_MB=500
+```
+
 ## 注意事项
 
 1. **文件过期**: 上传的文件会在 48 小时后自动过期

+ 3 - 5
middleware/auth.go

@@ -196,8 +196,8 @@ func TokenAuth() func(c *gin.Context) {
 			}
 			c.Request.Header.Set("Authorization", "Bearer "+key)
 		}
-		// 检查path是 /v1/messages (Claude API)
-		if strings.HasPrefix(c.Request.URL.Path, "/v1/messages") {
+		// 检查path包含/v1/messages 或 /v1/models
+		if strings.Contains(c.Request.URL.Path, "/v1/messages") || strings.Contains(c.Request.URL.Path, "/v1/models") {
 			anthropicKey := c.Request.Header.Get("x-api-key")
 			if anthropicKey != "" {
 				c.Request.Header.Set("Authorization", "Bearer "+anthropicKey)
@@ -205,10 +205,8 @@ func TokenAuth() func(c *gin.Context) {
 		}
 		// gemini api 从query中获取key
 		if strings.HasPrefix(c.Request.URL.Path, "/v1beta/models") ||
-			strings.HasPrefix(c.Request.URL.Path, "/v1beta/files") ||
-			strings.HasPrefix(c.Request.URL.Path, "/upload/v1beta/files") ||
 			strings.HasPrefix(c.Request.URL.Path, "/v1beta/openai/models") ||
-			strings.HasPrefix(c.Request.URL.Path, "/v1/models") {
+			strings.HasPrefix(c.Request.URL.Path, "/v1/models/") {
 			skKey := c.Query("key")
 			if skKey != "" {
 				c.Request.Header.Set("Authorization", "Bearer "+skKey)

+ 2 - 2
router/relay-router.go

@@ -22,8 +22,8 @@ func SetRelayRouter(router *gin.Engine) {
 			switch {
 			case c.GetHeader("x-api-key") != "" && c.GetHeader("anthropic-version") != "":
 				controller.ListModels(c, constant.ChannelTypeAnthropic)
-			case c.GetHeader("x-goog-api-key") != "":
-				controller.ListModels(c, constant.ChannelTypeGemini)
+			case c.GetHeader("x-goog-api-key") != "" || c.Query("key") != "": // 单独的适配
+				controller.RetrieveModel(c, constant.ChannelTypeGemini)
 			default:
 				controller.ListModels(c, constant.ChannelTypeOpenAI)
 			}