Node JS使用Multer缓慢上传

时间:2018-04-27 20:05:12

标签: javascript node.js mongodb docker multer

我正在使用Docker实现一个带有实例(我的测试服务器)的项目。

我实际上有一个客户端(上传应用程序的前端)和一个带有Node JS和multer存储以及Google云端存储的API。

我的数据库是3个容器MongoDB的副本集(1个主容器和2个辅助容器)。

问题是在上传过程中,过程很长(POST 17秒)。

我的策略是将文件的信息(png,pdf,jpeg ...)存储在我的mongoDB数据库中,并将文件存储在安装到我的api容器的文件系统中。 如果文件是图像,那么我们将其大小调整为thumbmail并将其存储在文件系统中,更新mongodb中的文档路径,将文件上传到Google Cloud Storage(原始大小)并将其删除到本地。

但是我的代码所有这个过程都很长。

// Enable Storage
const gcs = gcloud.storage({
    projectId: 'myprojectid',
    keyFilename: 'mykeyfilename',
});

// environments Variables
let clientUrl;
let smtpTransport;
let bucket;


 bucket = gcs.bucket('mybucket');

 module.exports = {

    async create(req, res) {
        const upload = multer({
            storage: multer.diskStorage({
                destination: (req, file, callback) => {
                    const client = JSON.parse(req.body.client);
                    const companyName = client.company_name;
                    const type = file.mimetype.split('/')[1];
                    const path = `./uploads/${companyName}/${type}`;
                    fs.mkdirsSync(path);
                    callback(null, path);
                },
                filename: (req, file, callback) => {
                    // originalname is the uploaded file's name with extn
                    callback(null, `${Date.now()}_${file.originalname}`);
                },
            }),
            limits: {
                fileSize: 500000000, // 500mb max
            },
        }).array('document');

        try {
            await upload(req, res, async () => {
                let addedBy;
                const docs = [];

                const userTypeJwt = extractTypeUserJwt(req);

                if (userTypeJwt === 'admins') {
                    addedBy = 'admin';
                } else {
                    addedBy = 'client';
                }

                const client = JSON.parse(req.body.client);

                req.files.forEach(async (file) => {
                    try {
                        const doc = await Document.create({
                            filename: file.originalname,
                            client: {
                                client_id: client._id,
                                company_name: client.company_name,
                                firstname: client.firstname,
                                lastname: client.lastname,
                                email: client.email,
                            },
                            added_by: addedBy,
                            mimetype: file.mimetype,
                            path: file.path,
                            size: file.size,
                        });
                        docs.push(doc);

                        /**
                         * Resizing de l'image originel
                         *
                         * Puis update path du document dans la base de donnée
                         */
                        const resizedPath = await imgProc(file, client.company_name);
                        await doc.update({
                            path: resizedPath,
                        });

                        /**
                         * Vérification image ?
                         *
                         * Si oui, upload de l'image originel sur le google cloud storage
                         *
                         * Suppression du fichier origine sur le local
                         */
                        if (file.mimetype.split('/')[0] === 'image') {
                            await bucket.upload(file.path);
                            console.log('File uploaded to Google cloud Storage');
                            await fs.unlink(file.path);
                            console.log('File deleted from local');
                        }
                    } catch (err) {
                        console.log(err);
                    }
                });

                if (addedBy === 'client') {
                    const mailOptions = {
                        to: config.contact_email,
                        from: config.contact_email,
                        subject: `${client.company_name} a uploadé dans son Espace`,
                        template: 'client_upload',
                        context: {
                            company_name: client.company_name,
                            docs,
                        },
                    };

                    try {
                        await smtpTransport.use('compile', mailerhbs({
                            viewPath: 'templates/emails',
                            extName: '.hbs',
                        }));
                        await smtpTransport.sendMail(mailOptions);
                        console.log('Send mail to mysmail@mail.com...');
                    } catch (err) {
                        console.log(err);
                    }
                }

                res.json({ success: true, message: 'Document(s) successfuly uploaded.' });
            });
        } catch (err) {
            console.log(err);
        }
    },

0 个答案:

没有答案