mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-05-11 18:09:55 +08:00
feat: 增加配置页面的picgo 自定义url, 处理自定义picgo的返回结果
This commit is contained in:
@@ -214,6 +214,7 @@ app/
|
||||
| `UPLOAD_PROVIDER` | Image upload provider: `smms`, `picgo`, `cloudflare_imgbed` | `smms` |
|
||||
| `SMMS_SECRET_TOKEN` | SM.MS API Token | `your-smms-token` |
|
||||
| `PICGO_API_KEY` | PicoGo API Key | `your-picogo-apikey` |
|
||||
| `PICGO_API_URL` | PicoGo API Server URL | `https://www.picgo.net/api/1/upload` |
|
||||
| `CLOUDFLARE_IMGBED_URL` | CloudFlare ImgBed upload URL | `https://xxxxxxx.pages.dev/upload` |
|
||||
| `CLOUDFLARE_IMGBED_AUTH_CODE`| CloudFlare ImgBed auth key | `your-cloudflare-imgber-auth-code` |
|
||||
| `CLOUDFLARE_IMGBED_UPLOAD_FOLDER`| CloudFlare ImgBed upload folder | `""` |
|
||||
|
||||
@@ -214,6 +214,7 @@ app/
|
||||
| `UPLOAD_PROVIDER` | 图片上传提供商: `smms`, `picgo`, `cloudflare_imgbed` | `smms` |
|
||||
| `SMMS_SECRET_TOKEN` | SM.MS图床的API Token | `your-smms-token` |
|
||||
| `PICGO_API_KEY` | [PicoGo](https://www.picgo.net/)图床的API Key | `your-picogo-apikey` |
|
||||
| `PICGO_API_URL` | [PicoGo](https://www.picgo.net/)图床的API服务器地址 | `https://www.picgo.net/api/1/upload` |
|
||||
| `CLOUDFLARE_IMGBED_URL` | [CloudFlare](https://github.com/MarSeventh/CloudFlare-ImgBed) 图床上传地址 | `https://xxxxxxx.pages.dev/upload` |
|
||||
| `CLOUDFLARE_IMGBED_AUTH_CODE`| CloudFlare图床的鉴权key | `your-cloudflare-imgber-auth-code` |
|
||||
| `CLOUDFLARE_IMGBED_UPLOAD_FOLDER`| CloudFlare图床的上传文件夹路径 | `""` |
|
||||
|
||||
@@ -115,6 +115,7 @@ class ImageCreateService:
|
||||
image_uploader = ImageUploaderFactory.create(
|
||||
provider=settings.UPLOAD_PROVIDER,
|
||||
api_key=settings.PICGO_API_KEY,
|
||||
api_url=settings.PICGO_API_URL,
|
||||
)
|
||||
elif settings.UPLOAD_PROVIDER == "cloudflare_imgbed":
|
||||
image_uploader = ImageUploaderFactory.create(
|
||||
|
||||
@@ -1835,6 +1835,23 @@ endblock %} {% block head_extra_styles %}
|
||||
<small class="text-gray-500 mt-1 block">SM.MS图床的密钥</small>
|
||||
</div>
|
||||
|
||||
<!-- PicGo API URL -->
|
||||
<div class="mb-6 provider-config" data-provider="picgo">
|
||||
<label
|
||||
for="PICGO_API_URL"
|
||||
class="block font-semibold mb-2 text-gray-700"
|
||||
>PicGo API地址</label
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
id="PICGO_API_URL"
|
||||
name="PICGO_API_URL"
|
||||
placeholder="https://www.picgo.net/api/1/upload"
|
||||
class="w-full px-4 py-3 rounded-lg form-input-themed"
|
||||
/>
|
||||
<small class="text-gray-500 mt-1 block">PicGo服务器的API地址,默认为 https://www.picgo.net/api/1/upload</small>
|
||||
</div>
|
||||
|
||||
<!-- PicGo API密钥 -->
|
||||
<div class="mb-6 provider-config" data-provider="picgo">
|
||||
<label
|
||||
|
||||
@@ -179,9 +179,22 @@ class PicGoUploader(ImageUploader):
|
||||
"""
|
||||
try:
|
||||
# 准备请求头
|
||||
headers = {
|
||||
"X-API-Key": self.api_key
|
||||
}
|
||||
headers = {}
|
||||
|
||||
# 构建请求URL
|
||||
request_url = self.api_url
|
||||
|
||||
# 判断是否为默认PicGo URL,如果是则使用header认证,否则使用URL参数认证
|
||||
if self.api_url == "https://www.picgo.net/api/1/upload":
|
||||
headers["X-API-Key"] = self.api_key
|
||||
else:
|
||||
# 对于自定义URL,将API key作为查询参数添加到URL中
|
||||
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode
|
||||
parsed_url = urlparse(request_url)
|
||||
query_params = parse_qs(parsed_url.query)
|
||||
query_params["key"] = self.api_key
|
||||
new_query = urlencode(query_params, doseq=True)
|
||||
request_url = urlunparse(parsed_url._replace(query=new_query))
|
||||
|
||||
# 准备文件数据
|
||||
files = {
|
||||
@@ -190,7 +203,7 @@ class PicGoUploader(ImageUploader):
|
||||
|
||||
# 发送请求
|
||||
response = requests.post(
|
||||
self.api_url,
|
||||
request_url,
|
||||
headers=headers,
|
||||
files=files
|
||||
)
|
||||
@@ -201,6 +214,34 @@ class PicGoUploader(ImageUploader):
|
||||
# 解析响应
|
||||
result = response.json()
|
||||
|
||||
# 处理自定义PicGo服务器的响应格式
|
||||
if "success" in result and "result" in result:
|
||||
# 自定义PicGo服务器格式: {"success": true, "result": ["url"]}
|
||||
if result["success"]:
|
||||
image_url = result["result"][0] if result["result"] and len(result["result"]) > 0 else ""
|
||||
image_metadata = ImageMetadata(
|
||||
width=0,
|
||||
height=0,
|
||||
filename=filename,
|
||||
size=0,
|
||||
url=image_url,
|
||||
delete_url=None
|
||||
)
|
||||
return UploadResponse(
|
||||
success=True,
|
||||
code="success",
|
||||
message="Upload success",
|
||||
data=image_metadata
|
||||
)
|
||||
else:
|
||||
raise UploadError(
|
||||
message="Upload failed",
|
||||
error_type=UploadErrorType.SERVER_ERROR,
|
||||
status_code=400,
|
||||
details=result
|
||||
)
|
||||
|
||||
# 处理官方PicGo服务器的响应格式
|
||||
# 验证上传是否成功
|
||||
if result.get("status_code") != 200:
|
||||
error_message = "Upload failed"
|
||||
|
||||
Reference in New Issue
Block a user