feat: implement caching for file info, add rate limiting, and enhance upload handling with retry logic

This commit is contained in:
MythEclipse
2026-05-21 23:31:49 +07:00
parent 10c968cf01
commit fd5eb98586
10 changed files with 557 additions and 11 deletions
+20 -2
View File
@@ -3,6 +3,7 @@ import { formatCreatedAt, getErrorMessage } from '../utils/file';
import logger from '../utils/logger';
import { checkRateLimit } from '../utils/rateLimit';
import { getBot } from '../utils/telegram';
import { fileInfoCache } from '../utils/cache';
type RequestWithParams = Request & {
params?: {
@@ -26,8 +27,25 @@ export const handleFileRedirect = async (req: RequestWithParams): Promise<Respon
return Response.json({ error: 'File not found' }, { status: 404 });
}
const bot = getBot();
const fileInfo = await bot.telegram.getFile(file.telegramFileId);
// Check cache first
const cacheKey = `file_info_${file.telegramFileId}`;
let fileInfo = fileInfoCache.get(cacheKey);
if (!fileInfo) {
// Cache miss - fetch from Telegram API
const bot = getBot();
const apiFileInfo = await bot.telegram.getFile(file.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 || '',
};
// Store in cache
fileInfoCache.set(cacheKey, fileInfo);
logger.debug('File info cached', { public_id, cacheKey });
} else {
logger.debug('File info from cache', { public_id, cacheKey });
}
const redirectUrl = `https://api.telegram.org/file/bot${process.env.BOT_TOKEN}/${fileInfo.file_path}`;
return new Response(null, {
+8 -5
View File
@@ -1,4 +1,5 @@
import { createReadStream, unlinkSync } from 'node:fs';
import { createReadStream } from 'node:fs';
import { unlink } from 'node:fs/promises';
import { nanoid } from 'nanoid';
import { db, files as fileSchema } from '../db';
import { findFileByHash } from '../db/files';
@@ -69,11 +70,13 @@ const performUpload = async (
updatedAt: new Date(),
};
} finally {
setTimeout(() => {
setTimeout(async () => {
try {
unlinkSync(tempPath);
} catch {}
}, 50);
await unlink(tempPath);
} catch (err) {
logger.warn('Failed to cleanup temp file', { tempPath, error: getErrorMessage(err) });
}
}, 500);
}
};