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