refactor: update Telegram file handling to use new getFileInfo function and include bot token in cache
This commit is contained in:
+8
-12
@@ -5,7 +5,7 @@ import { findFileByPublicId } from '../db/files';
|
||||
import { fileInfoCache } from '../utils/cache';
|
||||
import { formatCreatedAt, getErrorMessage } from '../utils/file';
|
||||
import logger from '../utils/logger';
|
||||
import { getBot } from '../utils/telegram';
|
||||
import { getFileInfo } from '../utils/telegram';
|
||||
import { locateZipEntry } from '../utils/zip';
|
||||
|
||||
type RequestWithParams = Request & {
|
||||
@@ -19,13 +19,7 @@ const getTelegramFileInfo = async (telegramFileId: string, public_id: string) =>
|
||||
let fileInfo = fileInfoCache.get(cacheKey);
|
||||
|
||||
if (!fileInfo) {
|
||||
const bot = getBot();
|
||||
const apiFileInfo = await bot.telegram.getFile(telegramFileId);
|
||||
fileInfo = {
|
||||
file_size: (apiFileInfo as any).file_size || 0,
|
||||
mime_type: (apiFileInfo as any).mime_type || 'application/octet-stream',
|
||||
file_path: (apiFileInfo as any).file_path || '',
|
||||
};
|
||||
fileInfo = await getFileInfo(telegramFileId);
|
||||
fileInfoCache.set(cacheKey, fileInfo);
|
||||
logger.debug('File info cached', { public_id, cacheKey });
|
||||
} else {
|
||||
@@ -35,8 +29,8 @@ const getTelegramFileInfo = async (telegramFileId: string, public_id: string) =>
|
||||
return fileInfo;
|
||||
};
|
||||
|
||||
const buildTelegramFileUrl = (filePath: string): string =>
|
||||
`https://api.telegram.org/file/bot${process.env.BOT_TOKEN}/${filePath}`;
|
||||
const buildTelegramFileUrl = (filePath: string, botToken: string): string =>
|
||||
`https://api.telegram.org/file/bot${botToken}/${filePath}`;
|
||||
|
||||
const cleanupTempFile = async (tempPath: string): Promise<void> => {
|
||||
try {
|
||||
@@ -69,7 +63,9 @@ export const handleFileRedirect = async (req: RequestWithParams): Promise<Respon
|
||||
if (archiveEntryName) {
|
||||
const archiveFileId = file.archiveTelegramFileId || file.telegramFileId;
|
||||
const archiveInfo = await getTelegramFileInfo(archiveFileId, public_id);
|
||||
const archiveResponse = await fetch(buildTelegramFileUrl(archiveInfo.file_path));
|
||||
const archiveResponse = await fetch(
|
||||
buildTelegramFileUrl(archiveInfo.file_path, archiveInfo.bot_token),
|
||||
);
|
||||
|
||||
if (!archiveResponse.ok) {
|
||||
logger.error('Archive download failed', { public_id, status: archiveResponse.status });
|
||||
@@ -109,7 +105,7 @@ export const handleFileRedirect = async (req: RequestWithParams): Promise<Respon
|
||||
}
|
||||
|
||||
const fileInfo = await getTelegramFileInfo(file.telegramFileId, public_id);
|
||||
const redirectUrl = buildTelegramFileUrl(fileInfo.file_path);
|
||||
const redirectUrl = buildTelegramFileUrl(fileInfo.file_path, fileInfo.bot_token);
|
||||
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
|
||||
@@ -68,6 +68,7 @@ export const fileInfoCache = new Cache<{
|
||||
file_size: number;
|
||||
mime_type: string;
|
||||
file_path: string;
|
||||
bot_token: string;
|
||||
}>(3600);
|
||||
|
||||
// Cleanup expired cache entries every 5 minutes
|
||||
|
||||
+17
-6
@@ -5,6 +5,11 @@ import { enqueueUpload } from './telegramQueue';
|
||||
|
||||
const botTokens = Array.from(new Set([config.botToken, ...config.additionalBotTokens]));
|
||||
|
||||
type FileInfoResult = {
|
||||
result: unknown;
|
||||
botToken: string;
|
||||
};
|
||||
|
||||
const bots = botTokens.map((token) => new Telegraf(token));
|
||||
|
||||
let nextBotIndex = 0;
|
||||
@@ -20,14 +25,15 @@ const sleep = (seconds: number): Promise<void> => {
|
||||
};
|
||||
|
||||
const executeWithBotRetry = async <T>(
|
||||
action: (botInstance: Telegraf) => Promise<T>,
|
||||
action: (botInstance: Telegraf, botToken: string) => Promise<T>,
|
||||
retries = 5,
|
||||
attemptedBots = 0,
|
||||
): Promise<T> => {
|
||||
const botIndex = claimBotIndex();
|
||||
const currentBot = bots[botIndex];
|
||||
const currentToken = botTokens[botIndex];
|
||||
try {
|
||||
return await action(currentBot);
|
||||
return await action(currentBot, currentToken);
|
||||
} catch (error: unknown) {
|
||||
const errorStr = error instanceof Error ? error.message : String(error);
|
||||
const match = errorStr.match(/retry after (\d+)/i);
|
||||
@@ -62,10 +68,11 @@ interface ForwardResult {
|
||||
storageMessageId: number;
|
||||
}
|
||||
|
||||
interface TelegramFileInfo {
|
||||
export interface TelegramFileInfo {
|
||||
file_size: number;
|
||||
mime_type: string;
|
||||
file_path: string;
|
||||
bot_token: string;
|
||||
}
|
||||
|
||||
interface UploadedTelegramFile {
|
||||
@@ -94,7 +101,7 @@ type SendMethod = (
|
||||
payload?: SendPayload,
|
||||
) => Promise<TelegramMessageResult>;
|
||||
|
||||
const sendMethodMap: Record<string, keyof Telegraf['telegram']> = {
|
||||
const sendMethodMap: Record<string, string> = {
|
||||
photo: 'sendPhoto',
|
||||
audio: 'sendAudio',
|
||||
video: 'sendVideo',
|
||||
@@ -235,8 +242,11 @@ export const forwardMediaGroupToStorage = async (
|
||||
|
||||
export const getFileInfo = async (telegramFileId: string): Promise<TelegramFileInfo> => {
|
||||
try {
|
||||
const result = await executeWithBotRetry((activeBot) =>
|
||||
activeBot.telegram.getFile(telegramFileId),
|
||||
const { result, botToken } = await executeWithBotRetry<FileInfoResult>(
|
||||
async (activeBot, activeToken) => ({
|
||||
result: await activeBot.telegram.getFile(telegramFileId),
|
||||
botToken: activeToken,
|
||||
}),
|
||||
);
|
||||
|
||||
const fileData = result as unknown as TelegramFileInfo;
|
||||
@@ -244,6 +254,7 @@ export const getFileInfo = async (telegramFileId: string): Promise<TelegramFileI
|
||||
file_size: fileData.file_size || 0,
|
||||
mime_type: fileData.mime_type || 'application/octet-stream',
|
||||
file_path: fileData.file_path || '',
|
||||
bot_token: botToken,
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
logger.error('Failed to get file info', {
|
||||
|
||||
Reference in New Issue
Block a user