node.jsmongodbmongoosemultermulter-gridfs-storage

Multer gridfs Storage


I wanted to change my filename but when I examine console.log (req.file) I can see the filename I changed to, but a different filename is saved in the database using Multergridfs Storage.

 1. the default filename 
 const storage = new GridFsStorage({
    url: config.db,
    file: (req, file) => {
        return new Promise((resolve, reject) => {
            crypto.randomBytes(16, (err, buf) => {
                if (err) {
                    return reject(err)
                }
                const filename = 'file' + path.extname(file.originalname);
                const fileInfo = {
                    filename: filename,
                    bucketName: 'contents'
                };
                resolve(fileInfo);
            });
        });
    }});

2 this is where i edited the filename

router.post('/', upload.single('file'), (req, res) => {
    req.file.filename = req.body.fileName + path.extname(req.file.originalname)
    res.redirect('/upload/files')
    
    console.log(req.file)
});

the result of the console is something like

{ fieldname: 'file', originalname: '\'YOU CAN ALSO BE GREAT\' - Elon Musk Motivation - Motivational Video.mp4', encoding: '7bit', mimetype: 'video/mp4', id: 5bfb292c13eec142f6c20fd9, filename: 'a.mp4', metadata: null, bucketName: 'contents', chunkSize: 261120, size: 19372377, md5: '513c6220ef3afff644cf8a6dc4cd9130', uploadDate: 2018-11-25T22:58:52.625Z, contentType: 'video/mp4' } { fileName: 'a' }


Solution

  • This part in your code

    const storage = new GridFsStorage({
        url: config.db,
        file: (req, file) => { // In this function is where you configure the name of your file
    

    The file configuration is the one that computes the filename before inserting the file in the database. What you are doing is:

    1. Generating a name like 'file' plus whatever extension comes from the browser, eg: 'file.mp4'
    2. Saving a file with that name into the database
    3. Overwriting a property in your request with a new name
    4. The file in the database remains unchanged

    I think what you really wanted was to generate the correct name before inserting

    You can do this by using

    const storage = new GridFsStorage({
        url: config.db,
        file: (req, file) => {
            return new Promise((resolve, reject) => {
                crypto.randomBytes(16, (err, buf) => {
                    if (err) {
                        return reject(err)
                    }
                    // In here you have access to the request and also to the body object
                    const filename = req.body.fileName + path.extname(file.originalname);
                    const fileInfo = {
                        filename: filename,
                        bucketName: 'contents'
                    };
                    resolve(fileInfo);
                });
            });
        }});
    

    Make sure you send all the fields before the file from the form in your browser or some values will be undefined because they are not processed yet.