import Multer from 'multer';
import type { Request, Response } from 'express';

const maxSize = 10 * 1024 * 1024;

const processFile = Multer({
  storage: Multer.memoryStorage(),
  limits: { fileSize: maxSize },
}).single('file');

/** Promise wrapper around the multer single-file middleware. */
function processFileMiddleware(req: Request, res: Response): Promise<void> {
  return new Promise<void>((resolve, reject) => {
    processFile(req, res, (error) => {
      if (error) {
        reject(error instanceof Error ? error : new Error(String(error)));
        return;
      }
      resolve();
    });
  });
}

export default processFileMiddleware;
