/*
 * Copyright (c) 2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */

import {Middleware} from "@decorators/express";
import {NextFunction, Request, Response} from "express";
import multer, {Multer, Field} from 'multer';

let instance : Multer | undefined;

export function useMulter() {
    if(typeof instance !== 'undefined') {
        return instance;
    }

    const storage = multer.memoryStorage();
    instance = multer({storage: storage, limits: {fieldSize: 25 * 1024 * 1024}});

    return instance;
}

export function createMulterFieldsMiddleware(fields: Field[]) {
    return class implements Middleware {
        public use(request: Request, response: Response, next: NextFunction) {
            return useMulter().fields(fields);
        }
    }
}





