Spaces:
Paused
Paused
Delete src/utils.py
Browse files- src/utils.py +0 -230
src/utils.py
DELETED
|
@@ -1,230 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import uuid
|
| 3 |
-
import aiohttp
|
| 4 |
-
import aiofiles
|
| 5 |
-
import logging
|
| 6 |
-
import ssl
|
| 7 |
-
from urllib.parse import urlparse
|
| 8 |
-
from .config import Config
|
| 9 |
-
|
| 10 |
-
# 导入上传模块
|
| 11 |
-
try:
|
| 12 |
-
from .image_uploader import upload_image
|
| 13 |
-
UPLOADER_AVAILABLE = True
|
| 14 |
-
except ImportError:
|
| 15 |
-
UPLOADER_AVAILABLE = False
|
| 16 |
-
|
| 17 |
-
# 初始化日志
|
| 18 |
-
logger = logging.getLogger("sora-api.utils")
|
| 19 |
-
|
| 20 |
-
# 图片本地化调试开关
|
| 21 |
-
IMAGE_DEBUG = os.getenv("IMAGE_DEBUG", "").lower() in ("true", "1", "yes")
|
| 22 |
-
|
| 23 |
-
# 修复Python 3.11之前版本中HTTPS代理处理HTTPS请求的问题
|
| 24 |
-
# 参考: https://docs.aiohttp.org/en/stable/client_advanced.html#proxy-support
|
| 25 |
-
try:
|
| 26 |
-
import aiohttp.connector
|
| 27 |
-
orig_create_connection = aiohttp.connector.TCPConnector._create_connection
|
| 28 |
-
|
| 29 |
-
async def patched_create_connection(self, req, traces, timeout):
|
| 30 |
-
if req.ssl and req.proxy and req.proxy.scheme == 'https':
|
| 31 |
-
# 为代理连接创建SSL上下文
|
| 32 |
-
proxy_ssl = ssl.create_default_context()
|
| 33 |
-
req.proxy_ssl = proxy_ssl
|
| 34 |
-
|
| 35 |
-
if IMAGE_DEBUG:
|
| 36 |
-
logger.debug("已应用HTTPS代理补丁")
|
| 37 |
-
|
| 38 |
-
return await orig_create_connection(self, req, traces, timeout)
|
| 39 |
-
|
| 40 |
-
# 应用猴子补丁
|
| 41 |
-
aiohttp.connector.TCPConnector._create_connection = patched_create_connection
|
| 42 |
-
|
| 43 |
-
if IMAGE_DEBUG:
|
| 44 |
-
logger.debug("已启用aiohttp HTTPS代理支持补丁")
|
| 45 |
-
except Exception as e:
|
| 46 |
-
logger.warning(f"应用HTTPS代理补丁失败: {e}")
|
| 47 |
-
|
| 48 |
-
async def download_and_save_image(image_url: str) -> str:
|
| 49 |
-
"""
|
| 50 |
-
下载图片并保存到本地
|
| 51 |
-
|
| 52 |
-
Args:
|
| 53 |
-
image_url: 图片URL
|
| 54 |
-
|
| 55 |
-
Returns:
|
| 56 |
-
本地化后的图片URL
|
| 57 |
-
"""
|
| 58 |
-
# 如果未启用本地化或URL已经是本地路径,直接返回
|
| 59 |
-
if not Config.IMAGE_LOCALIZATION:
|
| 60 |
-
if IMAGE_DEBUG:
|
| 61 |
-
logger.debug(f"图片本地化未启用,返回原始URL: {image_url}")
|
| 62 |
-
return image_url
|
| 63 |
-
|
| 64 |
-
# 检查是否已经是本地URL
|
| 65 |
-
# 准备URL检测所需的信息
|
| 66 |
-
parsed_base_url = urlparse(Config.BASE_URL)
|
| 67 |
-
base_path = parsed_base_url.path.rstrip('/')
|
| 68 |
-
|
| 69 |
-
# 直接检查常见的图片URL模式
|
| 70 |
-
local_url_patterns = [
|
| 71 |
-
"/images/",
|
| 72 |
-
"/static/images/",
|
| 73 |
-
f"{base_path}/images/",
|
| 74 |
-
f"{base_path}/static/images/"
|
| 75 |
-
]
|
| 76 |
-
|
| 77 |
-
# 检查是否有自定义前缀
|
| 78 |
-
if Config.STATIC_PATH_PREFIX:
|
| 79 |
-
prefix = Config.STATIC_PATH_PREFIX
|
| 80 |
-
if not prefix.startswith('/'):
|
| 81 |
-
prefix = f"/{prefix}"
|
| 82 |
-
local_url_patterns.append(f"{prefix}/images/")
|
| 83 |
-
local_url_patterns.append(f"{base_path}{prefix}/images/")
|
| 84 |
-
|
| 85 |
-
# 检查是否符合任何本地URL模式
|
| 86 |
-
is_local_url = any(pattern in image_url for pattern in local_url_patterns)
|
| 87 |
-
|
| 88 |
-
if is_local_url:
|
| 89 |
-
if IMAGE_DEBUG:
|
| 90 |
-
logger.debug(f"URL已是本地图片路径: {image_url}")
|
| 91 |
-
|
| 92 |
-
# 如果是相对路径,补充完整的URL
|
| 93 |
-
if image_url.startswith("/"):
|
| 94 |
-
return f"{parsed_base_url.scheme}://{parsed_base_url.netloc}{image_url}"
|
| 95 |
-
return image_url
|
| 96 |
-
|
| 97 |
-
try:
|
| 98 |
-
# 生成文件名和保存路径
|
| 99 |
-
parsed_url = urlparse(image_url)
|
| 100 |
-
file_extension = os.path.splitext(parsed_url.path)[1] or ".png"
|
| 101 |
-
filename = f"{uuid.uuid4()}{file_extension}"
|
| 102 |
-
save_path = os.path.join(Config.IMAGE_SAVE_DIR, filename)
|
| 103 |
-
|
| 104 |
-
# 确保保存目录存在
|
| 105 |
-
os.makedirs(Config.IMAGE_SAVE_DIR, exist_ok=True)
|
| 106 |
-
|
| 107 |
-
if IMAGE_DEBUG:
|
| 108 |
-
logger.debug(f"下载图片: {image_url} -> {save_path}")
|
| 109 |
-
|
| 110 |
-
# 配置代理
|
| 111 |
-
proxy = None
|
| 112 |
-
if Config.PROXY_HOST and Config.PROXY_PORT:
|
| 113 |
-
proxy_auth = None
|
| 114 |
-
if Config.PROXY_USER and Config.PROXY_PASS:
|
| 115 |
-
proxy_auth = aiohttp.BasicAuth(Config.PROXY_USER, Config.PROXY_PASS)
|
| 116 |
-
|
| 117 |
-
proxy_url = f"http://{Config.PROXY_HOST}:{Config.PROXY_PORT}"
|
| 118 |
-
if IMAGE_DEBUG:
|
| 119 |
-
auth_info = f" (使用认证)" if proxy_auth else ""
|
| 120 |
-
logger.debug(f"使用代理: {proxy_url}{auth_info}")
|
| 121 |
-
proxy = proxy_url
|
| 122 |
-
|
| 123 |
-
# 下载图片
|
| 124 |
-
timeout = aiohttp.ClientTimeout(total=60)
|
| 125 |
-
async with aiohttp.ClientSession(timeout=timeout) as session:
|
| 126 |
-
# 创建请求参数
|
| 127 |
-
request_kwargs = {"timeout": 30}
|
| 128 |
-
if proxy:
|
| 129 |
-
request_kwargs["proxy"] = proxy
|
| 130 |
-
if Config.PROXY_USER and Config.PROXY_PASS:
|
| 131 |
-
request_kwargs["proxy_auth"] = aiohttp.BasicAuth(Config.PROXY_USER, Config.PROXY_PASS)
|
| 132 |
-
|
| 133 |
-
async with session.get(image_url, **request_kwargs) as response:
|
| 134 |
-
if response.status != 200:
|
| 135 |
-
logger.warning(f"下载失败,状态码: {response.status}, URL: {image_url}")
|
| 136 |
-
return image_url
|
| 137 |
-
|
| 138 |
-
content = await response.read()
|
| 139 |
-
if not content:
|
| 140 |
-
logger.warning("下载内容为空")
|
| 141 |
-
return image_url
|
| 142 |
-
|
| 143 |
-
# 保存图片
|
| 144 |
-
async with aiofiles.open(save_path, "wb") as f:
|
| 145 |
-
await f.write(content)
|
| 146 |
-
|
| 147 |
-
# 检查文件是否成功保存
|
| 148 |
-
if not os.path.exists(save_path) or os.path.getsize(save_path) == 0:
|
| 149 |
-
logger.warning(f"图片保存失败: {save_path}")
|
| 150 |
-
if os.path.exists(save_path):
|
| 151 |
-
os.remove(save_path)
|
| 152 |
-
return image_url
|
| 153 |
-
|
| 154 |
-
# 尝试上传到PicGo或WebDAV
|
| 155 |
-
external_url = None
|
| 156 |
-
if UPLOADER_AVAILABLE:
|
| 157 |
-
try:
|
| 158 |
-
success, url = await upload_image(save_path)
|
| 159 |
-
if success and url:
|
| 160 |
-
logger.info(f"图片已上传到外部服务: {url}")
|
| 161 |
-
external_url = url
|
| 162 |
-
except Exception as e:
|
| 163 |
-
logger.error(f"上传图片到外部服务时出错: {str(e)}")
|
| 164 |
-
|
| 165 |
-
# 如果成功上传到外部服务,返回外部URL
|
| 166 |
-
if external_url:
|
| 167 |
-
return external_url
|
| 168 |
-
|
| 169 |
-
# 返回本地URL
|
| 170 |
-
# 获取文件名
|
| 171 |
-
filename = os.path.basename(save_path)
|
| 172 |
-
|
| 173 |
-
# 解析基础URL
|
| 174 |
-
parsed_base_url = urlparse(Config.BASE_URL)
|
| 175 |
-
base_path = parsed_base_url.path.rstrip('/')
|
| 176 |
-
|
| 177 |
-
# 使用固定的图片URL格式
|
| 178 |
-
relative_url = f"/images/{filename}"
|
| 179 |
-
|
| 180 |
-
# 如果设置了STATIC_PATH_PREFIX,优先使用该前缀
|
| 181 |
-
if Config.STATIC_PATH_PREFIX:
|
| 182 |
-
prefix = Config.STATIC_PATH_PREFIX
|
| 183 |
-
if not prefix.startswith('/'):
|
| 184 |
-
prefix = f"/{prefix}"
|
| 185 |
-
relative_url = f"{prefix}/images/{filename}"
|
| 186 |
-
|
| 187 |
-
# 如果BASE_URL有子路径,添加到相对路径前
|
| 188 |
-
if base_path:
|
| 189 |
-
relative_url = f"{base_path}{relative_url}"
|
| 190 |
-
|
| 191 |
-
# 处理重复斜杠
|
| 192 |
-
while "//" in relative_url:
|
| 193 |
-
relative_url = relative_url.replace("//", "/")
|
| 194 |
-
|
| 195 |
-
# 生成完整URL
|
| 196 |
-
full_url = f"{parsed_base_url.scheme}://{parsed_base_url.netloc}{relative_url}"
|
| 197 |
-
|
| 198 |
-
if IMAGE_DEBUG:
|
| 199 |
-
logger.debug(f"图片保存成功: {full_url}")
|
| 200 |
-
logger.debug(f"图片保存路径: {save_path}")
|
| 201 |
-
|
| 202 |
-
return full_url
|
| 203 |
-
except Exception as e:
|
| 204 |
-
logger.error(f"图片下载失败: {str(e)}", exc_info=IMAGE_DEBUG)
|
| 205 |
-
return image_url
|
| 206 |
-
|
| 207 |
-
async def localize_image_urls(image_urls: list) -> list:
|
| 208 |
-
"""
|
| 209 |
-
批量将图片URL本地化
|
| 210 |
-
|
| 211 |
-
Args:
|
| 212 |
-
image_urls: 图片URL列表
|
| 213 |
-
|
| 214 |
-
Returns:
|
| 215 |
-
本地化后的URL列表
|
| 216 |
-
"""
|
| 217 |
-
if not Config.IMAGE_LOCALIZATION or not image_urls:
|
| 218 |
-
return image_urls
|
| 219 |
-
|
| 220 |
-
if IMAGE_DEBUG:
|
| 221 |
-
logger.debug(f"本地化 {len(image_urls)} 个URL: {image_urls}")
|
| 222 |
-
else:
|
| 223 |
-
logger.info(f"本地化 {len(image_urls)} 个图片")
|
| 224 |
-
|
| 225 |
-
localized_urls = []
|
| 226 |
-
for url in image_urls:
|
| 227 |
-
localized_url = await download_and_save_image(url)
|
| 228 |
-
localized_urls.append(localized_url)
|
| 229 |
-
|
| 230 |
-
return localized_urls
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|