Skip to content

Commit

Permalink
0.12.0
Browse files Browse the repository at this point in the history
增加需要自己简单登录的3001图床
  • Loading branch information
BlueSkyXN committed Nov 18, 2024
1 parent aed7104 commit bedce03
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cloudflare-page/OneAPI-imgbed-MIX.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ <h2>File Upload</h2>
<option value="58img">58img-定期删图</option>
<option value="ucloud">Ucloud</option>
<option value="tgphimg">TGPH-Debug通道-短时间删图</option>
<option value="3001">3001-Freebuf-不提供账号请自行部署后端</option>
<option value="3001">3001-Freebuf-国内CDN-不提供账号请自行部署后端</option>
<option value="aliex">AliEx-国内CDN国外Akamai-不提供账号请自行部署后端</option>
</select>
</div>
Expand Down
78 changes: 62 additions & 16 deletions cloudflare-worker-js-api/API_IMG_3001.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
这个试验我放在了KV库
你需要创建和绑定名为 WORKER_IMGBED 的库,其中新建 K 字段,名为 3001_auth 然后在V中复制进去浏览器F12得到的完整Authorization即可
Authorization需要你注册登录 https://www.freebuf.com/ 获取,有效期不详,也许是一个月
*/

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
Expand All @@ -7,23 +13,23 @@ addEventListener('fetch', event => {
* @param {Request} request
*/
async function handle3001Request(request) {
// 允许跨域请求
// 定义 CORS 头部
const corsHeaders = {
'Access-Control-Allow-Origin': '*', // 根据需要调整
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Authorization, Content-Type',
}

// 处理 CORS 预检请求
if (request.method === 'OPTIONS') {
// 处理 CORS 预检请求
return new Response(null, {
status: 204,
headers: corsHeaders,
})
}

// 处理 GET 请求,返回说明信息
if (request.method === 'GET') {
// 返回简单的说明页面或提示信息
return new Response('3001 图床服务,请使用 POST 方法上传图片。', {
status: 200,
headers: {
Expand All @@ -33,13 +39,20 @@ addEventListener('fetch', event => {
})
}

// 仅处理 POST 请求
if (request.method === 'POST') {
try {
// 解析表单数据
const formData = await request.formData()
const imageFile = formData.get('file')
const imageFile = formData.get('image') // 前端发送的是 'image'
const isBase64 = formData.get('is_base64') || '0'

// 打印所有表单字段(调试用)
for (const [key, value] of formData.entries()) {
console.log(`Form field: ${key}`)
}

// 校验图片文件
if (!imageFile || imageFile.size === 0) {
return new Response('No image file found in the request', {
status: 400,
Expand All @@ -63,7 +76,7 @@ addEventListener('fetch', event => {
})
}

// 构建上传表单数据
// 构建上传表单数据,将 'image' 重命名为 'file'
const uploadFormData = new FormData()
uploadFormData.append('file', imageFile, imageFile.name)
uploadFormData.append('is_base64', isBase64)
Expand Down Expand Up @@ -91,9 +104,22 @@ addEventListener('fetch', event => {
body: uploadFormData,
})

// 检查响应状态码
// 处理响应
if (response.status === 200) {
const responseData = await response.json()
let responseData
try {
responseData = await response.json()
} catch (parseError) {
console.error('Error parsing JSON response:', parseError)
return new Response('上传失败:无法解析服务器响应。', {
status: 500,
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
...corsHeaders,
},
})
}

console.log('响应 JSON 内容:', JSON.stringify(responseData, null, 2))

// 检查响应结构
Expand All @@ -107,31 +133,51 @@ addEventListener('fetch', event => {

console.log(`图片上传成功: ${imageUrl}`)

// 返回成功响应
return new Response(JSON.stringify({ imageUrl: imageUrl }), {
// 返回纯文本格式的图片 URL
return new Response(imageUrl, {
status: 200,
headers: {
'Content-Type': 'application/json;charset=UTF-8',
'Content-Type': 'text/plain;charset=UTF-8',
...corsHeaders,
},
})
} else {
console.error(`上传失败,消息: ${responseData.msg || '未知错误'}`)
return new Response(`上传失败,消息: ${responseData.msg || '未知错误'}`, {

// 返回原始 JSON 错误信息
return new Response(JSON.stringify(responseData), {
status: 500,
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
'Content-Type': 'application/json;charset=UTF-8',
...corsHeaders,
},
})
}
} else {
const errorText = await response.text()
console.error(`上传失败。状态码: ${response.status}, 响应内容: ${errorText}`)
return new Response(`上传失败。状态码: ${response.status}`, {
// 尝试解析错误响应为 JSON
let errorData
try {
errorData = await response.json()
} catch {
// 如果解析失败,获取纯文本
const errorText = await response.text()
console.error(`上传失败。状态码: ${response.status}, 响应内容: ${errorText}`)
return new Response(`上传失败。状态码: ${response.status}`, {
status: response.status,
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
...corsHeaders,
},
})
}

console.error(`上传失败。状态码: ${response.status}, 响应内容: ${JSON.stringify(errorData)}`)

// 返回原始 JSON 错误信息
return new Response(JSON.stringify(errorData), {
status: response.status,
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
'Content-Type': 'application/json;charset=UTF-8',
...corsHeaders,
},
})
Expand Down
72 changes: 56 additions & 16 deletions cloudflare-worker-js-api/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,23 +468,23 @@ async function handleRequest(request) {
}

async function handle3001Request(request) {
// 允许跨域请求
// 定义 CORS 头部
const corsHeaders = {
'Access-Control-Allow-Origin': '*', // 根据需要调整
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Authorization, Content-Type',
}

// 处理 CORS 预检请求
if (request.method === 'OPTIONS') {
// 处理 CORS 预检请求
return new Response(null, {
status: 204,
headers: corsHeaders,
})
}

// 处理 GET 请求,返回说明信息
if (request.method === 'GET') {
// 返回简单的说明页面或提示信息
return new Response('3001 图床服务,请使用 POST 方法上传图片。', {
status: 200,
headers: {
Expand All @@ -494,13 +494,20 @@ async function handleRequest(request) {
})
}

// 仅处理 POST 请求
if (request.method === 'POST') {
try {
// 解析表单数据
const formData = await request.formData()
const imageFile = formData.get('file')
const imageFile = formData.get('image') // 前端发送的是 'image'
const isBase64 = formData.get('is_base64') || '0'

// 打印所有表单字段(调试用)
for (const [key, value] of formData.entries()) {
console.log(`Form field: ${key}`)
}

// 校验图片文件
if (!imageFile || imageFile.size === 0) {
return new Response('No image file found in the request', {
status: 400,
Expand All @@ -524,7 +531,7 @@ async function handleRequest(request) {
})
}

// 构建上传表单数据
// 构建上传表单数据,将 'image' 重命名为 'file'
const uploadFormData = new FormData()
uploadFormData.append('file', imageFile, imageFile.name)
uploadFormData.append('is_base64', isBase64)
Expand Down Expand Up @@ -552,9 +559,22 @@ async function handleRequest(request) {
body: uploadFormData,
})

// 检查响应状态码
// 处理响应
if (response.status === 200) {
const responseData = await response.json()
let responseData
try {
responseData = await response.json()
} catch (parseError) {
console.error('Error parsing JSON response:', parseError)
return new Response('上传失败:无法解析服务器响应。', {
status: 500,
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
...corsHeaders,
},
})
}

console.log('响应 JSON 内容:', JSON.stringify(responseData, null, 2))

// 检查响应结构
Expand All @@ -568,31 +588,51 @@ async function handleRequest(request) {

console.log(`图片上传成功: ${imageUrl}`)

// 返回成功响应
return new Response(JSON.stringify({ imageUrl: imageUrl }), {
// 返回纯文本格式的图片 URL
return new Response(imageUrl, {
status: 200,
headers: {
'Content-Type': 'application/json;charset=UTF-8',
'Content-Type': 'text/plain;charset=UTF-8',
...corsHeaders,
},
})
} else {
console.error(`上传失败,消息: ${responseData.msg || '未知错误'}`)
return new Response(`上传失败,消息: ${responseData.msg || '未知错误'}`, {

// 返回原始 JSON 错误信息
return new Response(JSON.stringify(responseData), {
status: 500,
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
'Content-Type': 'application/json;charset=UTF-8',
...corsHeaders,
},
})
}
} else {
const errorText = await response.text()
console.error(`上传失败。状态码: ${response.status}, 响应内容: ${errorText}`)
return new Response(`上传失败。状态码: ${response.status}`, {
// 尝试解析错误响应为 JSON
let errorData
try {
errorData = await response.json()
} catch {
// 如果解析失败,获取纯文本
const errorText = await response.text()
console.error(`上传失败。状态码: ${response.status}, 响应内容: ${errorText}`)
return new Response(`上传失败。状态码: ${response.status}`, {
status: response.status,
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
...corsHeaders,
},
})
}

console.error(`上传失败。状态码: ${response.status}, 响应内容: ${JSON.stringify(errorData)}`)

// 返回原始 JSON 错误信息
return new Response(JSON.stringify(errorData), {
status: response.status,
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
'Content-Type': 'application/json;charset=UTF-8',
...corsHeaders,
},
})
Expand Down
4 changes: 4 additions & 0 deletions python-uploader/test-3001.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
Authorization需要你注册登录 https://www.freebuf.com/ 获取,有效期不详,也许是一个月
"""

import requests
import os
import json
Expand Down

0 comments on commit bedce03

Please sign in to comment.