-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
大佬,可以上传原图吗?文件没有超过5m #211
Comments
应该是上传时TG自动压缩的,操作不了 |
但是直接在TG频道里发图片的时候,不是会让你选择以原图上传或者以压缩方式上传么... |
图片最大可以上传10MB,因为用的sendPhoto接口,会自动压缩。如果统一换成sendDocument接口可以实现原大小上传(免费用户最大支持50MB) |
你可以直接将upload.js改为只用 import { errorHandling, telemetryData } from "./utils/middleware";
export async function onRequestPost(context) {
const { request, env } = context;
try {
const clonedRequest = request.clone();
const formData = await clonedRequest.formData();
await errorHandling(context);
telemetryData(context);
const uploadFile = formData.get('file');
if (!uploadFile) {
throw new Error('No file uploaded');
}
const fileName = uploadFile.name;
const fileExtension = fileName.split('.').pop().toLowerCase();
const telegramFormData = new FormData();
telegramFormData.append("chat_id", env.TG_Chat_ID);
// 根据文件类型选择合适的上传方式
let apiEndpoint;
// if (uploadFile.type.startsWith('image/')) {
// telegramFormData.append("photo", uploadFile);
// apiEndpoint = 'sendPhoto';
// } else if (uploadFile.type.startsWith('audio/')) {
// telegramFormData.append("audio", uploadFile);
// apiEndpoint = 'sendAudio';
// } else {
telegramFormData.append("document", uploadFile);
apiEndpoint = 'sendDocument';
// }
const apiUrl = `https://api.telegram.org/bot${env.TG_Bot_Token}/${apiEndpoint}`;
console.log('Sending request to:', apiUrl);
const response = await fetch(
apiUrl,
{
method: "POST",
body: telegramFormData
}
);
console.log('Response status:', response.status);
const responseData = await response.json();
if (!response.ok) {
console.error('Error response from Telegram API:', responseData);
throw new Error(responseData.description || 'Upload to Telegram failed');
}
const fileId = getFileId(responseData);
if (!fileId) {
throw new Error('Failed to get file ID');
}
// 将文件信息保存到 KV 存储
if (env.img_url) {
await env.img_url.put(`${fileId}.${fileExtension}`, "", {
metadata: {
TimeStamp: Date.now(),
ListType: "None",
Label: "None",
liked: false,
fileName: fileName,
fileSize: uploadFile.size,
}
});
}
return new Response(
JSON.stringify([{ 'src': `/file/${fileId}.${fileExtension}` }]),
{
status: 200,
headers: { 'Content-Type': 'application/json' }
}
);
} catch (error) {
console.error('Upload error:', error);
return new Response(
JSON.stringify({ error: error.message }),
{
status: 500,
headers: { 'Content-Type': 'application/json' }
}
);
}
}
function getFileId(response) {
if (!response.ok || !response.result) return null;
const result = response.result;
// if (result.photo) {
// return result.photo.reduce((prev, current) =>
// (prev.file_size > current.file_size) ? prev : current
// ).file_id;
// }
if (result.document) return result.document.file_id;
// if (result.video) return result.video.file_id;
// if (result.audio) return result.audio.file_id;
return null;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
本来的图片是226kb,上传之后变成52kb了,压缩的都有点模糊了......
The text was updated successfully, but these errors were encountered: