add check for files that already exist

This commit is contained in:
hippoz 2020-10-02 23:03:59 +03:00
parent ce8c86f6da
commit 73491c9387

View file

@ -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.');
}
});
});