#How to use FilesInterceptor in Fastify platform

5 messages · Page 1 of 1 (latest)

prisma seal
#

This is my code when i use express platform

export const MediaInterceptor = (fieldName: string) => {
    return FileInterceptor(fieldName, {
        fileFilter: (req, file, callback) => {
            const imageRegex = /\.(jpg|jpeg|png|webp)$/;
            const gifRegex = /\.gif$/;
            const videoRegex = /\.mp4$/;

            // Check file size limits based on type
            if (
                imageRegex.test(file.originalname) &&
                file.size > 5 * 1024 * 1024
            ) {
                return callback(
                    new BadRequestException({
                        message: "Image files must be less than 5MB!",
                    }),
                    false,
                );
            }

            if (
                gifRegex.test(file.originalname) &&
                file.size > 10 * 1024 * 1024
            ) {
                return callback(
                    new BadRequestException({
                        message: "GIF files must be less than 10MB!",
                    }),
                    false,
                );
            }

            // Check allowed file types
            if (
                !imageRegex.test(file.originalname) &&
                !gifRegex.test(file.originalname)
            ) {
                return callback(
                    new BadRequestException({
                        message:
                            "Only image, GIF, and video files are allowed!",
                    }),
                    false,
                );
            }

            callback(null, true);
        },
    });
};

And i can't import FileInterceptor into this file

mighty nimbus
#

FileInterceptor uses multer which is an express middleware. I have @nest-lab/fastify-multer which kind of works, but after some updates recently it's not perfect and I need to find time to just implement a @nest-lab/busboy to handle both express and fastify at a lower level

mighty nimbus
#

What do you mean?

prisma seal