|
|
@@ -94,6 +94,13 @@ function checkBodyPropsExist(props) {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+function checkFilename(filename) {
|
|
|
+ const base = path.basename(filename);
|
|
|
+ let ext = path.extname(filename);
|
|
|
+ ext = ext ? ext.toLowerCase().substr(1) : ext;
|
|
|
+ return base && ['html', 'js', 'css'].indexOf( ext ) > -1;
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* Get repo parts
|
|
|
*/
|
|
|
@@ -131,11 +138,11 @@ app.get('/:repoSlug/:exampleSlug', getIndexExample);
|
|
|
|
|
|
function checkRepoExists(req, res, next) {
|
|
|
const { repoSlug } = req.params;
|
|
|
-console.log('### checkRepoExists', repoSlug);
|
|
|
+ console.log('### checkRepoExists', repoSlug);
|
|
|
// Get repo from store
|
|
|
req.repo = exStore.getRepo(repoSlug);
|
|
|
if(! req.repo) {
|
|
|
- res.status(404).send("Repo " + repoSlug + "not found");
|
|
|
+ return res.status(404).send("Repo " + repoSlug + "not found");
|
|
|
}
|
|
|
next();
|
|
|
}
|
|
|
@@ -181,19 +188,26 @@ app.post('/:repoSlug/examples/:exampleSlug/file',
|
|
|
if(! re.test(name)) {
|
|
|
return res.status(400).json('Le paramètre `name` est incorrect: caractères autorisés: lettres, chiffres, _, - et .' );
|
|
|
}
|
|
|
+ if( ! checkFilename(name)) {
|
|
|
+ return res.status(400).json("Le paramètre `name` est incorrect: il doit comporter un nom suivi d'une extension (.html, .js ou .css)" );
|
|
|
+ }
|
|
|
fs.statAsync(fullPath)
|
|
|
// Invert the usual flow of a Promise. fs.stat() fails if file does not exist (which is what we want)
|
|
|
// Hence .catch() is a success handler and .then() an error handler (has to rethrow)
|
|
|
+ .then(stats => {
|
|
|
+ throw new Error('Le fichier `' + name + '` existe déjà !');
|
|
|
+ })
|
|
|
.catch(err => {
|
|
|
+ // Rethrow error if it is not a "file not found" thrown by fs.stat()
|
|
|
+ if( ! err.message.startsWith('ENOENT') ) {
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
return fs.writeFileAsync(fullPath, '')
|
|
|
.then(() => res.json({
|
|
|
name,
|
|
|
path: fullPath
|
|
|
}));
|
|
|
})
|
|
|
- .then(stats => {
|
|
|
- throw new Error('Le fichier `' + name + '` existe déjà !');
|
|
|
- })
|
|
|
.catch(err => {
|
|
|
const statusCode = err.message.startsWith('Le fichier') ? 409 : 500;
|
|
|
return res.status(statusCode).send(err.message);
|