add check for files that already exist
This commit is contained in:
parent
ce8c86f6da
commit
73491c9387
1 changed files with 10 additions and 3 deletions
13
index.js
13
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.');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue