|
@@ -94,11 +94,11 @@ function checkBodyPropsExist(props) {
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function checkFilename(filename) {
|
|
|
|
|
|
|
+function checkNameAndGetExt(filename) {
|
|
|
const base = path.basename(filename);
|
|
const base = path.basename(filename);
|
|
|
let ext = path.extname(filename);
|
|
let ext = path.extname(filename);
|
|
|
ext = ext ? ext.toLowerCase().substr(1) : ext;
|
|
ext = ext ? ext.toLowerCase().substr(1) : ext;
|
|
|
- return base && ['html', 'js', 'css'].indexOf( ext ) > -1;
|
|
|
|
|
|
|
+ return base && ['html', 'js', 'css'].indexOf( ext ) > -1 ? ext : false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -185,12 +185,19 @@ app.post('/:repoSlug/examples/:exampleSlug/file',
|
|
|
const re = /^[A-Za-z0-9_\-\.]+$/;
|
|
const re = /^[A-Za-z0-9_\-\.]+$/;
|
|
|
const targetDir = examplesDir + '/' + repoSlug + '/' + exampleSlug;
|
|
const targetDir = examplesDir + '/' + repoSlug + '/' + exampleSlug;
|
|
|
const fullPath = targetDir + '/' + name;
|
|
const fullPath = targetDir + '/' + name;
|
|
|
|
|
+ const defaultContents = {
|
|
|
|
|
+ html: '<!-- ' + name + ' -->',
|
|
|
|
|
+ js: '// ' + name,
|
|
|
|
|
+ css: '/* ' + name + ' */',
|
|
|
|
|
+ }
|
|
|
|
|
+ let fileExt;
|
|
|
if(! re.test(name)) {
|
|
if(! re.test(name)) {
|
|
|
return res.status(400).json('Le paramètre `name` est incorrect: caractères autorisés: lettres, chiffres, _, - et .' );
|
|
return res.status(400).json('Le paramètre `name` est incorrect: caractères autorisés: lettres, chiffres, _, - et .' );
|
|
|
}
|
|
}
|
|
|
- if( ! checkFilename(name)) {
|
|
|
|
|
|
|
+ if( ! ( fileExt = checkNameAndGetExt( 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)" );
|
|
return res.status(400).json("Le paramètre `name` est incorrect: il doit comporter un nom suivi d'une extension (.html, .js ou .css)" );
|
|
|
}
|
|
}
|
|
|
|
|
+ const fileContent = defaultContents[fileExt];
|
|
|
fs.statAsync(fullPath)
|
|
fs.statAsync(fullPath)
|
|
|
// Invert the usual flow of a Promise. fs.stat() fails if file does not exist (which is what we want)
|
|
// 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)
|
|
// Hence .catch() is a success handler and .then() an error handler (has to rethrow)
|
|
@@ -202,12 +209,15 @@ app.post('/:repoSlug/examples/:exampleSlug/file',
|
|
|
if( ! err.message.startsWith('ENOENT') ) {
|
|
if( ! err.message.startsWith('ENOENT') ) {
|
|
|
throw err;
|
|
throw err;
|
|
|
}
|
|
}
|
|
|
- return fs.writeFileAsync(fullPath, '')
|
|
|
|
|
- .then(() => res.json({
|
|
|
|
|
- name,
|
|
|
|
|
- path: fullPath
|
|
|
|
|
- }));
|
|
|
|
|
|
|
+ return fs.writeFileAsync(fullPath, fileContent);
|
|
|
})
|
|
})
|
|
|
|
|
+ .then(() => exStore.addExampleFile(repoSlug, exampleSlug, name))
|
|
|
|
|
+ .then(() => res.json({
|
|
|
|
|
+ name,
|
|
|
|
|
+ path: fullPath,
|
|
|
|
|
+ content: fileContent
|
|
|
|
|
+ })
|
|
|
|
|
+ )
|
|
|
.catch(err => {
|
|
.catch(err => {
|
|
|
const statusCode = err.message.startsWith('Le fichier') ? 409 : 500;
|
|
const statusCode = err.message.startsWith('Le fichier') ? 409 : 500;
|
|
|
return res.status(statusCode).send(err.message);
|
|
return res.status(statusCode).send(err.message);
|