feat: enhance file upload process with temporary file handling, improved error management, and metrics logging

This commit is contained in:
MythEclipse
2026-05-22 00:29:54 +07:00
parent fd5eb98586
commit 56b929ffb1
7 changed files with 176 additions and 75 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ node_modules
out out
dist dist
*.tgz *.tgz
.codegraph
# code coverage # code coverage
coverage coverage
*.lcov *.lcov
+1 -1
View File
@@ -1,9 +1,9 @@
import { findFileByPublicId } from '../db/files'; import { findFileByPublicId } from '../db/files';
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 { checkRateLimit } from '../utils/rateLimit'; import { checkRateLimit } from '../utils/rateLimit';
import { getBot } from '../utils/telegram'; import { getBot } from '../utils/telegram';
import { fileInfoCache } from '../utils/cache';
type RequestWithParams = Request & { type RequestWithParams = Request & {
params?: { params?: {
+135 -26
View File
@@ -1,4 +1,4 @@
import { createReadStream } from 'node:fs'; import { createReadStream, createWriteStream } from 'node:fs';
import { unlink } from 'node:fs/promises'; import { unlink } from 'node:fs/promises';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import { db, files as fileSchema } from '../db'; import { db, files as fileSchema } from '../db';
@@ -43,16 +43,118 @@ const normalizeFileType = (mimeType: string, fileName: string): string => {
return fileType === 'application' ? 'document' : fileType; return fileType === 'application' ? 'document' : fileType;
}; };
const performUpload = async ( const JSON_UPLOAD_LIMIT_BYTES = 50 * 1024 * 1024;
fileBuffer: Buffer, const SIGNATURE_BYTES = 16;
fileName: string,
mimeType: string, type PreparedUpload = {
): Promise<UploadedFile> => { tempPath: string;
fileHash: string;
sizeBytes: number;
signatureBuffer: Buffer;
};
const cleanupTempFile = async (tempPath: string): Promise<void> => {
try {
await unlink(tempPath);
} catch (err) {
logger.warn('Failed to cleanup temp file', { tempPath, error: getErrorMessage(err) });
}
};
const streamFileToTemp = async (file: File): Promise<PreparedUpload> => {
const tempPath = `/tmp/teleuploader-${nanoid()}`;
const writer = createWriteStream(tempPath);
const hasher = new Bun.CryptoHasher('sha256');
const reader = file.stream().getReader();
const signatureChunks: Buffer[] = [];
let signatureBytes = 0;
let sizeBytes = 0;
const writeChunk = async (chunk: Buffer): Promise<void> => {
if (!writer.write(chunk)) {
await new Promise<void>((resolve, reject) => {
writer.once('drain', resolve);
writer.once('error', reject);
});
}
};
const finishWriter = async (): Promise<void> => {
await new Promise<void>((resolve, reject) => {
writer.end(() => resolve());
writer.once('error', reject);
});
};
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = Buffer.from(value);
sizeBytes += chunk.byteLength;
hasher.update(chunk);
await writeChunk(chunk);
if (signatureBytes < SIGNATURE_BYTES) {
const remaining = SIGNATURE_BYTES - signatureBytes;
const signatureChunk = chunk.subarray(0, remaining);
signatureChunks.push(signatureChunk);
signatureBytes += signatureChunk.byteLength;
}
}
await finishWriter();
return {
tempPath,
fileHash: hasher.digest('hex'),
sizeBytes,
signatureBuffer: Buffer.concat(signatureChunks, signatureBytes),
};
} catch (error) {
writer.destroy();
await cleanupTempFile(tempPath);
throw error;
} finally {
reader.releaseLock();
}
};
const writeBufferToTemp = async (fileBuffer: Buffer, fileHash: string): Promise<PreparedUpload> => {
const tempPath = `/tmp/teleuploader-${nanoid()}`; const tempPath = `/tmp/teleuploader-${nanoid()}`;
try { try {
await Bun.write(tempPath, fileBuffer); await Bun.write(tempPath, fileBuffer);
const fileStream = createReadStream(tempPath); return {
const result = await forwardToStorage(fileStream, fileName, getFileType(mimeType, fileName)); tempPath,
fileHash,
sizeBytes: fileBuffer.byteLength,
signatureBuffer: fileBuffer.subarray(0, SIGNATURE_BYTES),
};
} catch (error) {
await cleanupTempFile(tempPath);
throw error;
}
};
const closeFileStream = async (fileStream: ReturnType<typeof createReadStream>): Promise<void> => {
if (fileStream.closed) return;
await new Promise<void>((resolve) => {
fileStream.once('close', resolve);
fileStream.destroy();
});
};
const performUpload = async (
prepared: PreparedUpload,
fileName: string,
mimeType: string,
fileType: string,
): Promise<UploadedFile> => {
const fileStream = createReadStream(prepared.tempPath);
try {
const result = await forwardToStorage(fileStream, fileName, fileType);
return { return {
publicId: nanoid(), publicId: nanoid(),
@@ -62,21 +164,16 @@ const performUpload = async (
storageMessageId: result.storageMessageId, storageMessageId: result.storageMessageId,
fileName, fileName,
mimeType: mimeType || 'application/octet-stream', mimeType: mimeType || 'application/octet-stream',
sizeBytes: fileBuffer.byteLength, sizeBytes: prepared.sizeBytes,
fileType: getFileType(mimeType, fileName), fileType,
uploaderId: 0, uploaderId: 0,
fileHash: computeHash(fileBuffer), fileHash: prepared.fileHash,
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
}; };
} finally { } finally {
setTimeout(async () => { await closeFileStream(fileStream);
try { await cleanupTempFile(prepared.tempPath);
await unlink(tempPath);
} catch (err) {
logger.warn('Failed to cleanup temp file', { tempPath, error: getErrorMessage(err) });
}
}, 500);
} }
}; };
@@ -112,28 +209,28 @@ const handleMultipartUpload = async (req: Request): Promise<Response> => {
return Response.json({ error: 'No file provided' }, { status: 400 }); return Response.json({ error: 'No file provided' }, { status: 400 });
} }
const fileBytes = await file.arrayBuffer(); const prepared = await streamFileToTemp(file);
const fileBuffer = Buffer.from(fileBytes);
const hash = computeHash(fileBuffer);
const existingFile = await findFileByHash(hash); const existingFile = await findFileByHash(prepared.fileHash);
if (existingFile) { if (existingFile) {
await cleanupTempFile(prepared.tempPath);
return Response.json(buildUploadResponse(existingFile, config.baseUrl), { status: 200 }); return Response.json(buildUploadResponse(existingFile, config.baseUrl), { status: 200 });
} }
const rawMimeType = file.type || extractMimeType({}, req) || 'application/octet-stream'; const rawMimeType = file.type || extractMimeType({}, req) || 'application/octet-stream';
const { fileName: finalFileName, mimeType } = ensureExtension( const { fileName: finalFileName, mimeType } = ensureExtension(
fileName, fileName,
fileBuffer, prepared.signatureBuffer,
rawMimeType, rawMimeType,
); );
const fileType = getFileType(mimeType, finalFileName); const fileType = getFileType(mimeType, finalFileName);
if (!checkFileSize(fileBuffer.byteLength, fileType)) { if (!checkFileSize(prepared.sizeBytes, fileType)) {
await cleanupTempFile(prepared.tempPath);
return Response.json({ error: `File size exceeds ${fileType} limit` }, { status: 400 }); return Response.json({ error: `File size exceeds ${fileType} limit` }, { status: 400 });
} }
const uploaded = await performUpload(fileBuffer, finalFileName, mimeType); const uploaded = await performUpload(prepared, finalFileName, mimeType, fileType);
await db.insert(fileSchema).values(uploaded); await db.insert(fileSchema).values(uploaded);
return Response.json(buildUploadResponse(uploaded, config.baseUrl), { status: 200 }); return Response.json(buildUploadResponse(uploaded, config.baseUrl), { status: 200 });
@@ -156,6 +253,17 @@ const handleJSONUpload = async (req: Request): Promise<Response> => {
} }
const { base64Data, mimeType: rawMimeType } = parseBase64File(file); const { base64Data, mimeType: rawMimeType } = parseBase64File(file);
const estimatedSizeBytes = Math.floor((base64Data.length * 3) / 4);
if (estimatedSizeBytes > JSON_UPLOAD_LIMIT_BYTES) {
return Response.json(
{
error:
'JSON base64 uploads are limited to 50MB. Use multipart/form-data for larger files',
},
{ status: 400 },
);
}
const fileBytes = Buffer.from(base64Data, 'base64'); const fileBytes = Buffer.from(base64Data, 'base64');
const hash = computeHash(fileBytes); const hash = computeHash(fileBytes);
@@ -171,7 +279,8 @@ const handleJSONUpload = async (req: Request): Promise<Response> => {
return Response.json({ error: `File size exceeds ${fileType} limit` }, { status: 400 }); return Response.json({ error: `File size exceeds ${fileType} limit` }, { status: 400 });
} }
const uploaded = await performUpload(fileBytes, finalFileName, mimeType); const prepared = await writeBufferToTemp(fileBytes, hash);
const uploaded = await performUpload(prepared, finalFileName, mimeType, fileType);
await db.insert(fileSchema).values(uploaded); await db.insert(fileSchema).values(uploaded);
return Response.json(buildUploadResponse(uploaded, config.baseUrl), { status: 200 }); return Response.json(buildUploadResponse(uploaded, config.baseUrl), { status: 200 });
+5 -2
View File
@@ -71,11 +71,14 @@ export const fileInfoCache = new Cache<{
}>(3600); }>(3600);
// Cleanup expired cache entries every 5 minutes // Cleanup expired cache entries every 5 minutes
setInterval(() => { setInterval(
() => {
const removed = fileInfoCache.cleanup(); const removed = fileInfoCache.cleanup();
if (removed > 0) { if (removed > 0) {
console.log(`Cleaned up ${removed} expired cache entries`); console.log(`Cleaned up ${removed} expired cache entries`);
} }
}, 5 * 60 * 1000); },
5 * 60 * 1000,
);
export { Cache }; export { Cache };
+5 -2
View File
@@ -109,7 +109,8 @@ class MetricsCollector {
export const metricsCollector = new MetricsCollector(); export const metricsCollector = new MetricsCollector();
// Log metrics every 5 minutes // Log metrics every 5 minutes
setInterval(() => { setInterval(
() => {
const snapshot = metricsCollector.getSnapshot(); const snapshot = metricsCollector.getSnapshot();
logger.info('Metrics snapshot', { logger.info('Metrics snapshot', {
uploadLatency: snapshot.uploadLatency, uploadLatency: snapshot.uploadLatency,
@@ -117,6 +118,8 @@ setInterval(() => {
errorRate: snapshot.errorRate.toFixed(2), errorRate: snapshot.errorRate.toFixed(2),
cacheHitRate: snapshot.cacheHitRate.toFixed(2), cacheHitRate: snapshot.cacheHitRate.toFixed(2),
}); });
}, 5 * 60 * 1000); },
5 * 60 * 1000,
);
export { MetricsCollector }; export { MetricsCollector };
+17 -25
View File
@@ -7,12 +7,12 @@ const botTokens = Array.from(new Set([config.botToken, ...config.additionalBotTo
const bots = botTokens.map((token) => new Telegraf(token)); const bots = botTokens.map((token) => new Telegraf(token));
let currentBotIndex = 0; let nextBotIndex = 0;
const rotateBot = (): { previousIndex: number; nextIndex: number } => { const claimBotIndex = (): number => {
const previousIndex = currentBotIndex; const botIndex = nextBotIndex;
currentBotIndex = (currentBotIndex + 1) % bots.length; nextBotIndex = (nextBotIndex + 1) % bots.length;
return { previousIndex, nextIndex: currentBotIndex }; return botIndex;
}; };
const sleep = (seconds: number): Promise<void> => { const sleep = (seconds: number): Promise<void> => {
@@ -24,7 +24,8 @@ const executeWithBotRetry = async <T>(
retries = 5, retries = 5,
attemptedBots = 0, attemptedBots = 0,
): Promise<T> => { ): Promise<T> => {
const currentBot = bots[currentBotIndex]; const botIndex = claimBotIndex();
const currentBot = bots[botIndex];
try { try {
return await action(currentBot); return await action(currentBot);
} catch (error: unknown) { } catch (error: unknown) {
@@ -32,17 +33,16 @@ const executeWithBotRetry = async <T>(
const match = errorStr.match(/retry after (\d+)/i); const match = errorStr.match(/retry after (\d+)/i);
if (match) { if (match) {
const { previousIndex, nextIndex } = rotateBot(); const nextIndex = nextBotIndex;
attemptedBots++; const nextAttemptedBots = attemptedBots + 1;
if (attemptedBots < bots.length) { if (nextAttemptedBots < bots.length) {
logger.info( logger.info(
`Bot Index ${previousIndex} hit 429. Instantly rotating to Bot Index ${nextIndex}...`, `Bot Index ${botIndex} hit 429. Instantly rotating to Bot Index ${nextIndex}...`,
); );
return executeWithBotRetry(action, retries, attemptedBots); return executeWithBotRetry(action, retries, nextAttemptedBots);
} }
// If all bots in the pool have been tried and hit 429, sleep
if (retries > 0) { if (retries > 0) {
const seconds = parseInt(match[1], 10); const seconds = parseInt(match[1], 10);
logger.warn(`All bots in the pool are rate-limited. Sleeping for ${seconds} seconds...`, { logger.warn(`All bots in the pool are rate-limited. Sleeping for ${seconds} seconds...`, {
@@ -159,13 +159,10 @@ export const forwardToStorage = async (
const sendMethod = sendMethodMap[fileType] || 'sendDocument'; const sendMethod = sendMethodMap[fileType] || 'sendDocument';
const payload = buildSendPayload(fileType, fileName); const payload = buildSendPayload(fileType, fileName);
const uploadResult = await executeWithBotRetry((activeBot) => { return executeWithBotRetry((activeBot) => {
const telegram = activeBot.telegram as unknown as Record<string, SendMethod>; const telegram = activeBot.telegram as unknown as Record<string, SendMethod>;
return telegram[sendMethod](config.storageChatId, filePayload, payload); return telegram[sendMethod](config.storageChatId, filePayload, payload);
}); });
currentBotIndex = (currentBotIndex + 1) % bots.length;
return uploadResult;
}); });
const uploadedFile = extractUploadedFile(result, fileType); const uploadedFile = extractUploadedFile(result, fileType);
@@ -202,16 +199,13 @@ export const forwardMediaGroupToStorage = async (
const result = await enqueueUpload(async (): Promise<TelegramMessageResult[]> => { const result = await enqueueUpload(async (): Promise<TelegramMessageResult[]> => {
const mediaGroup = buildMediaGroup(items); const mediaGroup = buildMediaGroup(items);
const uploadResult = await executeWithBotRetry((activeBot) => { return executeWithBotRetry((activeBot) => {
const sendMediaGroup = activeBot.telegram.sendMediaGroup as unknown as ( const sendMediaGroup = activeBot.telegram.sendMediaGroup as unknown as (
chatId: number, chatId: number,
media: MediaGroupPayloadItem[], media: MediaGroupPayloadItem[],
) => Promise<TelegramMessageResult[]>; ) => Promise<TelegramMessageResult[]>;
return sendMediaGroup(config.storageChatId, mediaGroup); return sendMediaGroup(config.storageChatId, mediaGroup);
}); });
currentBotIndex = (currentBotIndex + 1) % bots.length;
return uploadResult;
}); });
const messages = Array.isArray(result) ? result : [result]; const messages = Array.isArray(result) ? result : [result];
@@ -239,9 +233,7 @@ export const forwardMediaGroupToStorage = async (
} }
}; };
export const getFileInfo = async ( export const getFileInfo = async (telegramFileId: string): Promise<TelegramFileInfo> => {
telegramFileId: string,
): Promise<TelegramFileInfo> => {
try { try {
const result = await executeWithBotRetry((activeBot) => const result = await executeWithBotRetry((activeBot) =>
activeBot.telegram.getFile(telegramFileId), activeBot.telegram.getFile(telegramFileId),
@@ -261,6 +253,6 @@ export const getFileInfo = async (
} }
}; };
export const getBot = (): Telegraf => bots[currentBotIndex]; export const getBot = (): Telegraf => bots[nextBotIndex];
export const getCurrentBotIndex = (): number => currentBotIndex; export const getCurrentBotIndex = (): number => nextBotIndex;
+1 -7
View File
@@ -1,14 +1,8 @@
import PQueue from 'p-queue'; import PQueue from 'p-queue';
import logger from './logger'; import logger from './logger';
// Create queue with concurrency limit matching bot pool size
// Concurrency: 4-8 uploads in parallel
// Interval: 1 second window for rate limiting
// IntervalCap: Max 10 tasks per second
const uploadQueue = new PQueue({ const uploadQueue = new PQueue({
concurrency: 4, concurrency: Number.POSITIVE_INFINITY,
interval: 1000,
intervalCap: 10,
}); });
// Monitor queue events // Monitor queue events