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,11 +85,18 @@ 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) {
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.');
}
});
});
app.listen(config.server.port);