35 lines
865 B
TypeScript
35 lines
865 B
TypeScript
import type { Request, Response } from 'express';
|
|
import multer from 'multer';
|
|
|
|
import type { FileUploadProcessor } from '../types/index.ts';
|
|
|
|
const processFile = multer({
|
|
storage: multer.memoryStorage(),
|
|
}).single('file');
|
|
|
|
function formatUnknownError(error: unknown): string {
|
|
if (typeof error === 'string') return error;
|
|
if (typeof error === 'number') return `${error}`;
|
|
if (typeof error === 'boolean') return `${error}`;
|
|
return 'File upload failed';
|
|
}
|
|
|
|
const processFileMiddleware: FileUploadProcessor = (
|
|
req: Request,
|
|
res: Response,
|
|
) =>
|
|
new Promise((resolve, reject) => {
|
|
processFile(req, res, (error: unknown) => {
|
|
if (error) {
|
|
reject(
|
|
error instanceof Error ? error : new Error(formatUnknownError(error)),
|
|
);
|
|
return;
|
|
}
|
|
|
|
resolve();
|
|
});
|
|
});
|
|
|
|
export default processFileMiddleware;
|