From 1ab7d94660a68436641a22bbbf7a47662a990e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AC=E5=8F=B8git?= <240241002@qq.com> Date: Mon, 2 Feb 2026 12:54:43 +0800 Subject: [PATCH] =?UTF-8?q?```=20feat(config):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E9=85=8D=E7=BD=AE=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加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服务 ``` --- config.py | 4 ++++ services/file_service.py | 3 ++- services/generation_service.py | 3 ++- services/task_service.py | 11 ++++++----- utils.py | 11 +++++++++++ 5 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 utils.py diff --git a/config.py b/config.py index f5696c4..1c51c0f 100644 --- a/config.py +++ b/config.py @@ -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=" diff --git a/services/file_service.py b/services/file_service.py index 4dcc91a..20232e7 100644 --- a/services/file_service.py +++ b/services/file_service.py @@ -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 = {} diff --git a/services/generation_service.py b/services/generation_service.py index da5a347..7690e37 100644 --- a/services/generation_service.py +++ b/services/generation_service.py @@ -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) diff --git a/services/task_service.py b/services/task_service.py index 0714eb7..7442e39 100644 --- a/services/task_service.py +++ b/services/task_service.py @@ -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 diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..cf7c2ee --- /dev/null +++ b/utils.py @@ -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