feat: extend file schema with archive metadata, implement batch upload processing, and add zip utilities for file handling
This commit is contained in:
+52
-18
@@ -4,6 +4,7 @@ import { formatCreatedAt, getErrorMessage } from '../utils/file';
|
||||
import logger from '../utils/logger';
|
||||
import { checkRateLimit } from '../utils/rateLimit';
|
||||
import { getBot } from '../utils/telegram';
|
||||
import { extractZipEntry } from '../utils/zip';
|
||||
|
||||
type RequestWithParams = Request & {
|
||||
params?: {
|
||||
@@ -11,6 +12,30 @@ type RequestWithParams = Request & {
|
||||
};
|
||||
};
|
||||
|
||||
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}`;
|
||||
|
||||
export const handleFileRedirect = async (req: RequestWithParams): Promise<Response> => {
|
||||
const public_id = req.params?.public_id;
|
||||
try {
|
||||
@@ -27,27 +52,36 @@ export const handleFileRedirect = async (req: RequestWithParams): Promise<Respon
|
||||
return Response.json({ error: 'File not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Check cache first
|
||||
const cacheKey = `file_info_${file.telegramFileId}`;
|
||||
let fileInfo = fileInfoCache.get(cacheKey);
|
||||
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));
|
||||
|
||||
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 });
|
||||
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),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const redirectUrl = `https://api.telegram.org/file/bot${process.env.BOT_TOKEN}/${fileInfo.file_path}`;
|
||||
const fileInfo = await getTelegramFileInfo(file.telegramFileId, public_id);
|
||||
const redirectUrl = buildTelegramFileUrl(fileInfo.file_path);
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers: {
|
||||
|
||||
+14
-60
@@ -1,9 +1,7 @@
|
||||
import { createReadStream, createWriteStream } from 'node:fs';
|
||||
import { createWriteStream } 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';
|
||||
import type { NewFile } from '../db/schema';
|
||||
import { config } from '../env';
|
||||
import {
|
||||
buildUploadResponse,
|
||||
@@ -15,12 +13,7 @@ import {
|
||||
getFileType,
|
||||
} from '../utils/file';
|
||||
import logger from '../utils/logger';
|
||||
import { forwardToStorage } from '../utils/telegram';
|
||||
|
||||
type UploadedFile = NewFile & {
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
};
|
||||
import { enqueuePreparedUpload, type PreparedUpload } from '../utils/uploadBatcher';
|
||||
|
||||
interface JsonUploadPayload {
|
||||
file?: unknown;
|
||||
@@ -46,13 +39,6 @@ const normalizeFileType = (mimeType: string, fileName: string): string => {
|
||||
const JSON_UPLOAD_LIMIT_BYTES = 50 * 1024 * 1024;
|
||||
const SIGNATURE_BYTES = 16;
|
||||
|
||||
type PreparedUpload = {
|
||||
tempPath: string;
|
||||
fileHash: string;
|
||||
sizeBytes: number;
|
||||
signatureBuffer: Buffer;
|
||||
};
|
||||
|
||||
const cleanupTempFile = async (tempPath: string): Promise<void> => {
|
||||
try {
|
||||
await unlink(tempPath);
|
||||
@@ -137,46 +123,6 @@ const writeBufferToTemp = async (fileBuffer: Buffer, fileHash: string): Promise<
|
||||
}
|
||||
};
|
||||
|
||||
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 {
|
||||
publicId: nanoid(),
|
||||
telegramFileId: result.telegramFileId,
|
||||
telegramFileUniqueId: result.telegramFileUniqueId,
|
||||
storageChatId: config.storageChatId,
|
||||
storageMessageId: result.storageMessageId,
|
||||
fileName,
|
||||
mimeType: mimeType || 'application/octet-stream',
|
||||
sizeBytes: prepared.sizeBytes,
|
||||
fileType,
|
||||
uploaderId: 0,
|
||||
fileHash: prepared.fileHash,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
} finally {
|
||||
await closeFileStream(fileStream);
|
||||
await cleanupTempFile(prepared.tempPath);
|
||||
}
|
||||
};
|
||||
|
||||
export const handleUpload = async (req: Request): Promise<Response> => {
|
||||
try {
|
||||
const contentType = req.headers.get('content-type') || '';
|
||||
@@ -230,8 +176,12 @@ const handleMultipartUpload = async (req: Request): Promise<Response> => {
|
||||
return Response.json({ error: `File size exceeds ${fileType} limit` }, { status: 400 });
|
||||
}
|
||||
|
||||
const uploaded = await performUpload(prepared, finalFileName, mimeType, fileType);
|
||||
await db.insert(fileSchema).values(uploaded);
|
||||
const uploaded = await enqueuePreparedUpload({
|
||||
prepared,
|
||||
fileName: finalFileName,
|
||||
mimeType,
|
||||
fileType,
|
||||
});
|
||||
|
||||
return Response.json(buildUploadResponse(uploaded, config.baseUrl), { status: 200 });
|
||||
} catch (error: unknown) {
|
||||
@@ -280,8 +230,12 @@ const handleJSONUpload = async (req: Request): Promise<Response> => {
|
||||
}
|
||||
|
||||
const prepared = await writeBufferToTemp(fileBytes, hash);
|
||||
const uploaded = await performUpload(prepared, finalFileName, mimeType, fileType);
|
||||
await db.insert(fileSchema).values(uploaded);
|
||||
const uploaded = await enqueuePreparedUpload({
|
||||
prepared,
|
||||
fileName: finalFileName,
|
||||
mimeType,
|
||||
fileType,
|
||||
});
|
||||
|
||||
return Response.json(buildUploadResponse(uploaded, config.baseUrl), { status: 200 });
|
||||
} catch (error: unknown) {
|
||||
|
||||
Reference in New Issue
Block a user