feat: enhance file upload handling with improved file detection, size limits, and response formatting
This commit is contained in:
+11
-25
@@ -1,5 +1,5 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { db, files as fileSchema } from '../db';
|
||||
import { findFileByPublicId } from '../db/files';
|
||||
import { formatCreatedAt, getErrorMessage } from '../utils/file';
|
||||
import logger from '../utils/logger';
|
||||
import { checkRateLimit } from '../utils/rateLimit';
|
||||
|
||||
@@ -18,18 +18,13 @@ export const handleFileRedirect = async (req: RequestWithParams): Promise<Respon
|
||||
return Response.json({ error: 'Rate limit exceeded' }, { status: 429 });
|
||||
}
|
||||
|
||||
const result = await db
|
||||
.select()
|
||||
.from(fileSchema)
|
||||
.where(eq(fileSchema.publicId, public_id))
|
||||
.limit(1);
|
||||
const file = await findFileByPublicId(public_id);
|
||||
|
||||
if (!result.length) {
|
||||
if (!file) {
|
||||
logger.warn('File not found', { public_id });
|
||||
return Response.json({ error: 'File not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const file = result[0];
|
||||
const { getBot } = await import('../utils/telegram');
|
||||
const bot = getBot();
|
||||
const fileInfo = await bot.telegram.getFile(file.telegramFileId);
|
||||
@@ -41,8 +36,8 @@ export const handleFileRedirect = async (req: RequestWithParams): Promise<Respon
|
||||
Location: redirectUrl,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
logger.error('File redirect error', { public_id, error: error.message });
|
||||
} catch (error: unknown) {
|
||||
logger.error('File redirect error', { public_id, error: getErrorMessage(error) });
|
||||
return Response.json({ error: 'Server error' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
@@ -54,18 +49,12 @@ export const handleFileInfo = async (req: RequestWithParams): Promise<Response>
|
||||
return Response.json({ error: 'Missing file id' }, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await db
|
||||
.select()
|
||||
.from(fileSchema)
|
||||
.where(eq(fileSchema.publicId, public_id))
|
||||
.limit(1);
|
||||
const file = await findFileByPublicId(public_id);
|
||||
|
||||
if (!result.length) {
|
||||
if (!file) {
|
||||
logger.warn('File not found', { public_id });
|
||||
return Response.json({ error: 'File not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const file = result[0];
|
||||
return Response.json(
|
||||
{
|
||||
public_id: file.publicId,
|
||||
@@ -74,15 +63,12 @@ export const handleFileInfo = async (req: RequestWithParams): Promise<Response>
|
||||
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(),
|
||||
created_at: formatCreatedAt(file.createdAt),
|
||||
},
|
||||
{ status: 200 },
|
||||
);
|
||||
} catch (error: any) {
|
||||
logger.error('File info error', { public_id, error: error.message });
|
||||
} catch (error: unknown) {
|
||||
logger.error('File info error', { public_id, error: getErrorMessage(error) });
|
||||
return Response.json({ error: 'Server error' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { sql } from 'drizzle-orm';
|
||||
import { db } from '../db';
|
||||
import { getErrorMessage } from '../utils/file';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
export const handleHealth = async (_req: Request): Promise<Response> => {
|
||||
try {
|
||||
await db.execute(sql`SELECT 1`);
|
||||
return Response.json({ status: 'ok' }, { status: 200 });
|
||||
} catch (error: any) {
|
||||
logger.error('Health check failed', { error: error.message });
|
||||
return Response.json({ status: 'error', error: error.message }, { status: 500 });
|
||||
} catch (error: unknown) {
|
||||
const message = getErrorMessage(error);
|
||||
logger.error('Health check failed', { error: message });
|
||||
return Response.json({ status: 'error', error: message }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
+43
-58
@@ -11,6 +11,45 @@ const jsonContent = (schema: object) => ({
|
||||
'application/json': { schema },
|
||||
});
|
||||
|
||||
const publicIdParameter = {
|
||||
name: 'public_id',
|
||||
in: 'path',
|
||||
required: true,
|
||||
description: 'Permanent public file ID.',
|
||||
schema: { type: 'string' },
|
||||
};
|
||||
|
||||
const fileInfoProperties = {
|
||||
public_id: { type: 'string', example: 'xYz123' },
|
||||
file_name: { type: 'string', example: 'document.pdf' },
|
||||
mime_type: { type: 'string', example: 'application/pdf' },
|
||||
size_bytes: { type: 'integer', example: 1048576 },
|
||||
file_type: { type: 'string', example: 'document' },
|
||||
uploader_id: { type: 'integer', example: 0 },
|
||||
created_at: {
|
||||
type: 'string',
|
||||
format: 'date-time',
|
||||
example: '2026-05-18T10:00:00.000Z',
|
||||
},
|
||||
};
|
||||
|
||||
const uploadProperties = {
|
||||
...fileInfoProperties,
|
||||
telegram_file_id: { type: 'string', example: 'BQACAgQAAxkBA...' },
|
||||
telegram_file_unique_id: { type: 'string', example: 'AgAD8w...' },
|
||||
storage_chat_id: { type: 'integer', example: -1001234567890 },
|
||||
storage_message_id: { type: 'integer', example: 42 },
|
||||
download_url: {
|
||||
type: 'string',
|
||||
example: `${config.baseUrl}/f/xYz123`,
|
||||
},
|
||||
};
|
||||
|
||||
const objectSchema = (properties: object) => ({
|
||||
type: 'object',
|
||||
properties,
|
||||
});
|
||||
|
||||
export const handleSwaggerJson = async (): Promise<Response> => {
|
||||
const spec = {
|
||||
openapi: '3.0.0',
|
||||
@@ -99,30 +138,7 @@ export const handleSwaggerJson = async (): Promise<Response> => {
|
||||
responses: {
|
||||
'200': {
|
||||
description: 'Successful upload metadata.',
|
||||
content: jsonContent({
|
||||
type: 'object',
|
||||
properties: {
|
||||
public_id: { type: 'string', example: 'xYz123' },
|
||||
telegram_file_id: { type: 'string', example: 'BQACAgQAAxkBA...' },
|
||||
telegram_file_unique_id: { type: 'string', example: 'AgAD8w...' },
|
||||
storage_chat_id: { type: 'integer', example: -1001234567890 },
|
||||
storage_message_id: { type: 'integer', example: 42 },
|
||||
file_name: { type: 'string', example: 'document.pdf' },
|
||||
mime_type: { type: 'string', example: 'application/pdf' },
|
||||
size_bytes: { type: 'integer', example: 1048576 },
|
||||
file_type: { type: 'string', example: 'document' },
|
||||
uploader_id: { type: 'integer', example: 0 },
|
||||
created_at: {
|
||||
type: 'string',
|
||||
format: 'date-time',
|
||||
example: '2026-05-18T10:00:00.000Z',
|
||||
},
|
||||
download_url: {
|
||||
type: 'string',
|
||||
example: `${config.baseUrl}/f/xYz123`,
|
||||
},
|
||||
},
|
||||
}),
|
||||
content: jsonContent(objectSchema(uploadProperties)),
|
||||
},
|
||||
'400': {
|
||||
description: 'Bad request.',
|
||||
@@ -140,15 +156,7 @@ export const handleSwaggerJson = async (): Promise<Response> => {
|
||||
summary: 'Redirect to Telegram File URL',
|
||||
description:
|
||||
'Gets a fresh Telegram download URL and redirects with 302. Rate-limited by IP.',
|
||||
parameters: [
|
||||
{
|
||||
name: 'public_id',
|
||||
in: 'path',
|
||||
required: true,
|
||||
description: 'Permanent public file ID.',
|
||||
schema: { type: 'string' },
|
||||
},
|
||||
],
|
||||
parameters: [publicIdParameter],
|
||||
responses: {
|
||||
'302': {
|
||||
description: 'Redirect to Telegram CDN URL.',
|
||||
@@ -180,34 +188,11 @@ export const handleSwaggerJson = async (): Promise<Response> => {
|
||||
get: {
|
||||
summary: 'Get File Info',
|
||||
description: 'Gets saved file metadata by public ID.',
|
||||
parameters: [
|
||||
{
|
||||
name: 'public_id',
|
||||
in: 'path',
|
||||
required: true,
|
||||
description: 'Permanent public file ID.',
|
||||
schema: { type: 'string' },
|
||||
},
|
||||
],
|
||||
parameters: [publicIdParameter],
|
||||
responses: {
|
||||
'200': {
|
||||
description: 'File metadata.',
|
||||
content: jsonContent({
|
||||
type: 'object',
|
||||
properties: {
|
||||
public_id: { type: 'string', example: 'xYz123' },
|
||||
file_name: { type: 'string', example: 'document.pdf' },
|
||||
mime_type: { type: 'string', example: 'application/pdf' },
|
||||
size_bytes: { type: 'integer', example: 1048576 },
|
||||
file_type: { type: 'string', example: 'document' },
|
||||
uploader_id: { type: 'integer', example: 0 },
|
||||
created_at: {
|
||||
type: 'string',
|
||||
format: 'date-time',
|
||||
example: '2026-05-18T10:00:00.000Z',
|
||||
},
|
||||
},
|
||||
}),
|
||||
content: jsonContent(objectSchema(fileInfoProperties)),
|
||||
},
|
||||
'400': {
|
||||
description: 'Missing public ID.',
|
||||
|
||||
+97
-181
@@ -1,18 +1,89 @@
|
||||
import { createReadStream, unlinkSync } from 'node:fs';
|
||||
import { eq } from 'drizzle-orm';
|
||||
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,
|
||||
checkFileSize,
|
||||
computeHash,
|
||||
ensureExtension,
|
||||
extractMimeType,
|
||||
getErrorMessage,
|
||||
getFileType,
|
||||
} from '../utils/file';
|
||||
import logger from '../utils/logger';
|
||||
import { forwardToStorage, getBot } from '../utils/telegram';
|
||||
|
||||
type UploadedFile = NewFile & {
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
type TelegramFileLookup = {
|
||||
mime_type?: string;
|
||||
file_size?: number;
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
const performUpload = async (
|
||||
fileBuffer: Buffer,
|
||||
fileName: string,
|
||||
mimeType: string,
|
||||
): Promise<UploadedFile> => {
|
||||
const tempPath = `/tmp/teleuploader-${nanoid()}`;
|
||||
try {
|
||||
await Bun.write(tempPath, fileBuffer);
|
||||
const fileStream = createReadStream(tempPath);
|
||||
const result = await forwardToStorage(fileStream, fileName, getFileType(mimeType, fileName));
|
||||
const bot = getBot();
|
||||
const fileInfo = (await bot.telegram.getFile(result.telegramFileId)) as TelegramFileLookup;
|
||||
|
||||
return {
|
||||
publicId: nanoid(),
|
||||
telegramFileId: result.telegramFileId,
|
||||
telegramFileUniqueId: result.telegramFileUniqueId,
|
||||
storageChatId: config.storageChatId,
|
||||
storageMessageId: result.storageMessageId,
|
||||
fileName,
|
||||
mimeType: fileInfo.mime_type || mimeType || 'application/octet-stream',
|
||||
sizeBytes: fileInfo.file_size || fileBuffer.byteLength,
|
||||
fileType: getFileType(mimeType, fileName),
|
||||
uploaderId: 0,
|
||||
fileHash: computeHash(fileBuffer),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
} finally {
|
||||
setTimeout(() => {
|
||||
try {
|
||||
unlinkSync(tempPath);
|
||||
} catch {}
|
||||
}, 50);
|
||||
}
|
||||
};
|
||||
|
||||
export const handleUpload = async (req: Request): Promise<Response> => {
|
||||
try {
|
||||
const contentType = req.headers.get('content-type') || '';
|
||||
@@ -27,14 +98,14 @@ export const handleUpload = async (req: Request): Promise<Response> => {
|
||||
{ error: 'Unsupported content type. Use multipart/form-data or application/json' },
|
||||
{ status: 400 },
|
||||
);
|
||||
} catch (error: any) {
|
||||
logger.error('Upload error', { error: error.message });
|
||||
return Response.json({ error: error.message }, { status: 500 });
|
||||
} catch (error: unknown) {
|
||||
const message = getErrorMessage(error);
|
||||
logger.error('Upload error', { error: message });
|
||||
return Response.json({ error: message }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
const handleMultipartUpload = async (req: Request): Promise<Response> => {
|
||||
let tempPath = '';
|
||||
try {
|
||||
const formData = await req.formData();
|
||||
const file = formData.get('file');
|
||||
@@ -49,33 +120,9 @@ const handleMultipartUpload = async (req: Request): Promise<Response> => {
|
||||
const fileBuffer = Buffer.from(fileBytes);
|
||||
const hash = computeHash(fileBuffer);
|
||||
|
||||
// Check for duplicate in DB
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(fileSchema)
|
||||
.where(eq(fileSchema.fileHash, hash))
|
||||
.limit(1);
|
||||
|
||||
if (existing.length > 0) {
|
||||
const existingFile = existing[0];
|
||||
const responsePayload = {
|
||||
public_id: existingFile.publicId,
|
||||
telegram_file_id: existingFile.telegramFileId,
|
||||
telegram_file_unique_id: existingFile.telegramFileUniqueId,
|
||||
storage_chat_id: existingFile.storageChatId,
|
||||
storage_message_id: existingFile.storageMessageId,
|
||||
file_name: existingFile.fileName,
|
||||
mime_type: existingFile.mimeType,
|
||||
size_bytes: existingFile.sizeBytes,
|
||||
file_type: existingFile.fileType,
|
||||
uploader_id: existingFile.uploaderId,
|
||||
created_at:
|
||||
existingFile.createdAt instanceof Date
|
||||
? existingFile.createdAt.toISOString()
|
||||
: new Date(existingFile.createdAt).toISOString(),
|
||||
download_url: `${config.baseUrl}/f/${existingFile.publicId}`,
|
||||
};
|
||||
return Response.json(responsePayload, { status: 200 });
|
||||
const existingFile = await findFileByHash(hash);
|
||||
if (existingFile) {
|
||||
return Response.json(buildUploadResponse(existingFile, config.baseUrl), { status: 200 });
|
||||
}
|
||||
|
||||
const rawMimeType = file.type || extractMimeType({}, req) || 'application/octet-stream';
|
||||
@@ -90,68 +137,20 @@ const handleMultipartUpload = async (req: Request): Promise<Response> => {
|
||||
return Response.json({ error: `File size exceeds ${fileType} limit` }, { status: 400 });
|
||||
}
|
||||
|
||||
// Write file to disk temporarily
|
||||
tempPath = `/tmp/teleuploader-${nanoid()}`;
|
||||
await Bun.write(tempPath, fileBuffer);
|
||||
|
||||
const fileStream = createReadStream(tempPath);
|
||||
const result = await forwardToStorage(fileStream, finalFileName, fileType);
|
||||
const bot = getBot();
|
||||
const fileInfo = (await bot.telegram.getFile(result.telegramFileId)) as any;
|
||||
|
||||
const uploaded = {
|
||||
publicId: nanoid(),
|
||||
telegramFileId: result.telegramFileId,
|
||||
telegramFileUniqueId: result.telegramFileUniqueId,
|
||||
storageChatId: config.storageChatId,
|
||||
storageMessageId: result.storageMessageId,
|
||||
fileName: finalFileName,
|
||||
mimeType: fileInfo.mime_type || mimeType || 'application/octet-stream',
|
||||
sizeBytes: fileInfo.file_size || fileBuffer.byteLength,
|
||||
fileType: fileType,
|
||||
uploaderId: 0,
|
||||
fileHash: hash,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const uploaded = await performUpload(fileBuffer, finalFileName, mimeType);
|
||||
await db.insert(fileSchema).values(uploaded);
|
||||
|
||||
const responsePayload = {
|
||||
public_id: uploaded.publicId,
|
||||
telegram_file_id: uploaded.telegramFileId,
|
||||
telegram_file_unique_id: uploaded.telegramFileUniqueId,
|
||||
storage_chat_id: uploaded.storageChatId,
|
||||
storage_message_id: uploaded.storageMessageId,
|
||||
file_name: uploaded.fileName,
|
||||
mime_type: uploaded.mimeType,
|
||||
size_bytes: uploaded.sizeBytes,
|
||||
file_type: uploaded.fileType,
|
||||
uploader_id: uploaded.uploaderId,
|
||||
created_at: uploaded.createdAt.toISOString(),
|
||||
download_url: `${config.baseUrl}/f/${uploaded.publicId}`,
|
||||
};
|
||||
|
||||
return Response.json(responsePayload, { status: 200 });
|
||||
} catch (error: any) {
|
||||
logger.error('Multipart upload error', { error: error.message });
|
||||
return Response.json({ error: error.message }, { status: 500 });
|
||||
} finally {
|
||||
if (tempPath) {
|
||||
const p = tempPath;
|
||||
setTimeout(() => {
|
||||
try {
|
||||
unlinkSync(p);
|
||||
} catch {}
|
||||
}, 50);
|
||||
}
|
||||
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 });
|
||||
}
|
||||
};
|
||||
|
||||
const handleJSONUpload = async (req: Request): Promise<Response> => {
|
||||
let tempPath = '';
|
||||
try {
|
||||
const { file, fileName = 'file' } = (await req.json()) as any;
|
||||
const { file, fileName = 'file' } = (await req.json()) as JsonUploadPayload;
|
||||
|
||||
if (!file || typeof file !== 'string') {
|
||||
return Response.json(
|
||||
@@ -160,112 +159,29 @@ const handleJSONUpload = async (req: Request): Promise<Response> => {
|
||||
);
|
||||
}
|
||||
|
||||
let base64Data = file;
|
||||
let rawMimeType = 'application/octet-stream';
|
||||
if (file.startsWith('data:')) {
|
||||
const match = file.match(/^data:([^;]+);base64,(.+)$/);
|
||||
if (match) {
|
||||
rawMimeType = match[1];
|
||||
base64Data = match[2];
|
||||
}
|
||||
}
|
||||
|
||||
const { base64Data, mimeType: rawMimeType } = parseBase64File(file);
|
||||
const fileBytes = Buffer.from(base64Data, 'base64');
|
||||
const hash = computeHash(fileBytes);
|
||||
|
||||
// Check for duplicate in DB
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(fileSchema)
|
||||
.where(eq(fileSchema.fileHash, hash))
|
||||
.limit(1);
|
||||
|
||||
if (existing.length > 0) {
|
||||
const existingFile = existing[0];
|
||||
const responsePayload = {
|
||||
public_id: existingFile.publicId,
|
||||
telegram_file_id: existingFile.telegramFileId,
|
||||
telegram_file_unique_id: existingFile.telegramFileUniqueId,
|
||||
storage_chat_id: existingFile.storageChatId,
|
||||
storage_message_id: existingFile.storageMessageId,
|
||||
file_name: existingFile.fileName,
|
||||
mime_type: existingFile.mimeType,
|
||||
size_bytes: existingFile.sizeBytes,
|
||||
file_type: existingFile.fileType,
|
||||
uploader_id: existingFile.uploaderId,
|
||||
created_at:
|
||||
existingFile.createdAt instanceof Date
|
||||
? existingFile.createdAt.toISOString()
|
||||
: new Date(existingFile.createdAt).toISOString(),
|
||||
download_url: `${config.baseUrl}/f/${existingFile.publicId}`,
|
||||
};
|
||||
return Response.json(responsePayload, { status: 200 });
|
||||
const existingFile = await findFileByHash(hash);
|
||||
if (existingFile) {
|
||||
return Response.json(buildUploadResponse(existingFile, config.baseUrl), { status: 200 });
|
||||
}
|
||||
|
||||
const { fileName: finalFileName, mimeType } = ensureExtension(fileName, fileBytes, rawMimeType);
|
||||
const fileType =
|
||||
getFileType(mimeType, finalFileName) === 'application'
|
||||
? 'document'
|
||||
: getFileType(mimeType, finalFileName);
|
||||
const fileType = normalizeFileType(mimeType, finalFileName);
|
||||
|
||||
if (!checkFileSize(fileBytes.byteLength, fileType)) {
|
||||
return Response.json({ error: `File size exceeds ${fileType} limit` }, { status: 400 });
|
||||
}
|
||||
|
||||
// Write file to disk temporarily
|
||||
tempPath = `/tmp/teleuploader-${nanoid()}`;
|
||||
await Bun.write(tempPath, fileBytes);
|
||||
|
||||
const fileStream = createReadStream(tempPath);
|
||||
const result = await forwardToStorage(fileStream, finalFileName, fileType);
|
||||
const bot = getBot();
|
||||
const fileInfo = (await bot.telegram.getFile(result.telegramFileId)) as any;
|
||||
|
||||
const uploaded = {
|
||||
publicId: nanoid(),
|
||||
telegramFileId: result.telegramFileId,
|
||||
telegramFileUniqueId: result.telegramFileUniqueId,
|
||||
storageChatId: config.storageChatId,
|
||||
storageMessageId: result.storageMessageId,
|
||||
fileName: finalFileName,
|
||||
mimeType: fileInfo.mime_type || mimeType || 'application/octet-stream',
|
||||
sizeBytes: fileInfo.file_size || fileBytes.byteLength,
|
||||
fileType: fileType,
|
||||
uploaderId: 0,
|
||||
fileHash: hash,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const uploaded = await performUpload(fileBytes, finalFileName, mimeType);
|
||||
await db.insert(fileSchema).values(uploaded);
|
||||
|
||||
const responsePayload = {
|
||||
public_id: uploaded.publicId,
|
||||
telegram_file_id: uploaded.telegramFileId,
|
||||
telegram_file_unique_id: uploaded.telegramFileUniqueId,
|
||||
storage_chat_id: uploaded.storageChatId,
|
||||
storage_message_id: uploaded.storageMessageId,
|
||||
file_name: uploaded.fileName,
|
||||
mime_type: uploaded.mimeType,
|
||||
size_bytes: uploaded.sizeBytes,
|
||||
file_type: uploaded.fileType,
|
||||
uploader_id: uploaded.uploaderId,
|
||||
created_at: uploaded.createdAt.toISOString(),
|
||||
download_url: `${config.baseUrl}/f/${uploaded.publicId}`,
|
||||
};
|
||||
|
||||
return Response.json(responsePayload, { status: 200 });
|
||||
} catch (error: any) {
|
||||
logger.error('JSON upload error', { error: error.message });
|
||||
return Response.json({ error: error.message }, { status: 500 });
|
||||
} finally {
|
||||
if (tempPath) {
|
||||
const p = tempPath;
|
||||
setTimeout(() => {
|
||||
try {
|
||||
unlinkSync(p);
|
||||
} catch {}
|
||||
}, 50);
|
||||
}
|
||||
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 });
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user