From 73491c93871f97ff2e77807fb345f69bde4d4ecd Mon Sep 17 00:00:00 2001 From: hippoz Date: Fri, 2 Oct 2020 23:03:59 +0300 Subject: [PATCH] add check for files that already exist --- index.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 9f8648b..2e9f643 100644 --- a/index.js +++ b/index.js @@ -85,10 +85,17 @@ app.post('/api/upload', (req, res) => { } const file = req.files.file; + const filepath = `${config.storagePath}/${file.name}`; - file.mv(`${config.storagePath}/${file.name}`, function(err) { - if (err) return res.status(500).send(err); - res.render('uploaded', { file: { name: file.name } }); + path.exists(filepath, (exists) => { + if (!exists) { + file.mv(filepath, (err) => { + if (err) return res.status(500).send(err); + res.render('uploaded', { file: { name: file.name } }); + }); + } else { + return res.status(400).send('File exists.'); + } }); });