2026-05-21 22:31:55 +07:00
|
|
|
import { findFileByPublicId } from '../db/files';
|
2026-05-22 00:29:54 +07:00
|
|
|
import { fileInfoCache } from '../utils/cache';
|
2026-05-21 22:31:55 +07:00
|
|
|
import { formatCreatedAt, getErrorMessage } from '../utils/file';
|
2026-05-18 07:29:01 +07:00
|
|
|
import logger from '../utils/logger';
|
|
|
|
|
import { checkRateLimit } from '../utils/rateLimit';
|
2026-05-21 22:55:42 +07:00
|
|
|
import { getBot } from '../utils/telegram';
|
2026-05-22 00:51:49 +07:00
|
|
|
import { extractZipEntry } from '../utils/zip';
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-05-18 07:27:06 +07:00
|
|
|
type RequestWithParams = Request & {
|
|
|
|
|
params?: {
|
|
|
|
|
public_id?: string;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-22 00:51:49 +07:00
|
|
|
const getTelegramFileInfo = async (telegramFileId: string, public_id: string) => {
|
|
|
|
|
const cacheKey = `file_info_${telegramFileId}`;
|
|
|
|
|
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 || '',
|
|
|
|
|
};
|
|
|
|
|
fileInfoCache.set(cacheKey, fileInfo);
|
|
|
|
|
logger.debug('File info cached', { public_id, cacheKey });
|
|
|
|
|
} else {
|
|
|
|
|
logger.debug('File info from cache', { public_id, cacheKey });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fileInfo;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const buildTelegramFileUrl = (filePath: string): string =>
|
|
|
|
|
`https://api.telegram.org/file/bot${process.env.BOT_TOKEN}/${filePath}`;
|
|
|
|
|
|
2026-05-18 07:27:06 +07:00
|
|
|
export const handleFileRedirect = async (req: RequestWithParams): Promise<Response> => {
|
|
|
|
|
const public_id = req.params?.public_id;
|
2026-05-18 07:09:29 +07:00
|
|
|
try {
|
|
|
|
|
const ip = req.headers.get('x-forwarded-for') || '127.0.0.1';
|
|
|
|
|
|
|
|
|
|
if (!public_id || !checkRateLimit(ip)) {
|
|
|
|
|
return Response.json({ error: 'Rate limit exceeded' }, { status: 429 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
const file = await findFileByPublicId(public_id);
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
if (!file) {
|
2026-05-18 07:09:29 +07:00
|
|
|
logger.warn('File not found', { public_id });
|
|
|
|
|
return Response.json({ error: 'File not found' }, { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 00:51:49 +07:00
|
|
|
const archiveEntryName = file.archiveEntryName;
|
|
|
|
|
if (archiveEntryName) {
|
|
|
|
|
const archiveFileId = file.archiveTelegramFileId || file.telegramFileId;
|
|
|
|
|
const archiveInfo = await getTelegramFileInfo(archiveFileId, public_id);
|
|
|
|
|
const archiveResponse = await fetch(buildTelegramFileUrl(archiveInfo.file_path));
|
2026-05-21 23:31:49 +07:00
|
|
|
|
2026-05-22 00:51:49 +07:00
|
|
|
if (!archiveResponse.ok) {
|
|
|
|
|
logger.error('Archive download failed', { public_id, status: archiveResponse.status });
|
|
|
|
|
return Response.json({ error: 'Server error' }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const archiveBuffer = Buffer.from(await archiveResponse.arrayBuffer());
|
|
|
|
|
const extractedFile = await extractZipEntry(archiveBuffer, archiveEntryName);
|
|
|
|
|
if (!extractedFile) {
|
|
|
|
|
logger.error('Archive entry not found', { public_id, archiveEntryName });
|
|
|
|
|
return Response.json({ error: 'File not found' }, { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Response(extractedFile, {
|
|
|
|
|
status: 200,
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': file.mimeType,
|
|
|
|
|
'Content-Disposition': `attachment; filename="${file.fileName.replace(/"/g, '')}"`,
|
|
|
|
|
'Content-Length': String(extractedFile.byteLength),
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-05-21 23:31:49 +07:00
|
|
|
}
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-05-22 00:51:49 +07:00
|
|
|
const fileInfo = await getTelegramFileInfo(file.telegramFileId, public_id);
|
|
|
|
|
const redirectUrl = buildTelegramFileUrl(fileInfo.file_path);
|
2026-05-18 07:09:29 +07:00
|
|
|
return new Response(null, {
|
|
|
|
|
status: 302,
|
|
|
|
|
headers: {
|
2026-05-18 07:29:01 +07:00
|
|
|
Location: redirectUrl,
|
|
|
|
|
},
|
2026-05-18 07:09:29 +07:00
|
|
|
});
|
2026-05-21 22:31:55 +07:00
|
|
|
} catch (error: unknown) {
|
|
|
|
|
logger.error('File redirect error', { public_id, error: getErrorMessage(error) });
|
2026-05-18 07:09:29 +07:00
|
|
|
return Response.json({ error: 'Server error' }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-18 07:27:06 +07:00
|
|
|
export const handleFileInfo = async (req: RequestWithParams): Promise<Response> => {
|
|
|
|
|
const public_id = req.params?.public_id;
|
2026-05-18 07:09:29 +07:00
|
|
|
try {
|
|
|
|
|
if (!public_id) {
|
|
|
|
|
return Response.json({ error: 'Missing file id' }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
const file = await findFileByPublicId(public_id);
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
if (!file) {
|
2026-05-18 07:09:29 +07:00
|
|
|
logger.warn('File not found', { public_id });
|
|
|
|
|
return Response.json({ error: 'File not found' }, { status: 404 });
|
|
|
|
|
}
|
2026-05-18 07:29:01 +07:00
|
|
|
return Response.json(
|
|
|
|
|
{
|
|
|
|
|
public_id: file.publicId,
|
|
|
|
|
file_name: file.fileName,
|
|
|
|
|
mime_type: file.mimeType,
|
|
|
|
|
size_bytes: file.sizeBytes,
|
|
|
|
|
file_type: file.fileType,
|
|
|
|
|
uploader_id: file.uploaderId,
|
2026-05-21 22:31:55 +07:00
|
|
|
created_at: formatCreatedAt(file.createdAt),
|
2026-05-18 07:29:01 +07:00
|
|
|
},
|
|
|
|
|
{ status: 200 },
|
|
|
|
|
);
|
2026-05-21 22:31:55 +07:00
|
|
|
} catch (error: unknown) {
|
|
|
|
|
logger.error('File info error', { public_id, error: getErrorMessage(error) });
|
2026-05-18 07:09:29 +07:00
|
|
|
return Response.json({ error: 'Server error' }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
};
|