2026-05-22 00:29:54 +07:00
|
|
|
import { createReadStream, createWriteStream } from 'node:fs';
|
2026-05-21 23:31:49 +07:00
|
|
|
import { unlink } from 'node:fs/promises';
|
2026-05-18 07:08:13 +07:00
|
|
|
import { nanoid } from 'nanoid';
|
2026-05-18 07:29:01 +07:00
|
|
|
import { db, files as fileSchema } from '../db';
|
2026-05-21 22:31:55 +07:00
|
|
|
import { findFileByHash } from '../db/files';
|
|
|
|
|
import type { NewFile } from '../db/schema';
|
2026-05-18 07:27:06 +07:00
|
|
|
import { config } from '../env';
|
2026-05-18 19:41:51 +07:00
|
|
|
import {
|
2026-05-21 22:31:55 +07:00
|
|
|
buildUploadResponse,
|
2026-05-18 19:41:51 +07:00
|
|
|
checkFileSize,
|
|
|
|
|
computeHash,
|
|
|
|
|
ensureExtension,
|
|
|
|
|
extractMimeType,
|
2026-05-21 22:31:55 +07:00
|
|
|
getErrorMessage,
|
2026-05-18 19:41:51 +07:00
|
|
|
getFileType,
|
|
|
|
|
} from '../utils/file';
|
2026-05-18 07:29:01 +07:00
|
|
|
import logger from '../utils/logger';
|
2026-05-21 22:55:42 +07:00
|
|
|
import { forwardToStorage } from '../utils/telegram';
|
2026-05-18 07:08:13 +07:00
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
type UploadedFile = NewFile & {
|
|
|
|
|
createdAt: Date;
|
|
|
|
|
updatedAt: Date;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface JsonUploadPayload {
|
|
|
|
|
file?: unknown;
|
|
|
|
|
fileName?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parseBase64File = (file: string): { base64Data: string; mimeType: string } => {
|
|
|
|
|
if (!file.startsWith('data:')) {
|
|
|
|
|
return { base64Data: file, mimeType: 'application/octet-stream' };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const match = file.match(/^data:([^;]+);base64,(.+)$/);
|
|
|
|
|
return match
|
|
|
|
|
? { base64Data: match[2], mimeType: match[1] }
|
|
|
|
|
: { base64Data: file, mimeType: 'application/octet-stream' };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const normalizeFileType = (mimeType: string, fileName: string): string => {
|
|
|
|
|
const fileType = getFileType(mimeType, fileName);
|
|
|
|
|
return fileType === 'application' ? 'document' : fileType;
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-22 00:29:54 +07:00
|
|
|
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);
|
|
|
|
|
} 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> => {
|
2026-05-21 22:31:55 +07:00
|
|
|
const tempPath = `/tmp/teleuploader-${nanoid()}`;
|
|
|
|
|
try {
|
|
|
|
|
await Bun.write(tempPath, fileBuffer);
|
2026-05-22 00:29:54 +07:00
|
|
|
return {
|
|
|
|
|
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);
|
2026-05-21 22:31:55 +07:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
publicId: nanoid(),
|
|
|
|
|
telegramFileId: result.telegramFileId,
|
|
|
|
|
telegramFileUniqueId: result.telegramFileUniqueId,
|
|
|
|
|
storageChatId: config.storageChatId,
|
|
|
|
|
storageMessageId: result.storageMessageId,
|
|
|
|
|
fileName,
|
2026-05-21 22:55:42 +07:00
|
|
|
mimeType: mimeType || 'application/octet-stream',
|
2026-05-22 00:29:54 +07:00
|
|
|
sizeBytes: prepared.sizeBytes,
|
|
|
|
|
fileType,
|
2026-05-21 22:31:55 +07:00
|
|
|
uploaderId: 0,
|
2026-05-22 00:29:54 +07:00
|
|
|
fileHash: prepared.fileHash,
|
2026-05-21 22:31:55 +07:00
|
|
|
createdAt: new Date(),
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
};
|
|
|
|
|
} finally {
|
2026-05-22 00:29:54 +07:00
|
|
|
await closeFileStream(fileStream);
|
|
|
|
|
await cleanupTempFile(prepared.tempPath);
|
2026-05-21 22:31:55 +07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-18 07:27:06 +07:00
|
|
|
export const handleUpload = async (req: Request): Promise<Response> => {
|
2026-05-18 07:08:13 +07:00
|
|
|
try {
|
|
|
|
|
const contentType = req.headers.get('content-type') || '';
|
|
|
|
|
|
|
|
|
|
if (contentType.includes('multipart/form-data')) {
|
|
|
|
|
return handleMultipartUpload(req);
|
|
|
|
|
} else if (contentType.includes('application/json')) {
|
|
|
|
|
return handleJSONUpload(req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Response.json(
|
|
|
|
|
{ error: 'Unsupported content type. Use multipart/form-data or application/json' },
|
2026-05-18 07:29:01 +07:00
|
|
|
{ status: 400 },
|
2026-05-18 07:08:13 +07:00
|
|
|
);
|
2026-05-21 22:31:55 +07:00
|
|
|
} catch (error: unknown) {
|
|
|
|
|
const message = getErrorMessage(error);
|
|
|
|
|
logger.error('Upload error', { error: message });
|
|
|
|
|
return Response.json({ error: message }, { status: 500 });
|
2026-05-18 07:08:13 +07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-18 07:27:06 +07:00
|
|
|
const handleMultipartUpload = async (req: Request): Promise<Response> => {
|
2026-05-18 07:08:13 +07:00
|
|
|
try {
|
|
|
|
|
const formData = await req.formData();
|
|
|
|
|
const file = formData.get('file');
|
2026-05-18 07:29:01 +07:00
|
|
|
const fileName =
|
|
|
|
|
(formData.get('fileName') as string) || (file instanceof File ? file.name : null) || 'file';
|
2026-05-18 07:08:13 +07:00
|
|
|
|
|
|
|
|
if (!file || !(file instanceof File)) {
|
|
|
|
|
return Response.json({ error: 'No file provided' }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 00:29:54 +07:00
|
|
|
const prepared = await streamFileToTemp(file);
|
2026-05-18 19:39:55 +07:00
|
|
|
|
2026-05-22 00:29:54 +07:00
|
|
|
const existingFile = await findFileByHash(prepared.fileHash);
|
2026-05-21 22:31:55 +07:00
|
|
|
if (existingFile) {
|
2026-05-22 00:29:54 +07:00
|
|
|
await cleanupTempFile(prepared.tempPath);
|
2026-05-21 22:31:55 +07:00
|
|
|
return Response.json(buildUploadResponse(existingFile, config.baseUrl), { status: 200 });
|
2026-05-18 19:39:55 +07:00
|
|
|
}
|
|
|
|
|
|
2026-05-18 09:20:03 +07:00
|
|
|
const rawMimeType = file.type || extractMimeType({}, req) || 'application/octet-stream';
|
|
|
|
|
const { fileName: finalFileName, mimeType } = ensureExtension(
|
|
|
|
|
fileName,
|
2026-05-22 00:29:54 +07:00
|
|
|
prepared.signatureBuffer,
|
2026-05-18 09:20:03 +07:00
|
|
|
rawMimeType,
|
|
|
|
|
);
|
|
|
|
|
const fileType = getFileType(mimeType, finalFileName);
|
2026-05-18 07:08:13 +07:00
|
|
|
|
2026-05-22 00:29:54 +07:00
|
|
|
if (!checkFileSize(prepared.sizeBytes, fileType)) {
|
|
|
|
|
await cleanupTempFile(prepared.tempPath);
|
2026-05-18 07:08:13 +07:00
|
|
|
return Response.json({ error: `File size exceeds ${fileType} limit` }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 00:29:54 +07:00
|
|
|
const uploaded = await performUpload(prepared, finalFileName, mimeType, fileType);
|
2026-05-18 07:08:13 +07:00
|
|
|
await db.insert(fileSchema).values(uploaded);
|
|
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
return Response.json(buildUploadResponse(uploaded, config.baseUrl), { status: 200 });
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
const message = getErrorMessage(error);
|
|
|
|
|
logger.error('Multipart upload error', { error: message });
|
|
|
|
|
return Response.json({ error: message }, { status: 500 });
|
2026-05-18 07:08:13 +07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-18 07:27:06 +07:00
|
|
|
const handleJSONUpload = async (req: Request): Promise<Response> => {
|
2026-05-18 07:08:13 +07:00
|
|
|
try {
|
2026-05-21 22:31:55 +07:00
|
|
|
const { file, fileName = 'file' } = (await req.json()) as JsonUploadPayload;
|
2026-05-18 07:08:13 +07:00
|
|
|
|
|
|
|
|
if (!file || typeof file !== 'string') {
|
|
|
|
|
return Response.json(
|
|
|
|
|
{ error: 'Invalid JSON. Must include "file" (base64) and optional "fileName"' },
|
2026-05-18 07:29:01 +07:00
|
|
|
{ status: 400 },
|
2026-05-18 07:08:13 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
const { base64Data, mimeType: rawMimeType } = parseBase64File(file);
|
2026-05-22 00:29:54 +07:00
|
|
|
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 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 09:20:03 +07:00
|
|
|
const fileBytes = Buffer.from(base64Data, 'base64');
|
2026-05-18 19:39:55 +07:00
|
|
|
const hash = computeHash(fileBytes);
|
|
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
const existingFile = await findFileByHash(hash);
|
|
|
|
|
if (existingFile) {
|
|
|
|
|
return Response.json(buildUploadResponse(existingFile, config.baseUrl), { status: 200 });
|
2026-05-18 19:39:55 +07:00
|
|
|
}
|
|
|
|
|
|
2026-05-18 09:20:03 +07:00
|
|
|
const { fileName: finalFileName, mimeType } = ensureExtension(fileName, fileBytes, rawMimeType);
|
2026-05-21 22:31:55 +07:00
|
|
|
const fileType = normalizeFileType(mimeType, finalFileName);
|
2026-05-18 07:08:13 +07:00
|
|
|
|
|
|
|
|
if (!checkFileSize(fileBytes.byteLength, fileType)) {
|
|
|
|
|
return Response.json({ error: `File size exceeds ${fileType} limit` }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 00:29:54 +07:00
|
|
|
const prepared = await writeBufferToTemp(fileBytes, hash);
|
|
|
|
|
const uploaded = await performUpload(prepared, finalFileName, mimeType, fileType);
|
2026-05-18 07:08:13 +07:00
|
|
|
await db.insert(fileSchema).values(uploaded);
|
|
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
return Response.json(buildUploadResponse(uploaded, config.baseUrl), { status: 200 });
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
const message = getErrorMessage(error);
|
|
|
|
|
logger.error('JSON upload error', { error: message });
|
|
|
|
|
return Response.json({ error: message }, { status: 500 });
|
2026-05-18 07:08:13 +07:00
|
|
|
}
|
|
|
|
|
};
|