阿里图床测试
This commit is contained in:
BlueSkyXN 2024-10-14 09:57:07 +08:00
parent 9f3a0c5676
commit 78fc32eed6
5 changed files with 210 additions and 57 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
python-uploader/log.txt
python-uploader/ck-ali.txt

View File

@ -72,6 +72,7 @@
<select class="form-control" id="apiSelect">
<option value="ipfs">IPFS-去中心化多网关兼容</option>
<option value="58img">58img-定期删图</option>
<option value="AliEx">Ali</option>
<option value="tgphimg">TGPH-Debug通道</option>
<option value="qst8">qst8-国内CDN</option>
<option value="vviptuangou">vviptuangou-国内CDN</option>

View File

@ -0,0 +1,74 @@
export default {
async fetch(request, env) {
return handleAliExpressRequest(request, env);
}
};
async function handleAliExpressRequest(request, env) {
try {
// 确保请求方法为 POST
if (request.method !== 'POST') {
return new Response('Method not allowed', { status: 405 });
}
// 解析 multipart/form-data
const formData = await request.formData();
const imageFile = formData.get('image'); // 假设前端发送的字段名是 'image'
if (!imageFile) {
return new Response('No image file found in the request', { status: 400 });
}
// 从 Cloudflare KV 中读取 Cookie
const cookie = await env.WORKER_IMGBED.get('ali_express_cookie');
if (!cookie) {
return new Response('Missing required cookie in KV storage', { status: 500 });
}
// 准备发送到 AliExpress 的 FormData
const uploadFormData = new FormData();
uploadFormData.append('file', imageFile, imageFile.name);
uploadFormData.append('bizCode', 'ae_profile_avatar_upload');
// AliExpress 的上传 URL
const uploadUrl = 'https://filebroker.aliexpress.com/x/upload?jiketuchuang=1';
// 发送请求到 AliExpress 接口
const response = await fetch(uploadUrl, {
method: 'POST',
body: uploadFormData,
headers: {
'Origin': 'https://filebroker.aliexpress.com',
'Cookie': cookie
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
if (result.url) {
// 如果成功,返回图片 URL
return new Response(result.url, {
status: 200,
headers: { 'Content-Type': 'text/plain' }
});
} else {
// 如果没有返回 URL则可能上传失败
return new Response('Upload failed: No URL returned', { status: 500 });
}
} catch (error) {
console.error('Error in handleAliExpressRequest:', error);
return new Response(`Upload failed: ${error.message}`, { status: 500 });
}
}
// KV 命名空间设计
// 在 Cloudflare 中创建一个名为 "WORKER_IMGBED" 的 KV 命名空间,用于存储不同接口所需的 Cookie。
// 使用 "WORKER_IMGBED" 的键 "ali_express_cookie" 来存储具体的 AliExpress Cookie 字符串。
// 例如,在 wrangler.toml 文件中添加:
// [[kv_namespaces]]
// binding = "WORKER_IMGBED"
// id = "<your_kv_namespace_id>"

View File

@ -62,6 +62,9 @@ async function handleRequest(request) {
case '/upload/tgphimg':
response = await handleTgphimgRequest(request);
break;
case '/upload/aliex':
response = await handleAliExpressRequest(request);
break;
default:
response = new Response('Not Found', { status: 404 });
break;
@ -603,68 +606,64 @@ async function handleRequest(request) {
}
}
async function handle10086Request(request) {
console.log('Request received:', request.url);
async function handleAliExpressRequest(request, env) {
try {
// 确保请求方法为 POST
if (request.method !== 'POST') {
return new Response('Method not allowed', { status: 405 });
}
try {
// 解析 multipart/form-data
const formData = await request.formData();
const file = formData.get('image'); // 使用 'image' 字段名
if (!file) {
return new Response('No file uploaded', { status: 400 });
const imageFile = formData.get('image'); // 假设前端发送的字段名是 'image'
if (!imageFile) {
return new Response('No image file found in the request', { status: 400 });
}
const newFormData = new FormData();
newFormData.append('file', file, file.name); // 上传到目标服务器时使用 'file'
// 从 Cloudflare KV 中读取 Cookie
const cookie = await env.WORKER_IMGBED.get('ali_express_cookie');
if (!cookie) {
return new Response('Missing required cookie in KV storage', { status: 500 });
}
const targetUrl = 'https://mlw10086.serv00.net/upload.php';
// 准备发送到 AliExpress 的 FormData
const uploadFormData = new FormData();
uploadFormData.append('file', imageFile, imageFile.name);
uploadFormData.append('bizCode', 'ae_profile_avatar_upload');
const response = await fetch(targetUrl, {
// AliExpress 的上传 URL
const uploadUrl = 'https://filebroker.aliexpress.com/x/upload?jiketuchuang=1';
// 发送请求到 AliExpress 接口
const response = await fetch(uploadUrl, {
method: 'POST',
body: newFormData,
body: uploadFormData,
headers: {
'Accept': '*/*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Cache-Control': 'no-cache',
'DNT': '1',
'Origin': 'https://mlw10086.serv00.net',
'Pragma': 'no-cache',
'Referer': 'https://mlw10086.serv00.net/',
'Sec-Ch-Ua': '"Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
'Origin': 'https://filebroker.aliexpress.com',
'Cookie': cookie
}
});
console.log('Response status:', response.status);
const responseText = await response.text();
console.log('Response body:', responseText);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
try {
const jsonResponse = JSON.parse(responseText);
if (jsonResponse.code === 200 && jsonResponse.url) {
return new Response(jsonResponse.url, {
const result = await response.json();
if (result.url) {
// 如果成功,返回图片 URL
return new Response(result.url, {
status: 200,
headers: { 'Content-Type': 'text/plain' }
});
} else {
// 如果没有返回 URL则可能上传失败
return new Response('Upload failed: No URL returned', { status: 500 });
}
} catch (error) {
console.error('Error in handleAliExpressRequest:', error);
return new Response(`Upload failed: ${error.message}`, { status: 500 });
}
} catch (e) {
console.error('Failed to parse JSON:', e);
}
return new Response(responseText, {
status: response.status,
headers: response.headers
});
} catch (error) {
console.error('Error:', error);
return new Response('Internal Server Error', { status: 500 });
}
}

View File

@ -0,0 +1,78 @@
import requests
import os
import random
# 上传图片的本地路径
image_path = r"F:\Download\20241011-111233.jpg"
# AliExpress 上传接口的 URL
upload_url = "https://filebroker.aliexpress.com/x/upload?jiketuchuang=1"
# 允许的主机名
cdn_hosts = ["ae01", "ae02", "ae03", "ae04", "ae05"]
def load_cookie(file_path):
if os.path.exists(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return file.read().strip()
else:
print(f"错误: Cookie 文件不存在。文件路径: {file_path}")
return None
def upload_image(image_path):
if not os.path.exists(image_path):
print("错误: 图片文件不存在。")
return
# 加载 Cookie
cookie_path = r"G:\Github\WorkerJS_CloudFlare_ImageBed\python-uploader\ck-ali.txt"
cookie = load_cookie(cookie_path)
if not cookie:
return
# 请求头设置
headers = {
"Origin": "https://filebroker.aliexpress.com",
"Cookie": cookie
}
# 文件参数
files = {
'file': (os.path.basename(image_path), open(image_path, 'rb'))
}
# 其他参数
data = {
"bizCode": "ae_profile_avatar_upload"
}
try:
# 发送 POST 请求上传图片
response = requests.post(upload_url, headers=headers, files=files, data=data)
response.raise_for_status() # 检查请求是否成功
result = response.json()
# 处理返回结果
if result.get("code") == 0:
# 上传成功,随机选择一个 CDN 主机名
url = result.get("url")
if url:
selected_host = random.choice(cdn_hosts)
final_url = url.replace("ae02", selected_host)
print(f"上传成功,图片 URL: {final_url}")
else:
print("上传失败:未返回图片 URL。")
else:
print("上传失败,请检查是否已登录阿里巴巴账号?")
login_confirm = input("是否前往登录页面?(y/n): ")
if login_confirm.lower() == 'y':
print("请前往登录页面https://best.aliexpress.com/")
except requests.exceptions.RequestException as e:
# 网络请求异常处理
print(f"请求失败: {e}")
except ValueError:
# JSON 解码失败
print("上传失败:无法解析服务器返回的响应。")
# 执行图片上传
upload_image(image_path)