feat: support multipart file uploads

This commit is contained in:
Pham Ba Tuan
2025-06-07 15:15:56 +07:00
parent 309b815b55
commit 2dbe8571ed
2 changed files with 23 additions and 19 deletions
+2
View File
@@ -20,6 +20,8 @@ Chạy trên Cloudflare Worker, đơn giản hoạt động như một proxy cho
- Hiệu suất cao với mạng lưới toàn cầu của Cloudflare
- Trang tài liệu tích hợp sẵn
- Hỗ trợ tất cả các phương thức HTTP (GET, POST, PUT, DELETE)
- Gửi tệp tin bằng `multipart/form-data` (ví dụ: `sendPhoto`, `sendDocument`)
- Xử lý biểu tượng cảm xúc và ký tự đặc biệt ổn định hơn
## Cài đặt
+21 -19
View File
@@ -108,25 +108,27 @@ async function handleRequest(request) {
// Reconstruct the Telegram API URL
const telegramUrl = `${TELEGRAM_API_BASE}${url.pathname}${url.search}`;
// Create headers for the new request
const headers = new Headers(request.headers);
// Forward the request to Telegram API
let body = undefined;
if (request.method !== 'GET' && request.method !== 'HEAD') {
try {
body = await request.arrayBuffer();
} catch (err) {
return new Response(`Failed to read request body: ${err.message}`, { status: 400 });
}
}
const proxyReq = new Request(telegramUrl, {
method: request.method,
headers: request.headers,
body,
redirect: 'follow',
});
// Clone request headers so we can safely modify them
const headers = new Headers(request.headers);
// Ensure JSON requests explicitly use UTF-8 to avoid issues with emoji or
// other special characters
const contentType = headers.get('Content-Type');
if (contentType && contentType.startsWith('application/json') && !contentType.includes('charset')) {
headers.set('Content-Type', 'application/json; charset=UTF-8');
}
const init = {
method: request.method,
headers,
redirect: 'follow',
};
if (request.method !== 'GET' && request.method !== 'HEAD') {
init.body = request.body;
}
// Forward the request to Telegram API, streaming the body when present.
const proxyReq = new Request(telegramUrl, init);
try {
const tgRes = await fetch(proxyReq);