2026-05-18 07:09:29 +07:00
|
|
|
import { eq } from 'drizzle-orm';
|
2026-05-18 07:29:01 +07:00
|
|
|
import { db, files as fileSchema } from '../db';
|
|
|
|
|
import logger from '../utils/logger';
|
|
|
|
|
import { checkRateLimit } from '../utils/rateLimit';
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-05-18 07:27:06 +07:00
|
|
|
type RequestWithParams = Request & {
|
|
|
|
|
params?: {
|
|
|
|
|
public_id?: string;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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-18 07:29:01 +07:00
|
|
|
const result = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(fileSchema)
|
|
|
|
|
.where(eq(fileSchema.publicId, public_id))
|
|
|
|
|
.limit(1);
|
2026-05-18 07:09:29 +07:00
|
|
|
|
|
|
|
|
if (!result.length) {
|
|
|
|
|
logger.warn('File not found', { public_id });
|
|
|
|
|
return Response.json({ error: 'File not found' }, { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const file = result[0];
|
2026-05-18 07:27:06 +07:00
|
|
|
const { getBot } = await import('../utils/telegram');
|
2026-05-18 07:09:29 +07:00
|
|
|
const bot = getBot();
|
2026-05-18 07:27:06 +07:00
|
|
|
const fileInfo = await bot.telegram.getFile(file.telegramFileId);
|
2026-05-18 07:09:29 +07:00
|
|
|
|
|
|
|
|
const redirectUrl = `https://api.telegram.org/file/bot${process.env.BOT_TOKEN}/${fileInfo.file_path}`;
|
|
|
|
|
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-18 07:27:06 +07:00
|
|
|
} catch (error: any) {
|
2026-05-18 07:09:29 +07:00
|
|
|
logger.error('File redirect error', { public_id, error: error.message });
|
|
|
|
|
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-18 07:29:01 +07:00
|
|
|
const result = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(fileSchema)
|
|
|
|
|
.where(eq(fileSchema.publicId, public_id))
|
|
|
|
|
.limit(1);
|
2026-05-18 07:09:29 +07:00
|
|
|
|
|
|
|
|
if (!result.length) {
|
|
|
|
|
logger.warn('File not found', { public_id });
|
|
|
|
|
return Response.json({ error: 'File not found' }, { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const file = result[0];
|
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,
|
|
|
|
|
created_at:
|
|
|
|
|
typeof file.createdAt === 'string'
|
|
|
|
|
? file.createdAt
|
|
|
|
|
: (file.createdAt as Date).toISOString(),
|
|
|
|
|
},
|
|
|
|
|
{ status: 200 },
|
|
|
|
|
);
|
2026-05-18 07:27:06 +07:00
|
|
|
} catch (error: any) {
|
2026-05-18 07:09:29 +07:00
|
|
|
logger.error('File info error', { public_id, error: error.message });
|
|
|
|
|
return Response.json({ error: 'Server error' }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
};
|