server: if portfolio name already exists send it (closes #290431)

instead of creating a new one

authorSamuel Trégouët <samuel.tregouet@logilab.fr>
changeset115f8bb69aa0
branchdefault
phasepublic
hiddenno
parent revision#b17731194cfb xmosaicframe: display view menu as a buttons list (instead of dropdown)
child revision#401a1823e077 portfolio: move save button next to its friends (`add frame`, `add text`)
files modified by this revision
mosaicserver/routes.js
# HG changeset patch
# User Samuel Trégouët <samuel.tregouet@logilab.fr>
# Date 1428051197 -7200
# Fri Apr 03 10:53:17 2015 +0200
# Node ID 115f8bb69aa04781dcdb3dd504925f5db6734d46
# Parent b17731194cfbab2d5f329d23653a09bead3a2fc1
server: if portfolio name already exists send it (closes #290431)

instead of creating a new one

diff --git a/mosaicserver/routes.js b/mosaicserver/routes.js
@@ -28,20 +28,27 @@
1 
2  router.post('/newportfolio', function(req, res, next) {
3      var slug = req.body.name.replace(/[ \/]/g, '_'), // XXX sanitize
4          filename = path.join(__dirname, '/../portfolios/' + slug + '.html'),
5          template = path.join(__dirname, '/../portfolios/_template.html');
6 -    var istream = fs.createReadStream(template),
7 -        ostream = fs.createWriteStream(filename);
8 -    ostream.on('finish', function() {
9 -        res.redirect('/' + slug);
10 +
11 +    fs.exists(filename, function(exists) {
12 +        if (exists) {
13 +            res.redirect('/' + slug);
14 +        } else {
15 +            var istream = fs.createReadStream(template),
16 +                ostream = fs.createWriteStream(filename);
17 +            ostream.on('finish', function() {
18 +                res.redirect('/' + slug);
19 +            });
20 +            ostream.on('error', function () {
21 +                res.status(500).send('could not create resource');
22 +                next();
23 +            });
24 +            istream.pipe(ostream);
25 +        }
26      });
27 -    ostream.on('error', function () {
28 -        res.status(500).send('could not create resource');
29 -        next();
30 -    });
31 -    istream.pipe(ostream);
32  });
33 
34  router.get('/:slug', function(req, res, next) {
35      var filepath = path.join('portfolios/', req.params.slug + '.html');
36      console.log('filepath', filepath);