Compare commits

...

2 Commits

Author SHA1 Message Date
1b7fcd603c Merge branch 'main' of http://331002.xyz:8418/240241002/ai_v 2026-02-02 12:54:46 +08:00
1ab7d94660 ```
feat(config): 添加代理配置选项

添加USE_PROXY和PROXY_URL配置项以支持请求代理功能

feat(utils): 新增代理URL获取工具函数

引入get_proxied_url工具函数用于处理代理请求

refactor(services): 将所有外部API请求通过代理转发

修改file_service、generation_service和task_service中的requests.get/post调用,
统一使用get_proxied_url包装URL以支持代理访问外部API服务
```
2026-02-02 12:54:43 +08:00
5 changed files with 25 additions and 7 deletions

View File

@ -66,3 +66,7 @@ MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlDx4KdOtOQE+tBq6jHKKFenRaRe2gbBnleBk
# 开发模式配置
DEV_MODE = False # True=开发模式固定验证码False=生产模式(真实短信)
DEV_SMS_CODE = "888888" # 开发模式下的固定验证码
# Proxy Config
USE_PROXY = True # 是否开启代理
PROXY_URL = "https://nas.4x4g.com:10011/api/tool/proxy/?url="

View File

@ -6,6 +6,7 @@ from urllib.parse import quote
from werkzeug.utils import secure_filename
from config import Config
from extensions import s3_client
from utils import get_proxied_url
def handle_file_uploads(files):
"""处理文件上传到 MinIO"""
@ -22,7 +23,7 @@ def handle_file_uploads(files):
def get_remote_file_stream(url):
"""获取远程文件的流"""
req = requests.get(url, stream=True, timeout=60)
req = requests.get(get_proxied_url(url), stream=True, timeout=60)
req.raise_for_status()
headers = {}

View File

@ -8,6 +8,7 @@ import json
import uuid
import threading
from flask import current_app
from utils import get_proxied_url
def get_model_cost(model_value, is_video=False):
"""获取模型消耗积分"""
@ -88,7 +89,7 @@ def handle_chat_generation_sync(user_id, api_key, model_value, prompt, use_trial
"messages": [{"role": "user", "content": prompt}]
}
try:
resp = requests.post(Config.CHAT_API, json=chat_payload, headers=headers, timeout=120)
resp = requests.post(get_proxied_url(Config.CHAT_API), json=chat_payload, headers=headers, timeout=120)
if resp.status_code != 200:
if use_trial:
refund_points(user_id, cost)

View File

@ -13,6 +13,7 @@ from extensions import s3_client, redis_client, db
from models import GenerationRecord, User
from config import Config
from services.logger import system_logger
from utils import get_proxied_url
def sync_images_background(app, record_id, raw_urls):
"""后台同步图片至 MinIO并生成缩略图带重试机制"""
@ -22,7 +23,7 @@ def sync_images_background(app, record_id, raw_urls):
success = False
for attempt in range(3): # 3 次重试机制
try:
img_resp = requests.get(raw_url, timeout=30)
img_resp = requests.get(get_proxied_url(raw_url), timeout=30)
if img_resp.status_code == 200:
content = img_resp.content
ext = ".png"
@ -98,7 +99,7 @@ def process_image_generation(app, user_id, task_id, payload, api_key, target_api
try:
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
# 使用较长的超时时间 (10分钟),确保长耗时任务不被中断
resp = requests.post(target_api, json=payload, headers=headers, timeout=1000)
resp = requests.post(get_proxied_url(target_api), json=payload, headers=headers, timeout=1000)
if resp.status_code != 200:
if use_trial:
@ -151,7 +152,7 @@ def sync_video_background(app, record_id, raw_url, internal_task_id=None):
for attempt in range(3):
try:
# 增加了流式下载,处理大视频文件
with requests.get(raw_url, stream=True, timeout=120) as r:
with requests.get(get_proxied_url(raw_url), stream=True, timeout=120) as r:
r.raise_for_status()
content_type = r.headers.get('content-type', 'video/mp4')
ext = ".mp4"
@ -209,7 +210,7 @@ def process_video_generation(app, user_id, internal_task_id, payload, api_key, c
try:
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
# 1. 提交任务
submit_resp = requests.post(Config.VIDEO_GEN_API, json=payload, headers=headers, timeout=60)
submit_resp = requests.post(get_proxied_url(Config.VIDEO_GEN_API), json=payload, headers=headers, timeout=60)
if submit_resp.status_code != 200:
raise Exception(f"视频任务提交失败: {submit_resp.text}")
@ -233,7 +234,7 @@ def process_video_generation(app, user_id, internal_task_id, payload, api_key, c
time.sleep(10)
poll_url = Config.VIDEO_POLL_API.format(task_id=remote_task_id)
poll_resp = requests.get(poll_url, headers=headers, timeout=30)
poll_resp = requests.get(get_proxied_url(poll_url), headers=headers, timeout=30)
if poll_resp.status_code != 200:
continue

11
utils.py Normal file
View File

@ -0,0 +1,11 @@
from urllib.parse import quote
from config import Config
def get_proxied_url(url):
"""
如果启用了代理则返回经过代理包装的 URL
否则返回原 URL
"""
if Config.USE_PROXY and url:
return f"{Config.PROXY_URL}{quote(url)}"
return url