Commit df9a6468 authored by Damien Armillon's avatar Damien Armillon
Browse files

(feature): change pdf endpoint

parent 481e3e77
/node_modules /node_modules
.env.json .env.json
/private /private
pdf/*
!pdf/.gitkeep
img/*
!img/.gitkeep
...@@ -6,13 +6,15 @@ ...@@ -6,13 +6,15 @@
"author": "Damien <damien.armillon@gmail.com>", "author": "Damien <damien.armillon@gmail.com>",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"lint": "yarn eslint ." "start": "yarn nodemon app.js",
"lint": "yarn eslint ."
}, },
"dependencies": { "dependencies": {
"axios": "^0.18.0", "axios": "^0.18.0",
"body-parser": "^1.18.3", "body-parser": "^1.18.3",
"celebrate": "^9.1.0", "celebrate": "^9.1.0",
"connect-mongo": "^2.0.3", "connect-mongo": "^2.0.3",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.16.4", "express": "^4.16.4",
"express-session": "^1.16.1", "express-session": "^1.16.1",
...@@ -22,6 +24,7 @@ ...@@ -22,6 +24,7 @@
"querystring": "^0.2.0" "querystring": "^0.2.0"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^6.6.0" "eslint": "^6.6.0",
"nodemon": "^2.0.4"
} }
} }
...@@ -3,7 +3,7 @@ var fs = require("fs"); ...@@ -3,7 +3,7 @@ var fs = require("fs");
var path = require("path"); var path = require("path");
var { celebrate } = require("celebrate"); var { celebrate } = require("celebrate");
var { newToucan, validId, validGet } = require("../utils/schema"); var { newToucan, validId, validGet, validTitle } = require("../utils/schema");
var env = require("../.env"); var env = require("../.env");
var isLogged = require("../utils/authentification"); var isLogged = require("../utils/authentification");
var upload = require("../utils/fileSaver"); var upload = require("../utils/fileSaver");
...@@ -109,7 +109,7 @@ router.route("/delete/:id") ...@@ -109,7 +109,7 @@ router.route("/delete/:id")
// Renvoie la cover du toucan avec l'id donné // Renvoie la cover du toucan avec l'id donné
router.route("/img/:id") router.route("/img/:id")
.get(celebrate({params: validId}),function(req,res) { .get(celebrate({params: validId}),function(req,res) {
var imgPath = path.join(env.savedExtensions[0].path,"/",req.params.id); var imgPath = path.resolve(env.savedExtensions[0].path,req.params.id);
var fileKnown = false; var fileKnown = false;
env.savedExtensions[0].extensions.forEach(ext => { env.savedExtensions[0].extensions.forEach(ext => {
if (fs.existsSync(imgPath+ext)){ if (fs.existsSync(imgPath+ext)){
...@@ -124,6 +124,19 @@ router.route("/img/:id") ...@@ -124,6 +124,19 @@ router.route("/img/:id")
} }
}); });
router.use("/pdf",express.static(env.savedExtensions[1].path)); router.route("/pdf/:title")
.get(celebrate({params: validTitle}), function(req,res) {
Toucan.findOne({title: req.params.title}, (err,data) => {
if (err) {
res.end(500);
return;
} else if (!data) {
res.status(404).send("Toucan non trouvée");
return;
}
const id = data._id;
res.sendFile(path.resolve(env.savedExtensions[1].path,id+".pdf"));
}
);});
module.exports = router; module.exports = router;
\ No newline at end of file
...@@ -7,6 +7,12 @@ var newToucan = Joi.object({ ...@@ -7,6 +7,12 @@ var newToucan = Joi.object({
date: Joi.date().required() date: Joi.date().required()
}); });
var validTitle = Joi.object({
title: Joi.string()
.regex(/^[a-zA-Z0-9áàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ._\s'-]{1,60}$/) //Autorise la plupart des acents
.required(),
});
var validId = Joi.object({id: Joi.string().hex().length(24).required()}); var validId = Joi.object({id: Joi.string().hex().length(24).required()});
var validGet = Joi.object({ var validGet = Joi.object({
limit: Joi.number().integer(), limit: Joi.number().integer(),
...@@ -14,4 +20,4 @@ var validGet = Joi.object({ ...@@ -14,4 +20,4 @@ var validGet = Joi.object({
after: Joi.date().timestamp() after: Joi.date().timestamp()
}); });
module.exports = { newToucan, validId, validGet }; module.exports = { newToucan, validTitle, validId, validGet };
\ No newline at end of file \ No newline at end of file
This diff is collapsed.
...@@ -22,9 +22,9 @@ L'api possède les routes : ...@@ -22,9 +22,9 @@ L'api possède les routes :
Si l'oppération est un succès, on renvoie un message de succès et l'id du toucan enregistré. Si l'oppération est un succès, on renvoie un message de succès et l'id du toucan enregistré.
2. `toucan/pdf/:id` : 2. `toucan/pdf/:title` :
* `GET` Renvoie le pdf du toucan avec id comme _id dans la base de donnée. * `GET` Renvoie le pdf du toucan avec title comme titre (title) dans la base de donnée.
3. `toucan/img/:id` : 3. `toucan/img/:id` :
......
...@@ -49,7 +49,7 @@ function AllToucan(props){ ...@@ -49,7 +49,7 @@ function AllToucan(props){
{toucans.map( toucan => { {toucans.map( toucan => {
return <ToucanCard return <ToucanCard
image={`${props.backURL}/toucan/img/${toucan["_id"]}`} image={`${props.backURL}/toucan/img/${toucan["_id"]}`}
link={`${props.backURL}/toucan/pdf/${toucan["_id"]}.pdf`} link={`${props.backURL}/toucan/pdf/${toucan.title}`}
header={toucan.title} header={toucan.title}
toucanId={toucan._id} toucanId={toucan._id}
isAdmin={props.isAdmin} isAdmin={props.isAdmin}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment